Skip to content

Commit d6b814d

Browse files
committed
Add field types checkbox and checkboxes
1 parent b80fdef commit d6b814d

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/common/components/forms/user/RegisterForm.js

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const validate = (values) => {
2727
errors.password = 'Required';
2828
}
2929

30+
if (!values.isAgreeTerms) {
31+
errors.isAgreeTerms = 'Required';
32+
}
33+
3034
if (configs.recaptcha && !values.recaptcha) {
3135
errors.recaptcha = 'Required';
3236
}
@@ -100,6 +104,30 @@ class RegisterForm extends Component {
100104
type="password"
101105
placeholder="Password"
102106
/>
107+
<Field
108+
label="Skills"
109+
name="skills"
110+
component={FormField}
111+
type="checkboxes"
112+
style={{float: 'left', paddingRight: 20}}
113+
options={[{
114+
label: 'Nodejs',
115+
value: 'NODEJS',
116+
}, {
117+
label: 'Reactjs',
118+
value: 'REACTJS',
119+
}, {
120+
label: 'Redux',
121+
value: 'REDUX',
122+
}]}
123+
/>
124+
<Field
125+
label=" "
126+
name="isAgreeTerms"
127+
component={FormField}
128+
type="checkbox"
129+
text={<span>I agree the <a href="#">terms</a></span>}
130+
/>
103131
<Field
104132
label=" "
105133
name="recaptcha"

src/common/components/utils/BsForm.js

+41
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,47 @@ let BsFormField = ({
8282
<pre>Recaptcha is disabled</pre>
8383
);
8484
/* eslint-enable */
85+
} else if (type === 'checkbox') {
86+
formControl = (
87+
<div className="checkbox">
88+
<label>
89+
<input
90+
type="checkbox"
91+
{...input}
92+
{...rest}
93+
/> {text}
94+
</label>
95+
</div>
96+
);
97+
} else if (type === 'checkboxes') {
98+
// ref:
99+
// - <https://github.com/erikras/redux-form/issues/1037>
100+
formControl = (
101+
options.map((option, index) => (
102+
<div className="checkbox" key={option.value} {...rest}>
103+
<label>
104+
<input
105+
type="checkbox"
106+
name={`${input.name}[${index}]`}
107+
value={option.value}
108+
checked={input.value.indexOf(option.value) !== -1}
109+
onChange={event => {
110+
let newValue = [...input.value];
111+
112+
if (event.target.checked) {
113+
newValue.push(option.value);
114+
} else {
115+
newValue.splice(newValue.indexOf(option.value), 1);
116+
}
117+
118+
return input.onChange(newValue);
119+
}}
120+
/>
121+
{option.label}
122+
</label>
123+
</div>
124+
))
125+
);
85126
} else if (type === 'radiobutton') {
86127
// ref: <https://github.com/erikras/redux-form/issues/1857#issuecomment-249890206>
87128
formControl = (

0 commit comments

Comments
 (0)