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