mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: apply feedback
This commit is contained in:
parent
af502e97dd
commit
236c0bad19
@ -24,7 +24,6 @@ import {
|
|||||||
IContentType,
|
IContentType,
|
||||||
IContentTypeAttributes,
|
IContentTypeAttributes,
|
||||||
} from "@/types/content-type.types";
|
} from "@/types/content-type.types";
|
||||||
import { slugify } from "@/utils/string";
|
|
||||||
|
|
||||||
import { FieldInput } from "./components/FieldInput";
|
import { FieldInput } from "./components/FieldInput";
|
||||||
import { FIELDS_FORM_DEFAULT_VALUES } from "./constants";
|
import { FIELDS_FORM_DEFAULT_VALUES } from "./constants";
|
||||||
@ -44,10 +43,12 @@ export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
|||||||
formState: { errors },
|
formState: { errors },
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
} = useForm<IContentType>({
|
} = useForm<IContentType>({
|
||||||
defaultValues: contentType || {
|
defaultValues: contentType
|
||||||
name: "",
|
? { name: contentType.name, fields: contentType.fields }
|
||||||
fields: FIELDS_FORM_DEFAULT_VALUES,
|
: {
|
||||||
},
|
name: "",
|
||||||
|
fields: FIELDS_FORM_DEFAULT_VALUES,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const { append, fields, remove } = useFieldArray({
|
const { append, fields, remove } = useFieldArray({
|
||||||
name: "fields",
|
name: "fields",
|
||||||
@ -119,19 +120,10 @@ export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
|
|||||||
>
|
>
|
||||||
<FieldInput
|
<FieldInput
|
||||||
idx={idx}
|
idx={idx}
|
||||||
|
name={field.name}
|
||||||
|
remove={remove}
|
||||||
control={control}
|
control={control}
|
||||||
onLabelChange={(value) => {
|
setValue={setValue}
|
||||||
const fieldName = contentType?.fields?.find(
|
|
||||||
({ name }) => name === field.name,
|
|
||||||
)?.name;
|
|
||||||
|
|
||||||
if (!fieldName) {
|
|
||||||
setValue(`fields.${idx}.name`, value ? slugify(value) : "");
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
onRemove={() => {
|
|
||||||
remove(idx);
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</ContentItem>
|
</ContentItem>
|
||||||
))}
|
))}
|
||||||
|
@ -9,23 +9,28 @@
|
|||||||
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
|
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
|
||||||
import { MenuItem } from "@mui/material";
|
import { MenuItem } from "@mui/material";
|
||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import { Control, Controller } from "react-hook-form";
|
import { Control, Controller, UseFormSetValue } from "react-hook-form";
|
||||||
|
|
||||||
import { IconButton } from "@/app-components/buttons/IconButton";
|
import { IconButton } from "@/app-components/buttons/IconButton";
|
||||||
import { Input } from "@/app-components/inputs/Input";
|
import { Input } from "@/app-components/inputs/Input";
|
||||||
import { useTranslate } from "@/hooks/useTranslate";
|
import { useTranslate } from "@/hooks/useTranslate";
|
||||||
import { ContentFieldType, IContentType } from "@/types/content-type.types";
|
import { ContentFieldType, IContentType } from "@/types/content-type.types";
|
||||||
|
import { slugify } from "@/utils/string";
|
||||||
|
|
||||||
import { READ_ONLY_FIELDS } from "../constants";
|
import { READ_ONLY_FIELDS } from "../constants";
|
||||||
|
|
||||||
export const FieldInput = ({
|
export const FieldInput = ({
|
||||||
idx,
|
idx,
|
||||||
...props
|
name,
|
||||||
|
remove,
|
||||||
|
control,
|
||||||
|
setValue,
|
||||||
}: {
|
}: {
|
||||||
idx: number;
|
idx: number;
|
||||||
|
name: string;
|
||||||
|
remove: (index?: number | number[]) => void;
|
||||||
control: Control<IContentType>;
|
control: Control<IContentType>;
|
||||||
onRemove?: () => void;
|
setValue: UseFormSetValue<IContentType>;
|
||||||
onLabelChange?: (value: string) => void;
|
|
||||||
}) => {
|
}) => {
|
||||||
const { t } = useTranslate();
|
const { t } = useTranslate();
|
||||||
const isDisabled = useMemo(() => idx < READ_ONLY_FIELDS.length, [idx]);
|
const isDisabled = useMemo(() => idx < READ_ONLY_FIELDS.length, [idx]);
|
||||||
@ -36,13 +41,13 @@ export const FieldInput = ({
|
|||||||
variant="text"
|
variant="text"
|
||||||
color="error"
|
color="error"
|
||||||
size="medium"
|
size="medium"
|
||||||
onClick={() => props.onRemove?.()}
|
onClick={() => remove(idx)}
|
||||||
disabled={isDisabled}
|
disabled={isDisabled}
|
||||||
>
|
>
|
||||||
<DeleteOutlineIcon strokeWidth={1} fontSize="medium" />
|
<DeleteOutlineIcon strokeWidth={1} fontSize="medium" />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
<Controller
|
<Controller
|
||||||
control={props.control}
|
control={control}
|
||||||
name={`fields.${idx}.label`}
|
name={`fields.${idx}.label`}
|
||||||
rules={{ required: t("message.label_is_required") }}
|
rules={{ required: t("message.label_is_required") }}
|
||||||
render={({ field, fieldState }) => (
|
render={({ field, fieldState }) => (
|
||||||
@ -53,7 +58,11 @@ export const FieldInput = ({
|
|||||||
error={!!fieldState.error}
|
error={!!fieldState.error}
|
||||||
helperText={fieldState.error?.message}
|
helperText={fieldState.error?.message}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
props?.onLabelChange?.(e.target.value);
|
const value = e.target.value;
|
||||||
|
|
||||||
|
if (!name) {
|
||||||
|
setValue(`fields.${idx}.name`, value ? slugify(value) : "");
|
||||||
|
}
|
||||||
field.onChange(e);
|
field.onChange(e);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -64,11 +73,11 @@ export const FieldInput = ({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<Input disabled {...field} label={t("label.name")} />
|
<Input disabled {...field} label={t("label.name")} />
|
||||||
)}
|
)}
|
||||||
control={props.control}
|
control={control}
|
||||||
/>
|
/>
|
||||||
<Controller
|
<Controller
|
||||||
name={`fields.${idx}.type`}
|
name={`fields.${idx}.type`}
|
||||||
control={props.control}
|
control={control}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<Input
|
<Input
|
||||||
disabled={isDisabled}
|
disabled={isDisabled}
|
||||||
|
Loading…
Reference in New Issue
Block a user