fix: fixed showing default option label

This commit is contained in:
2025-11-03 10:42:13 +04:00
parent 03be3903cb
commit 311210394f
14 changed files with 98 additions and 60 deletions

View File

@ -1,51 +0,0 @@
import { useEffect, useState } from "react";
import { omit } from "lodash";
import useAttributeOptionsList from "@/app/module-editor/[moduleId]/components/shared/AttrOptionSelect/useAttributeOptionsList";
import ObjectSelect from "@/components/selects/ObjectSelect/ObjectSelect";
import { AttrOptionSchema } from "@/lib/client";
type Props = {
value: any;
onChange: (val: any) => void;
selectId: number;
error?: string;
label?: string;
};
const AttrOptionSelect = (props: Props) => {
const { options } = useAttributeOptionsList(props);
const [selectedOption, setSelectedOption] = useState<AttrOptionSchema>();
useEffect(() => {
if (!props.value) {
setSelectedOption(undefined);
return;
}
setSelectedOption(options.find(option => option.value === props.value));
}, [props.value, options]);
const restProps = omit(props, ["value, onChange", "selectId"]);
return (
<ObjectSelect
label={"Значение"}
{...restProps}
data={options}
value={selectedOption}
onChange={option => {
setSelectedOption(option);
props.onChange(option.value);
}}
onClear={() => {
setSelectedOption(undefined);
props.onChange(null);
}}
getLabelFn={option => option.label}
clearable
searchable
/>
);
};
export default AttrOptionSelect;

View File

@ -1,19 +0,0 @@
import { useQuery } from "@tanstack/react-query";
import { getAttrSelectOptionsOptions } from "@/lib/client/@tanstack/react-query.gen";
type Props = {
selectId: number;
};
const useAttributeSelectsList = ({ selectId }: Props) => {
const { data, refetch } = useQuery(
getAttrSelectOptionsOptions({ path: { selectId } })
);
return {
options: data?.items ?? [],
refetch,
};
};
export default useAttributeSelectsList;

View File

@ -3,14 +3,14 @@
import { useMemo } from "react";
import { DataTableColumn } from "mantine-datatable";
import { Center } from "@mantine/core";
import { useAttributesContext } from "@/app/attributes/contexts/AttributesContext";
import AttributeTableActions from "@/app/module-editor/[moduleId]/components/shared/AttributeTableActions/AttributeTableActions";
import { useModuleEditorContext } from "@/app/module-editor/[moduleId]/contexts/ModuleEditorContext";
import useIsMobile from "@/hooks/utils/useIsMobile";
import { AttributeSchema } from "@/lib/client";
const useAttributesTableColumns = () => {
const isMobile = useIsMobile();
const { attributesActions } = useAttributesContext();
const { attributeActions } = useModuleEditorContext();
return useMemo(
() =>
@ -24,7 +24,7 @@ const useAttributesTableColumns = () => {
accessor: "type.name",
render: attr =>
attr.type.type === "select"
? `Выбор "${attr.label}"`
? `Выбор "${attr.select?.label}"`
: attr.type.name,
},
{
@ -34,7 +34,7 @@ const useAttributesTableColumns = () => {
render: attribute => (
<AttributeTableActions
attribute={attribute}
{...attributesActions}
{...attributeActions}
/>
),
},

View File

@ -0,0 +1,29 @@
import ObjectSelect from "@/components/selects/ObjectSelect/ObjectSelect";
import useAttributeOptionsList from "@/hooks/lists/useAttributeOptionsList";
import { AttrOptionSchema } from "@/lib/client";
type Props = {
value?: AttrOptionSchema | null;
onChange: (val: AttrOptionSchema | null) => void;
selectId: number;
error?: string;
label?: string;
};
const DefaultAttrOptionSelect = ({ selectId, ...props }: Props) => {
const { options } = useAttributeOptionsList({ selectId });
return (
<ObjectSelect
label={"Значение"}
{...props}
data={options}
onClear={() => props.onChange(null)}
getLabelFn={(option: AttrOptionSchema) => option.label}
clearable
searchable
/>
);
};
export default DefaultAttrOptionSelect;

View File

@ -1,7 +1,7 @@
import { Checkbox, NumberInput, TextInput } from "@mantine/core";
import { DatePickerInput, DateTimePicker } from "@mantine/dates";
import { UseFormReturnType } from "@mantine/form";
import AttrOptionSelect from "@/app/module-editor/[moduleId]/components/shared/AttrOptionSelect/AttrOptionSelect";
import DefaultAttrOptionSelect from "@/app/module-editor/[moduleId]/components/shared/DefaultAttrOptionSelect/DefaultAttrOptionSelect";
import { AttributeSchema } from "@/lib/client";
import { naiveDateTimeStringToUtc } from "@/utils/datetime";
@ -91,11 +91,14 @@ const DefaultAttributeValueInput = ({ form }: Props) => {
} else if (type === "select") {
if (!form.values.select?.id) return <></>;
return (
<AttrOptionSelect
<DefaultAttrOptionSelect
label={"Значение по умолчанию"}
{...form.getInputProps("defaultValue")}
value={form.values.defaultValue}
onChange={value => form.setFieldValue("defaultValue", value)}
{...form.getInputProps("defaultOption")}
value={form.values.defaultOption}
onChange={value => {
form.setFieldValue("defaultOption", value);
form.setFieldValue("defaultOptionId", value?.id);
}}
selectId={form.values.select?.id}
/>
);

View File

@ -11,6 +11,8 @@ import PageBlock from "@/components/layout/PageBlock/PageBlock";
const PageBody = () => {
const { module } = useModuleEditorContext();
if (!module) return;
return (
<Stack h={"100%"}>
<PageBlock