-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
215 additions
and
23 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { useState } from 'react'; | ||
import { Controller, useForm } from 'react-hook-form'; | ||
import { Button } from '../../lib/button/Button'; | ||
import { MultiStateCheckbox } from '../../lib/multistatecheckbox/MultiStateCheckbox'; | ||
import { classNames } from '../../lib/utils/Utils'; | ||
import { DocSectionCode } from '../common/docsectioncode'; | ||
import { DocSectionText } from '../common/docsectiontext'; | ||
|
||
export function ValidationDoc(props) { | ||
const [formData, setFormData] = useState({}); | ||
const defaultValues = { level: '' }; | ||
const form = useForm({ defaultValues }); | ||
const errors = form.formState.errors; | ||
|
||
const options = [ | ||
{ value: 'public', icon: 'pi pi-globe' }, | ||
{ value: 'protected', icon: 'pi pi-lock-open' }, | ||
{ value: 'private', icon: 'pi pi-lock' } | ||
]; | ||
|
||
const onSubmit = (data) => { | ||
setFormData(data); | ||
}; | ||
|
||
const getFormErrorMessage = (name) => { | ||
return errors[name] && <small className="p-error ml-2">{errors[name].message}</small>; | ||
}; | ||
|
||
const code = { | ||
basic: ` | ||
<Controller name="level" control={form.control} rules={{ required: 'Level is required.'}} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames({ 'p-error': errors.name })}>Level*</label> | ||
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" className={classNames({ 'p-invalid': fieldState.error })} /> | ||
{getFormErrorMessage(field.name)} | ||
</> | ||
)} | ||
/> | ||
`, | ||
javascript: ` | ||
import { useState } from 'react'; | ||
import { Controller, useForm } from 'react-hook-form'; | ||
import { Button } from '../../lib/button/Button'; | ||
import { MultiStateCheckbox } from "primereact/multistatecheckbox"; | ||
import { classNames } from '../../lib/utils/Utils'; | ||
export default function ValidationDemo() { | ||
const [formData, setFormData] = useState({}); | ||
const defaultValues = {level: ''}; | ||
const form = useForm({ defaultValues }); | ||
const errors = form.formState.errors; | ||
const options = [ | ||
{ value: 'public', icon: 'pi pi-globe' }, | ||
{ value: 'protected', icon: 'pi pi-lock-open' }, | ||
{ value: 'private', icon: 'pi pi-lock' } | ||
]; | ||
const onSubmit = (data) => { | ||
setFormData(data); | ||
}; | ||
const getFormErrorMessage = (name) => { | ||
return errors[name] && <small className="p-error ml-2">{errors[name].message}</small> | ||
}; | ||
return ( | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid"> | ||
<div className="field"> | ||
<Controller | ||
name="level" | ||
control={form.control} | ||
rules={{ required: 'Level is required.' }} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames('mr-2', { 'p-error': errors.level })}> | ||
Level* | ||
</label> | ||
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" 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 '../../lib/button/Button'; | ||
import { MultiStateCheckbox } from "primereact/multistatecheckbox"; | ||
import { classNames } from '../../lib/utils/Utils'; | ||
export default function InvalidDemo() { | ||
const [formData, setFormData] = useState<any>({}); | ||
const defaultValues = {level: ''}; | ||
const form = useForm({ defaultValues }); | ||
const errors = form.formState.errors; | ||
const options = [ | ||
{ value: 'public', icon: 'pi pi-globe' }, | ||
{ value: 'protected', icon: 'pi pi-lock-open' }, | ||
{ value: 'private', icon: 'pi pi-lock' } | ||
]; | ||
const onSubmit = (data: any) => { | ||
setFormData(data); | ||
}; | ||
const getFormErrorMessage = (name: string) => { | ||
return errors[name] && <small className="p-error ml-2">{errors[name].message}</small> | ||
}; | ||
return ( | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid"> | ||
<div className="field"> | ||
<Controller | ||
name="level" | ||
control={form.control} | ||
rules={{ required: 'Level is required.' }} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames('mr-2', { 'p-error': errors.level })}> | ||
Level* | ||
</label> | ||
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" 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="level" | ||
control={form.control} | ||
rules={{ required: 'Level is required.' }} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<label htmlFor={field.name} className={classNames('mr-2', { 'p-error': errors.level })}> | ||
Level* | ||
</label> | ||
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" 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} /> | ||
</> | ||
); | ||
} |
Oops, something went wrong.