diff options
Diffstat (limited to 'src/components/ui/Radio.tsx')
-rw-r--r-- | src/components/ui/Radio.tsx | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/components/ui/Radio.tsx b/src/components/ui/Radio.tsx new file mode 100644 index 000000000..594ea70e4 --- /dev/null +++ b/src/components/ui/Radio.tsx | |||
@@ -0,0 +1,77 @@ | |||
1 | import { Component } from 'react'; | ||
2 | import { observer } from 'mobx-react'; | ||
3 | import { Field } from 'mobx-react-form'; | ||
4 | import classnames from 'classnames'; | ||
5 | |||
6 | type Props = { | ||
7 | field: typeof Field; | ||
8 | className: string; | ||
9 | focus: boolean; | ||
10 | showLabel: boolean; | ||
11 | }; | ||
12 | |||
13 | @observer | ||
14 | class Radio extends Component<Props> { | ||
15 | static defaultProps = { | ||
16 | focus: false, | ||
17 | showLabel: true, | ||
18 | }; | ||
19 | |||
20 | inputElement = null; | ||
21 | |||
22 | componentDidMount() { | ||
23 | if (this.props.focus) { | ||
24 | this.focus(); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | focus() { | ||
29 | // @ts-expect-error Object is possibly 'null'. | ||
30 | this.inputElement.focus(); | ||
31 | } | ||
32 | |||
33 | render() { | ||
34 | const { field, className, showLabel } = this.props; | ||
35 | |||
36 | return ( | ||
37 | <div | ||
38 | className={classnames({ | ||
39 | 'franz-form__field': true, | ||
40 | 'has-error': field.error, | ||
41 | [`${className}`]: className, | ||
42 | })} | ||
43 | > | ||
44 | {field.label && showLabel && ( | ||
45 | <label className="franz-form__label" htmlFor={field.name}> | ||
46 | {field.label} | ||
47 | </label> | ||
48 | )} | ||
49 | <div className="franz-form__radio-wrapper"> | ||
50 | {field.options.map(type => ( | ||
51 | <label | ||
52 | key={type.value} | ||
53 | htmlFor={`${field.id}-${type.value}`} | ||
54 | className={classnames({ | ||
55 | 'franz-form__radio': true, | ||
56 | 'is-selected': field.value === type.value, | ||
57 | })} | ||
58 | > | ||
59 | <input | ||
60 | id={`${field.id}-${type.value}`} | ||
61 | type="radio" | ||
62 | name="type" | ||
63 | value={type.value} | ||
64 | onChange={field.onChange} | ||
65 | checked={field.value === type.value} | ||
66 | /> | ||
67 | {type.label} | ||
68 | </label> | ||
69 | ))} | ||
70 | </div> | ||
71 | {field.error && <div className="franz-form__error">{field.error}</div>} | ||
72 | </div> | ||
73 | ); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | export default Radio; | ||