ES6 assets/js/src/form/fields/checkbox.jsx
This commit is contained in:
@@ -1,48 +1,43 @@
|
||||
define([
|
||||
'react',
|
||||
],
|
||||
(
|
||||
React
|
||||
) => {
|
||||
const FormFieldCheckbox = React.createClass({
|
||||
onValueChange: function onValueChange(e) {
|
||||
e.target.value = this.checkbox.checked ? '1' : '0';
|
||||
return this.props.onValueChange(e);
|
||||
},
|
||||
render: function render() {
|
||||
if (this.props.field.values === undefined) {
|
||||
return false;
|
||||
}
|
||||
import React from 'react';
|
||||
|
||||
// isChecked will be true only if the value is "1"
|
||||
// it will be false in case value is "0" or empty
|
||||
const isChecked = !!(Number(this.props.item[this.props.field.name]));
|
||||
const options = Object.keys(this.props.field.values).map(
|
||||
(value, index) => (
|
||||
<p key={`checkbox-${index}`}>
|
||||
<label htmlFor={this.props.field.name}>
|
||||
<input
|
||||
ref={(c) => { this.checkbox = c; }}
|
||||
type="checkbox"
|
||||
value="1"
|
||||
checked={isChecked}
|
||||
onChange={this.onValueChange}
|
||||
name={this.props.field.name}
|
||||
id={this.props.field.name}
|
||||
/>
|
||||
{ this.props.field.values[value] }
|
||||
</label>
|
||||
</p>
|
||||
)
|
||||
);
|
||||
const FormFieldCheckbox = React.createClass({
|
||||
onValueChange: function onValueChange(e) {
|
||||
e.target.value = this.checkbox.checked ? '1' : '0';
|
||||
return this.props.onValueChange(e);
|
||||
},
|
||||
render: function render() {
|
||||
if (this.props.field.values === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
{ options }
|
||||
</div>
|
||||
);
|
||||
},
|
||||
});
|
||||
// isChecked will be true only if the value is "1"
|
||||
// it will be false in case value is "0" or empty
|
||||
const isChecked = !!(Number(this.props.item[this.props.field.name]));
|
||||
const options = Object.keys(this.props.field.values).map(
|
||||
value => (
|
||||
<p key={`checkbox-${value}`}>
|
||||
<label htmlFor={this.props.field.name}>
|
||||
<input
|
||||
ref={(c) => { this.checkbox = c; }}
|
||||
type="checkbox"
|
||||
value="1"
|
||||
checked={isChecked}
|
||||
onChange={this.onValueChange}
|
||||
name={this.props.field.name}
|
||||
id={this.props.field.name}
|
||||
/>
|
||||
{ this.props.field.values[value] }
|
||||
</label>
|
||||
</p>
|
||||
)
|
||||
);
|
||||
|
||||
return FormFieldCheckbox;
|
||||
return (
|
||||
<div>
|
||||
{ options }
|
||||
</div>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export default FormFieldCheckbox;
|
||||
|
Reference in New Issue
Block a user