|
| 1 | +--- |
| 2 | +title: Checkbox |
| 3 | +--- |
| 4 | + |
| 5 | +> ⚠️ All examples below are using **TypeScript**, if you're not using it you can simply remove all type definitions as the `React.FC<Props>` from component definition. |
| 6 | +
|
| 7 | +## Checkbox input component |
| 8 | + |
| 9 | +```tsx lineNumbers=true |
| 10 | +import React, { useEffect, useRef, InputHTMLAttributes } from 'react'; |
| 11 | +import { useField } from '@unform/core'; |
| 12 | + |
| 13 | +interface Props extends InputHTMLAttributes<HTMLInputElement> { |
| 14 | + name: string; |
| 15 | + options: { |
| 16 | + id: string; |
| 17 | + value: string; |
| 18 | + label: string; |
| 19 | + }[]; |
| 20 | +} |
| 21 | + |
| 22 | +const CheckboxInput: React.FC<Props> = ({ name, options, ...rest }) => { |
| 23 | + const inputRefs = useRef<HTMLInputElement[]>([]); |
| 24 | + const { fieldName, registerField, defaultValue = [] } = useField(name); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + registerField({ |
| 28 | + name: fieldName, |
| 29 | + ref: inputRefs.current, |
| 30 | + getValue: (refs: HTMLInputElement[]) => { |
| 31 | + return refs.filter(ref => ref.checked).map(ref => ref.value); |
| 32 | + }, |
| 33 | + clearValue: (refs: HTMLInputElement[]) => { |
| 34 | + refs.forEach(ref => { |
| 35 | + ref.checked = false; |
| 36 | + }); |
| 37 | + }, |
| 38 | + setValue: (refs: HTMLInputElement[], values: string[]) => { |
| 39 | + refs.forEach(ref => { |
| 40 | + if (values.includes(ref.id)) { |
| 41 | + ref.checked = true; |
| 42 | + } |
| 43 | + }); |
| 44 | + }, |
| 45 | + }); |
| 46 | + }, [defaultValue, fieldName, registerField]); |
| 47 | + |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + {options.map((option, index) => ( |
| 51 | + <label htmlFor={option.id} key={option.id}> |
| 52 | + <input |
| 53 | + defaultChecked={defaultValue.find((dv: string) => dv === option.id)} |
| 54 | + ref={ref => { |
| 55 | + inputRefs.current[index] = ref as HTMLInputElement; |
| 56 | + }} |
| 57 | + value={option.value} |
| 58 | + type="checkbox" |
| 59 | + id={option.id} |
| 60 | + {...rest} |
| 61 | + /> |
| 62 | + {option.label} |
| 63 | + </label> |
| 64 | + ))} |
| 65 | + </div> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +export default CheckboxInput; |
| 70 | +``` |
| 71 | + |
| 72 | +## Usage example |
| 73 | + |
| 74 | +```jsx lineNumbers=true |
| 75 | +import React, { useRef } from 'react'; |
| 76 | +import { FormHandles } from '@unform/core'; |
| 77 | +import { Form } from '@unform/web'; |
| 78 | + |
| 79 | +import { CheckboxInput } from '../../components/Form'; |
| 80 | + |
| 81 | +interface CheckboxOption { |
| 82 | + id: string; |
| 83 | + value: string; |
| 84 | + label: string; |
| 85 | +} |
| 86 | + |
| 87 | +const CheckboxForm: React.FC = () => { |
| 88 | + const formRef = useRef<FormHandles>(null); |
| 89 | + |
| 90 | + const checkboxOptions: CheckboxOption[] = [ |
| 91 | + { id: 'node', value: 'node', label: 'Node' }, |
| 92 | + { id: 'react', value: 'react', label: 'ReactJS' }, |
| 93 | + ]; |
| 94 | + |
| 95 | + function handleSubmit(data) { |
| 96 | + /** |
| 97 | + * Out example when 'Node' marked: ['node'] |
| 98 | + * You get a string array with the value |
| 99 | + */ |
| 100 | + console.log(data); |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <Form |
| 105 | + ref={formRef} |
| 106 | + onSubmit={handleSubmit} |
| 107 | + initialData={{ checkbox: ['node'] }} |
| 108 | + > |
| 109 | + <CheckboxInput name="checkbox" options={checkboxOptions} /> |
| 110 | + |
| 111 | + <button type="submit">Enviar</button> |
| 112 | + </Form> |
| 113 | + ); |
| 114 | +}; |
| 115 | + |
| 116 | +export default CheckboxForm; |
| 117 | +``` |
0 commit comments