forked from primefaces/primereact
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix primefaces#3790: SelectButton focus method
- Loading branch information
Showing
4 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
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] && <small className="p-error">{errors[name].message}</small>; | ||
}; | ||
|
||
const code = { | ||
basic: ` | ||
<Controller name="firstname" control={form.control} rules={{ required: 'First Name is required.'}} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames({ 'p-error': errors.name })}>First Name*</label> | ||
<InputText id={field.name} {...field} className={classNames({ 'p-invalid': fieldState.error })} /> | ||
{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 { InputText } from "primereact/inputtext"; | ||
export default function ValidationDemo() { | ||
const [formData, setFormData] = useState({}); | ||
const defaultValues = {firstname: ''}; | ||
const form = useForm({ defaultValues }); | ||
const errors = form.formState.errors; | ||
const onSubmit = (data) => { | ||
setFormData(data); | ||
}; | ||
const getFormErrorMessage = (name) => { | ||
return errors[name] && <small className="p-error">{errors[name].message}</small> | ||
}; | ||
return ( | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid"> | ||
<div className="field"> | ||
<Controller | ||
name="firstname" | ||
control={form.control} | ||
rules={{ required: 'First Name is required.' }} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames({ 'p-error': errors.firstname })}> | ||
First Name* | ||
</label> | ||
<InputText id={field.name} {...field} className={classNames({ 'p-invalid': fieldState.error })} /> | ||
{getFormErrorMessage(field.name)} | ||
</> | ||
)} | ||
/> | ||
</div> | ||
<Button label="Submit" type="submit" icon="pi pi-check" /> | ||
</form> | ||
) | ||
} | ||
`, | ||
typescript: ` | ||
import { useState } from 'react'; | ||
import { Controller, useForm } from 'react-hook-form'; | ||
import { Button } from 'primereact/button'; | ||
import { classNames } from 'primereact/utils'; | ||
import { InputText } from "primereact/inputtext"; | ||
export default function InvalidDemo() { | ||
const [formData, setFormData] = useState<any>({}); | ||
const defaultValues = {firstname: ''}; | ||
const form = useForm({ defaultValues }); | ||
const errors = form.formState.errors; | ||
const onSubmit = (data: any) => { | ||
setFormData(data); | ||
}; | ||
const getFormErrorMessage = (name: string) => { | ||
return errors[name] && <small className="p-error">{errors[name].message}</small> | ||
}; | ||
return ( | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid"> | ||
<div className="field"> | ||
<Controller | ||
name="firstname" | ||
control={form.control} | ||
rules={{ required: 'First Name is required.' }} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames({ 'p-error': errors.firstname })}> | ||
First Name* | ||
</label> | ||
<InputText id={field.name} {...field} className={classNames({ 'p-invalid': fieldState.error })} /> | ||
{getFormErrorMessage(field.name)} | ||
</> | ||
)} | ||
/> | ||
</div> | ||
<Button label="Submit" type="submit" icon="pi pi-check" /> | ||
</form> | ||
) | ||
} | ||
` | ||
}; | ||
|
||
return ( | ||
<> | ||
<DocSectionText {...props}> | ||
<p> | ||
<a href="https://react-hook-form.com/">React Hook Form</a> is the most popular React library for form validation. The field will be highlighted and receive focus on validation failure. | ||
</p> | ||
</DocSectionText> | ||
<div className="card flex justify-content-center"> | ||
<div className="flex flex-column gap-2"> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid"> | ||
<div className="field"> | ||
<Controller | ||
name="engine" | ||
control={form.control} | ||
rules={{ required: 'Engine State is required.' }} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames({ 'p-error': errors.engine })}> | ||
Engine State* | ||
</label> | ||
<SelectButton id={field.name} options={options} {...field} className={classNames({ 'p-invalid': fieldState.error })} /> | ||
{getFormErrorMessage(field.name)} | ||
</> | ||
)} | ||
/> | ||
</div> | ||
<Button label="Submit" type="submit" icon="pi pi-check" /> | ||
</form> | ||
</div> | ||
</div> | ||
<DocSectionCode code={code} /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters