Skip to content

Commit

Permalink
Add onChange prop to KuiCheckboxGroup. (elastic#13510)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed Aug 26, 2017
1 parent 9ac7927 commit 9d913f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
39 changes: 34 additions & 5 deletions ui_framework/doc_site/src/views/form/form_controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,32 @@ import {
KuiTextArea,
} from '../../../../components';

// Don't use this, make proper ids instead. This is just for the example.
function makeId() {
return Math.random().toString(36).substr(2, 5);
}

export default class extends Component {
constructor(props) {
super(props);

const idPrefix = makeId();

this.state = {
isSwitchChecked: false,
checkboxes: [{
id: `${idPrefix}0`,
label: 'Option one',
}, {
id: `${idPrefix}1`,
label: 'Option two is checked by default',
}, {
id: `${idPrefix}2`,
label: 'Option three',
}],
checkboxIdToSelectedMap: {
[`${idPrefix}1`]: true,
},
};
}

Expand All @@ -29,6 +49,16 @@ export default class extends Component {
});
}

onCheckboxChange = option => {
const newCheckboxIdToSelectedMap = Object.assign({}, this.state.checkboxIdToSelectedMap, {
[option.id]: !this.state.checkboxIdToSelectedMap[option.id],
});

this.setState({
checkboxIdToSelectedMap: newCheckboxIdToSelectedMap,
});
}

render() {
return (
<div>
Expand Down Expand Up @@ -99,11 +129,10 @@ export default class extends Component {
<br />

<KuiCheckboxGroup
options={[
{ id: '0', label: 'Option one', onChange: () => {} },
{ id: '1', label: 'Option two is checked by default', checked: true, onChange: () => {} },
{ id: '2', label: 'Option three', onChange: () => {} },
]}
options={this.state.checkboxes.map(checkbox => Object.assign({}, checkbox, {
checked: this.state.checkboxIdToSelectedMap[checkbox.id],
}))}
onChange={this.onCheckboxChange}
/>
</div>
);
Expand Down
35 changes: 30 additions & 5 deletions ui_framework/doc_site/src/views/form/form_rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,28 @@ import {
function makeId() {
return Math.random().toString(36).substr(2, 5);
}

export default class extends Component {
constructor(props) {
super(props);

const idPrefix = makeId();

this.state = {
isSwitchChecked: false,
checkboxes: [{
id: `${idPrefix}0`,
label: 'Option one',
}, {
id: `${idPrefix}1`,
label: 'Option two is checked by default',
}, {
id: `${idPrefix}2`,
label: 'Option three',
}],
checkboxIdToSelectedMap: {
[`${idPrefix}1`]: true,
},
};
}

Expand All @@ -35,6 +51,16 @@ export default class extends Component {
});
}

onCheckboxChange = option => {
const newCheckboxIdToSelectedMap = Object.assign({}, this.state.checkboxIdToSelectedMap, {
[option.id]: !this.state.checkboxIdToSelectedMap[option.id],
});

this.setState({
checkboxIdToSelectedMap: newCheckboxIdToSelectedMap,
});
}

render() {
return (
<KuiForm>
Expand Down Expand Up @@ -131,11 +157,10 @@ export default class extends Component {
label="Checkboxes"
>
<KuiCheckboxGroup
options={[
{ id: makeId(), label: 'Option one', onChange: () => {} },
{ id: makeId(), label: 'Option two is checked by default', checked: true, onChange: () => {} },
{ id: makeId(), label: 'Option three', onChange: () => {} },
]}
options={this.state.checkboxes.map(checkbox => Object.assign({}, checkbox, {
checked: this.state.checkboxIdToSelectedMap[checkbox.id],
}))}
onChange={this.onCheckboxChange}
/>
</KuiFormRow>
</KuiForm>
Expand Down
9 changes: 4 additions & 5 deletions ui_framework/src/components/form/checkbox/checkbox_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

export const KuiCheckboxGroup = ({ options, className, ...rest }) => {
export const KuiCheckboxGroup = ({ options, onChange, className, ...rest }) => {
const classes = classNames('kuiCheckbox kuiCheckboxGroup__item', className);

return (
<div>
{options.map((option, index) => {

return (
<div
className={classes}
Expand All @@ -19,8 +18,8 @@ export const KuiCheckboxGroup = ({ options, className, ...rest }) => {
className="kuiCheckbox__input"
type="checkbox"
id={option.id}
checked={option.checked}
onChange={option.onChange}
checked={option.checked || false}
onChange={onChange.bind(null, option)}
/>

<div className="kuiCheckbox__square">
Expand All @@ -45,10 +44,10 @@ KuiCheckboxGroup.propTypes = {
PropTypes.shape({
id: PropTypes.string,
checked: PropTypes.bool,
onChange: PropTypes.func.isRequired,
label: PropTypes.string,
}),
).isRequired,
onChange: PropTypes.func.isRequired,
};

KuiCheckboxGroup.defaultProps = {
Expand Down

0 comments on commit 9d913f4

Please sign in to comment.