From e1acacb72c5aca99fc3121edaf29b2d4bb7151b9 Mon Sep 17 00:00:00 2001 From: melloware Date: Fri, 16 Dec 2022 08:53:45 -0500 Subject: [PATCH] Fix #3790: SelectButton focus method --- components/doc/selectbutton/validationdoc.js | 161 ++++++++++++++++++ components/lib/selectbutton/SelectButton.js | 11 +- components/lib/selectbutton/selectbutton.d.ts | 3 +- pages/selectbutton/index.js | 14 +- 4 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 components/doc/selectbutton/validationdoc.js diff --git a/components/doc/selectbutton/validationdoc.js b/components/doc/selectbutton/validationdoc.js new file mode 100644 index 0000000000..89b5d97601 --- /dev/null +++ b/components/doc/selectbutton/validationdoc.js @@ -0,0 +1,161 @@ +import { useState } from 'react'; +import { Controller, useForm } from 'react-hook-form'; +import { Button } from '../../lib/button/Button'; +import { SelectButton } from '../../lib/selectbutton/SelectButton'; +import { classNames } from '../../lib/utils/Utils'; +import { DocSectionCode } from '../common/docsectioncode'; +import { DocSectionText } from '../common/docsectiontext'; + +export function ValidationDoc(props) { + const options = ['Off', 'On']; + const [formData, setFormData] = useState({}); + const defaultValues = { engine: '' }; + const form = useForm({ defaultValues }); + const errors = form.formState.errors; + + const onSubmit = (data) => { + setFormData(data); + }; + + const getFormErrorMessage = (name) => { + return errors[name] && {errors[name].message}; + }; + + const code = { + basic: ` + ( + <> + + + {getFormErrorMessage(field.name)} + + )} +/> + `, + javascript: ` +import { useState } from 'react'; +import { Controller, useForm } from 'react-hook-form'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; +import { SelectButton } from "primereact/selectbutton"; + +export default function ValidationDemo() { + const options = ['Off', 'On']; + const [formData, setFormData] = useState({}); + const defaultValues = { engine: '' }; + const form = useForm({ defaultValues }); + const errors = form.formState.errors; + + const onSubmit = (data) => { + setFormData(data); + }; + + const getFormErrorMessage = (name) => { + return errors[name] && {errors[name].message} + }; + + return ( +
+
+ ( + <> + + + {getFormErrorMessage(field.name)} + + )} + /> +
+