react-hook-form#FieldValues TypeScript Examples
The following examples show how to use
react-hook-form#FieldValues.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: handle-form-error.tsx From admin with MIT License | 7 votes |
function getFormErrors(errors: DeepMap<FieldValues, FieldError>) {
const messages: string[] = Object.values(errors).reduce(
(acc, { message }) => {
if (message) {
acc.push(message)
}
return acc
},
[]
)
const refs = Object.values(errors).reduce((acc, { ref }) => {
if (ref) {
acc.push(ref)
}
return acc
}, [])
const list = (
<ul className="list-disc list-inside">
{messages.map((m) => (
<li>{m}</li>
))}
</ul>
)
const title =
messages.length > 1
? `There were ${messages.length} errors with your submission`
: "There was an error with your submission"
return { title, list, refs }
}
Example #2
Source File: handle-form-error.tsx From admin with MIT License | 6 votes |
handleFormError: SubmitErrorHandler<FieldValues> = (errors) => {
const { title, list, refs } = getFormErrors(errors)
if (refs?.[0] && refs[0].focus) {
refs[0].focus()
}
Controller.custom(
(t) => <FormErrorToaster toast={t} message={list} title={title} />,
{
position: "top-right",
duration: 3000,
ariaProps: {
role: "alert",
"aria-live": "polite",
},
}
)
}
Example #3
Source File: index.tsx From admin with MIT License | 5 votes |
UpdateNotification = ({ isLoading = false }) => {
const {
formState,
onSubmit,
handleSubmit,
resetForm,
additionalDirtyState,
} = useGiftCardForm()
const [visible, setVisible] = useState(false)
const [blocking, setBlocking] = useState(true)
const onUpdate = (values: FieldValues) => {
onSubmit({ ...values })
}
useEffect(() => {
const timeout = setTimeout(setBlocking, 300, false)
return () => clearTimeout(timeout)
}, [])
const isDirty = checkForDirtyState(
formState.dirtyFields,
additionalDirtyState
)
useEffect(() => {
if (!blocking) {
setVisible(isDirty)
}
return () => {
toast.dismiss(TOAST_ID)
}
}, [isDirty])
return (
<Toaster
visible={visible}
duration={Infinity}
id={TOAST_ID}
position="bottom-right"
>
<FormToasterContainer isLoading={isLoading}>
<FormToasterContainer.Actions>
<FormToasterContainer.ActionButton
onClick={handleSubmit(onUpdate, handleFormError)}
>
Save
</FormToasterContainer.ActionButton>
<FormToasterContainer.DiscardButton onClick={resetForm}>
Discard
</FormToasterContainer.DiscardButton>
</FormToasterContainer.Actions>
</FormToasterContainer>
</Toaster>
)
}
Example #4
Source File: edit.tsx From admin with MIT License | 5 votes |
UpdateNotification = ({ isLoading = false }) => {
const {
formState,
onSubmit,
handleSubmit,
resetForm,
additionalDirtyState,
} = useProductForm()
const [visible, setVisible] = useState(false)
const [blocking, setBlocking] = useState(true)
const onUpdate = (values: FieldValues) => {
onSubmit({ ...values })
}
useEffect(() => {
const timeout = setTimeout(setBlocking, 300, false)
return () => clearTimeout(timeout)
}, [])
const isDirty = checkForDirtyState(
formState.dirtyFields,
additionalDirtyState
)
useEffect(() => {
if (!blocking) {
setVisible(isDirty)
}
return () => {
toast.dismiss(TOAST_ID)
}
}, [isDirty])
return (
<Toaster
visible={visible}
duration={Infinity}
id={TOAST_ID}
position="bottom-right"
>
<FormToasterContainer isLoading={isLoading}>
<FormToasterContainer.Actions>
<FormToasterContainer.ActionButton
onClick={handleSubmit(onUpdate, handleFormError)}
>
Save
</FormToasterContainer.ActionButton>
<FormToasterContainer.DiscardButton onClick={resetForm}>
Discard
</FormToasterContainer.DiscardButton>
</FormToasterContainer.Actions>
</FormToasterContainer>
</Toaster>
)
}
Example #5
Source File: new.tsx From admin with MIT License | 5 votes |
SaveNotification = ({ isLoading = false }) => {
const {
formState,
onSubmit,
handleSubmit,
resetForm,
additionalDirtyState,
} = useProductForm()
const [visible, setVisible] = useState(false)
const onPublish = (values: FieldValues) => {
onSubmit({ ...values, status: "published" })
}
const onSaveDraft = (values: FieldValues) => {
onSubmit({ ...values, status: "draft" })
}
const isDirty = checkForDirtyState(
formState.dirtyFields,
additionalDirtyState
)
useEffect(() => {
if (isDirty) {
setVisible(true)
} else {
setVisible(false)
}
return () => {
toast.dismiss(TOAST_ID)
}
}, [isDirty])
return (
<Toaster
visible={visible}
duration={Infinity}
id={TOAST_ID}
position="bottom-right"
>
<FormToasterContainer isLoading={isLoading}>
<FormToasterContainer.Actions>
<FormToasterContainer.MultiActionButton
actions={[
{
label: "Save and publish",
onClick: handleSubmit(onPublish, handleFormError),
},
{
label: "Save as draft",
onClick: handleSubmit(onSaveDraft, handleFormError),
},
]}
>
Save
</FormToasterContainer.MultiActionButton>
<FormToasterContainer.DiscardButton onClick={resetForm}>
Discard
</FormToasterContainer.DiscardButton>
</FormToasterContainer.Actions>
</FormToasterContainer>
</Toaster>
)
}