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

Adds possibility to disable checkboxes individually in an EuiCheckboxGroup component #2548

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Added `badge` prop and new styles `EuiHeaderAlert` ([#2506](https://github.com/elastic/eui/pull/2506))
- Added `disabled` prop to the `EuiCheckboxGroup` definition ([#2545](https://github.com/elastic/eui/pull/2545))
- Added `disabled` option to the `option` attribute of the `options` object that is passed to the `EuiCheckboxGroup` so that checkboxes in a group can be individually disabled ([#2548](https://github.com/elastic/eui/pull/2548))

## [`16.0.1`](https://github.com/elastic/eui/tree/v16.0.1)

Expand Down
32 changes: 29 additions & 3 deletions src-docs/src/views/form_controls/checkbox_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@ export default class extends Component {
return { ...checkbox, id: `${checkbox.id}_disabled` };
});

this.checkboxesIndividuallyDisabled = this.checkboxes.map(checkbox => {
const isIndividuallyDisabled =
checkbox.id.charAt(checkbox.id.length - 1) === '1';
return {
...checkbox,
id: `${checkbox.id}_individually_disabled`,
label: isIndividuallyDisabled
? 'Option two is individually disabled'
: checkbox.label,
disabled: isIndividuallyDisabled,
};
});

this.state = {
checkboxIdToSelectedMap: {
[`${idPrefix}1`]: true,
},
checkboxIdToSelectedMapDisabled: {
[`${idPrefix}1_disabled`]: true,
[`${idPrefix}1_individually_disabled`]: true,
},
};
}
Expand Down Expand Up @@ -75,10 +87,24 @@ export default class extends Component {

<EuiCheckboxGroup
options={this.checkboxesDisabled}
idToSelectedMap={this.state.checkboxIdToSelectedMapDisabled}
idToSelectedMap={this.state.checkboxIdToSelectedMap}
onChange={this.onChange}
disabled
/>

<EuiSpacer size="m" />

<EuiTitle size="xxs">
<h3>Individually disabled checkbox</h3>
</EuiTitle>

<EuiSpacer size="s" />

<EuiCheckboxGroup
options={this.checkboxesIndividuallyDisabled}
idToSelectedMap={this.state.checkboxIdToSelectedMap}
onChange={this.onChange}
/>
</Fragment>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiCheckboxGroup (mocked checkbox) individual disabled is rendered 1`] = `
<div>
<eui_checkbox
checked=""
class="euiCheckboxGroup__item"
disabled=""
id="1"
label="kibana"
/>
<eui_checkbox
class="euiCheckboxGroup__item"
id="2"
label="elastic"
/>
</div>
`;

exports[`EuiCheckboxGroup (mocked checkbox) disabled is rendered 1`] = `
<div>
<eui_checkbox
Expand Down
3 changes: 2 additions & 1 deletion src/components/form/checkbox/checkbox_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const EuiCheckboxGroup = ({
id={option.id}
checked={idToSelectedMap[option.id]}
label={option.label}
disabled={disabled}
disabled={disabled || option.disabled}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disabled needs to be added to this component's propTypes.options shape definition (lines 34-39). Similarly, the typescript definition needs to be updated -

export interface EuiCheckboxGroupOption {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

onChange={onChange.bind(null, option.id)}
compressed={compressed}
/>
Expand All @@ -35,6 +35,7 @@ EuiCheckboxGroup.propTypes = {
PropTypes.shape({
id: PropTypes.string.isRequired,
label: PropTypes.node,
disabled: PropTypes.bool,
})
).isRequired,
idToSelectedMap: PropTypes.objectOf(PropTypes.bool).isRequired,
Expand Down
18 changes: 18 additions & 0 deletions src/components/form/checkbox/checkbox_group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,22 @@ describe('EuiCheckboxGroup (mocked checkbox)', () => {

expect(component).toMatchSnapshot();
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test!

test('individual disabled is rendered', () => {
const component = render(
<EuiCheckboxGroup
options={[
{ id: '1', label: 'kibana', disabled: true },
{ id: '2', label: 'elastic' },
]}
idToSelectedMap={{
'1': true,
'2': false,
}}
onChange={() => {}}
/>
);

expect(component).toMatchSnapshot();
});
});
1 change: 1 addition & 0 deletions src/components/form/checkbox/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ declare module '@elastic/eui' {
export interface EuiCheckboxGroupOption {
id: string;
label?: ReactNode;
disabled?: boolean;
}

export interface EuiCheckboxGroupIdToSelectedMap {
Expand Down