|
| 1 | +--- |
| 2 | +title: Radio |
| 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 | +[Click here](https://codesandbox.io/embed/unform-web-radio-example-er0cn?autoresize=1&expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark) to access a Radio Input Live Demo. |
| 8 | + |
| 9 | +## Radio input component |
| 10 | + |
| 11 | +```tsx lineNumbers=true |
| 12 | +import React, { useEffect, useRef, InputHTMLAttributes } from 'react'; |
| 13 | +import { useField } from '@unform/core'; |
| 14 | + |
| 15 | +interface Props extends InputHTMLAttributes<HTMLInputElement> { |
| 16 | + name: string; |
| 17 | + options: { |
| 18 | + id: string; |
| 19 | + value: string; |
| 20 | + label: string; |
| 21 | + }[]; |
| 22 | +} |
| 23 | + |
| 24 | +const RadioInput: React.FC<Props> = ({ name, options, ...rest }) => { |
| 25 | + const inputRefs = useRef<HTMLInputElement[]>([]); |
| 26 | + const { fieldName, registerField, defaultValue = '' } = useField(name); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + registerField({ |
| 30 | + name: fieldName, |
| 31 | + ref: inputRefs.current, |
| 32 | + getValue: (refs: HTMLInputElement[]) => { |
| 33 | + return refs.find(ref => ref.checked)?.value || ''; |
| 34 | + }, |
| 35 | + setValue: (refs: HTMLInputElement[], id: string) => { |
| 36 | + const inputRef = refs.find(ref => ref.id === id); |
| 37 | + if (inputRef) inputRef.checked = true; |
| 38 | + }, |
| 39 | + clearValue: (refs: HTMLInputElement[]) => { |
| 40 | + const inputRef = refs.find(ref => ref.checked === true); |
| 41 | + if (inputRef) inputRef.checked = false; |
| 42 | + }, |
| 43 | + }); |
| 44 | + }, [defaultValue, fieldName, registerField]); |
| 45 | + |
| 46 | + return ( |
| 47 | + <> |
| 48 | + {options.map(option => ( |
| 49 | + <label htmlFor={option.id} key={option.id}> |
| 50 | + <input |
| 51 | + ref={ref => inputRefs.current.push(ref as HTMLInputElement)} |
| 52 | + id={option.id} |
| 53 | + type="radio" |
| 54 | + name={name} |
| 55 | + defaultChecked={defaultValue.includes(option.id)} |
| 56 | + value={option.value} |
| 57 | + {...rest} |
| 58 | + /> |
| 59 | + {option.label} |
| 60 | + </label> |
| 61 | + ))} |
| 62 | + </> |
| 63 | + ); |
| 64 | +}; |
| 65 | + |
| 66 | +export default RadioInput; |
| 67 | +``` |
| 68 | + |
| 69 | +## Usage example |
| 70 | + |
| 71 | +```tsx lineNumbers=true |
| 72 | +const App: React.FC = () => { |
| 73 | + const radioOptions = [ |
| 74 | + { id: 'rocketseat', value: 'rocketseat', label: 'Rocketseat' }, |
| 75 | + { id: 'eliasgcf', value: 'eliasgcf', label: 'EliasGcf' }, |
| 76 | + ]; |
| 77 | + |
| 78 | + function handleSubmit(data) { |
| 79 | + /** |
| 80 | + * Out example when 'EliasGcf' marked: { user: 'eliasgcf' } |
| 81 | + * You get a string with the value |
| 82 | + */ |
| 83 | + console.log(data); |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <Form onSubmit={handleSubmit} initialData={{ radio: radioOptions[1].id }}> |
| 88 | + <RadioInput name="user" options={radioOptions} /> |
| 89 | + |
| 90 | + <button type="submit">Open</button> |
| 91 | + </Form> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +export default App; |
| 96 | +``` |
0 commit comments