Skip to content

Commit

Permalink
add required asterisk
Browse files Browse the repository at this point in the history
  • Loading branch information
spiderman authored and spiderman committed Dec 3, 2024
1 parent 610c143 commit 20abdb8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/remult-uikit/src/Controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
ControllerRenderProps,
FieldValues,
} from 'react-hook-form'
import { getValidator } from './util'
import { getValidator, isRequired } from './util'
import { Repository } from 'remult'

type ControllerPropsWithRepo<T> = Omit<ControllerProps, 'name'> & {
Expand All @@ -29,9 +29,13 @@ const Controller = <T,>(props: ControllerPropsWithRepo<T>) => {
validate: getValidator(props.repo, props.name as keyof T),
}}
render={({ field, fieldState, formState }) => {
// @ts-expect-error
const isRequiredField = isRequired(props.repo.fields[props.name])
const newField: ControllerRenderPropsWithRemult = {
...field,
label: props.repo.fields[props.name as keyof T].caption,
label: `${props.repo.fields[props.name as keyof T].caption}${
isRequiredField ? ' *' : ''
}`,
// required: isRequired(props.repo.fields[props.name]),
}
return props.render({ field: newField, fieldState, formState })
Expand Down
3 changes: 2 additions & 1 deletion packages/remult-uikit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const useRemultForm = <T>(repo: Repository<T>) => {
const repoResolver = <T>(repo: Repository<T>) => {
return async (values: T) => {
const errors = await repo.validate(values)
if (errors && errors.modelState)
if (errors && errors.modelState) {
return {
values,
errors: Object.fromEntries(
Expand All @@ -94,6 +94,7 @@ const repoResolver = <T>(repo: Repository<T>) => {
])
),
}
}
return { values, errors: {} }
}
}
Expand Down

0 comments on commit 20abdb8

Please sign in to comment.