Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UI Framework] [K7] Add onChange prop to KuiCheckboxGroup. #13510

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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