Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 23f2765

Browse files
EliasGcfdiego3g
authored andcommitted
docs: Add radio input example
1 parent 039b430 commit 23f2765

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

docs/config/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
link: '/examples/file-input'
5858
- label: 'Checkbox'
5959
link: '/examples/checkbox'
60+
- label: 'Radio'
61+
link: '/examples/radio'
6062
- label: 'React Select'
6163
link: '/examples/react-select'
6264
- label: 'React DatePicker'

docs/src/docs/examples/radio.mdx

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)