Skip to content

Commit

Permalink
Refactor form inputs handling in PrismaTable
Browse files Browse the repository at this point in the history
The form inputs logic in PrismaTable has been simplified to be more efficient. The useFormContext control has been replaced with register and getFieldState functionalities. These changes clean up code redundancy and deliver better error message handling.
  • Loading branch information
AhmedElywa committed Jun 24, 2024
1 parent 379cb91 commit 5c9e869
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/admin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @paljs/admin

## 7.0.4

### Patch Changes

- Refactor form inputs handling in PrismaTable

## 7.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/admin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paljs/admin",
"version": "7.0.3",
"version": "7.0.4",
"main": "index.js",
"module": "./esm/index.js",
"types": "index.d.ts",
Expand Down
21 changes: 9 additions & 12 deletions packages/admin/src/PrismaTable/Form/Inputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,22 @@ const defaultInputs: Omit<FormInputs, 'Upload' | 'Editor'> = {
break;
}
}
const { control } = useFormContext();
const {
field: inputField,
fieldState: { error },
} = useController({
name: field.name,
control,
defaultValue: value,
rules: { required: field.required, ...getFieldValidation(field, inputValidation) },
});
const { register, getFieldState } = useFormContext();
const { error } = getFieldState(field.name);
return (
<div className="flex flex-wrap w-full sm:w-1/2 pr-2 pt-2">
<div className="w-full text-gray-600">
{field.title}
{error && <span className="text-red-700 text-xs">{error.message ? error.message : lang.isRequired}</span>}
{error && (
<span className="text-red-700 text-xs">
{typeof error.message === 'string' ? error.message : lang.isRequired}
</span>
)}
</div>
<input
className={classNames('w-full', inputClasses, error ? 'border-red-400' : '')}
{...inputField}
defaultValue={value}
{...register(field.name, { required: field.required, ...getFieldValidation(field, inputValidation) })}
{...options}
/>
</div>
Expand Down

0 comments on commit 5c9e869

Please sign in to comment.