mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
admin ui: fix "enter" on create new modal (inside fields)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { typeboxResolver } from "@hookform/resolvers/typebox";
|
import { typeboxResolver } from "@hookform/resolvers/typebox";
|
||||||
|
import type { Static } from "core/utils";
|
||||||
import { type TAppDataEntityFields, entitiesSchema } from "data/data-schema";
|
import { type TAppDataEntityFields, entitiesSchema } from "data/data-schema";
|
||||||
import { useRef, useState } from "react";
|
import { useRef } from "react";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { MantineSelect } from "ui/components/form/hook-form-mantine/MantineSelect";
|
import { MantineSelect } from "ui/components/form/hook-form-mantine/MantineSelect";
|
||||||
import { useEvent } from "ui/hooks/use-event";
|
import { useEvent } from "ui/hooks/use-event";
|
||||||
@@ -11,31 +12,33 @@ import {
|
|||||||
import { ModalBody, ModalFooter, type TCreateModalSchema, useStepContext } from "./CreateModal";
|
import { ModalBody, ModalFooter, type TCreateModalSchema, useStepContext } from "./CreateModal";
|
||||||
|
|
||||||
const schema = entitiesSchema;
|
const schema = entitiesSchema;
|
||||||
|
type Schema = Static<typeof schema>;
|
||||||
|
|
||||||
export function StepEntityFields() {
|
export function StepEntityFields() {
|
||||||
const { nextStep, stepBack, state, setState } = useStepContext<TCreateModalSchema>();
|
const { nextStep, stepBack, state, setState } = useStepContext<TCreateModalSchema>();
|
||||||
const entity = state.entities?.create?.[0]!;
|
const entity = state.entities?.create?.[0]!;
|
||||||
const defaultFields = { id: { type: "primary", name: "id" } } as const;
|
const defaultFields = { id: { type: "primary", name: "id" } } as const;
|
||||||
const ref = useRef<EntityFieldsFormRef>(null);
|
const ref = useRef<EntityFieldsFormRef>(null);
|
||||||
|
const initial = entity
|
||||||
|
? entity
|
||||||
|
: {
|
||||||
|
fields: defaultFields,
|
||||||
|
config: {
|
||||||
|
sort_field: "id",
|
||||||
|
sort_dir: "asc"
|
||||||
|
}
|
||||||
|
};
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
formState: { isValid, errors },
|
formState: { isValid, errors },
|
||||||
getValues,
|
getValues,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
watch,
|
watch,
|
||||||
register,
|
|
||||||
setValue
|
setValue
|
||||||
} = useForm({
|
} = useForm({
|
||||||
mode: "onTouched",
|
mode: "onTouched",
|
||||||
resolver: typeboxResolver(schema),
|
resolver: typeboxResolver(schema),
|
||||||
defaultValues: {
|
defaultValues: initial as NonNullable<Schema>
|
||||||
...entity,
|
|
||||||
fields: defaultFields,
|
|
||||||
config: {
|
|
||||||
sort_field: "id",
|
|
||||||
sort_dir: "asc"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const values = watch();
|
const values = watch();
|
||||||
@@ -74,7 +77,11 @@ export function StepEntityFields() {
|
|||||||
Add fields to <strong>{entity.name}</strong>:
|
Add fields to <strong>{entity.name}</strong>:
|
||||||
</p>
|
</p>
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-1">
|
||||||
<EntityFieldsForm ref={ref} fields={defaultFields} onChange={updateListener} />
|
<EntityFieldsForm
|
||||||
|
ref={ref}
|
||||||
|
fields={initial.fields as any}
|
||||||
|
onChange={updateListener}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col gap-3">
|
<div className="flex flex-col gap-3">
|
||||||
@@ -82,7 +89,7 @@ export function StepEntityFields() {
|
|||||||
<div className="flex flex-row gap-2">
|
<div className="flex flex-row gap-2">
|
||||||
<MantineSelect
|
<MantineSelect
|
||||||
label="Field"
|
label="Field"
|
||||||
data={Object.keys(values.fields).filter((name) => name.length > 0)}
|
data={Object.keys(values.fields ?? {}).filter((name) => name.length > 0)}
|
||||||
placeholder="Select field"
|
placeholder="Select field"
|
||||||
name="config.sort_field"
|
name="config.sort_field"
|
||||||
allowDeselect={false}
|
allowDeselect={false}
|
||||||
@@ -102,7 +109,7 @@ export function StepEntityFields() {
|
|||||||
<div>
|
<div>
|
||||||
{Object.entries(errors).map(([key, value]) => (
|
{Object.entries(errors).map(([key, value]) => (
|
||||||
<p key={key}>
|
<p key={key}>
|
||||||
{key}: {value.message}
|
{key}: {(value as any).message}
|
||||||
</p>
|
</p>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -72,22 +72,16 @@ export const EntityFieldsForm = forwardRef<
|
|||||||
name,
|
name,
|
||||||
field
|
field
|
||||||
}));
|
}));
|
||||||
/*const entityFields = entity.fields.map((field) => ({
|
|
||||||
name: field.name,
|
|
||||||
field: field.toJSON()
|
|
||||||
}));*/
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
formState: { isValid, errors },
|
formState: { isValid, errors },
|
||||||
getValues,
|
getValues,
|
||||||
handleSubmit,
|
|
||||||
watch,
|
watch,
|
||||||
register,
|
register,
|
||||||
setValue,
|
setValue,
|
||||||
setError,
|
setError,
|
||||||
reset,
|
reset
|
||||||
clearErrors
|
|
||||||
} = useForm({
|
} = useForm({
|
||||||
mode: "all",
|
mode: "all",
|
||||||
resolver: typeboxResolver(schema),
|
resolver: typeboxResolver(schema),
|
||||||
@@ -114,7 +108,6 @@ export const EntityFieldsForm = forwardRef<
|
|||||||
props?.onChange?.(toCleanValues(data));
|
props?.onChange?.(toCleanValues(data));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
//props?.onChange?.()
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useImperativeHandle(ref, () => ({
|
useImperativeHandle(ref, () => ({
|
||||||
@@ -122,25 +115,9 @@ export const EntityFieldsForm = forwardRef<
|
|||||||
getValues: () => getValues(),
|
getValues: () => getValues(),
|
||||||
getData: () => {
|
getData: () => {
|
||||||
return toCleanValues(getValues());
|
return toCleanValues(getValues());
|
||||||
/*return Object.fromEntries(
|
|
||||||
getValues().fields.map((field) => [field.name, objectCleanEmpty(field.field)])
|
|
||||||
);*/
|
|
||||||
},
|
},
|
||||||
isValid: () => isValid
|
isValid: () => isValid
|
||||||
}));
|
}));
|
||||||
console.log("errors", errors.fields);
|
|
||||||
|
|
||||||
/*useEffect(() => {
|
|
||||||
console.log("change", values);
|
|
||||||
onSubmit(values);
|
|
||||||
}, [values]);*/
|
|
||||||
|
|
||||||
function onSubmit(data: TFieldsFormSchema) {
|
|
||||||
console.log("submit", isValid, data, errors);
|
|
||||||
}
|
|
||||||
function onSubmitInvalid(a, b) {
|
|
||||||
console.log("submit invalid", a, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleAppend(_type: keyof typeof fieldsSchemaObject) {
|
function handleAppend(_type: keyof typeof fieldsSchemaObject) {
|
||||||
const newField = {
|
const newField = {
|
||||||
@@ -151,7 +128,6 @@ export const EntityFieldsForm = forwardRef<
|
|||||||
config: {}
|
config: {}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
console.log("handleAppend", _type, newField);
|
|
||||||
append(newField);
|
append(newField);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,10 +141,7 @@ export const EntityFieldsForm = forwardRef<
|
|||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<form
|
<div className="flex flex-col gap-6">
|
||||||
onSubmit={handleSubmit(onSubmit as any, onSubmitInvalid)}
|
|
||||||
className="flex flex-col gap-6"
|
|
||||||
>
|
|
||||||
<div className="flex flex-col gap-3">
|
<div className="flex flex-col gap-3">
|
||||||
<div className="flex flex-col gap-4">
|
<div className="flex flex-col gap-4">
|
||||||
{sortable ? (
|
{sortable ? (
|
||||||
@@ -220,9 +193,7 @@ export const EntityFieldsForm = forwardRef<
|
|||||||
</Popover>
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" className="hidden" />
|
</div>
|
||||||
{/*<Debug watch={watch} errors={errors} />*/}
|
|
||||||
</form>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user