diff --git a/ui_framework/doc_site/src/views/form/form_controls.js b/ui_framework/doc_site/src/views/form/form_controls.js index c066c3c576fde..e8bd0bcd85b46 100644 --- a/ui_framework/doc_site/src/views/form/form_controls.js +++ b/ui_framework/doc_site/src/views/form/form_controls.js @@ -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, + }, }; } @@ -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 (
@@ -99,11 +129,10 @@ export default class extends Component {
{} }, - { 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} />
); diff --git a/ui_framework/doc_site/src/views/form/form_rows.js b/ui_framework/doc_site/src/views/form/form_rows.js index 59f1572d34547..85155ce2a3322 100644 --- a/ui_framework/doc_site/src/views/form/form_rows.js +++ b/ui_framework/doc_site/src/views/form/form_rows.js @@ -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, + }, }; } @@ -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 ( @@ -131,11 +157,10 @@ export default class extends Component { label="Checkboxes" > {} }, - { 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} /> diff --git a/ui_framework/src/components/form/checkbox/checkbox_group.js b/ui_framework/src/components/form/checkbox/checkbox_group.js index e85925df8626b..2686163e4ff85 100644 --- a/ui_framework/src/components/form/checkbox/checkbox_group.js +++ b/ui_framework/src/components/form/checkbox/checkbox_group.js @@ -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 (
{options.map((option, index) => { - return (
{ className="kuiCheckbox__input" type="checkbox" id={option.id} - checked={option.checked} - onChange={option.onChange} + checked={option.checked || false} + onChange={onChange.bind(null, option)} />
@@ -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 = {