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

Support for indeterminate checkboxes #1108

Merged
merged 6 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `3.4.0`.
- Added support for `indeterminate` to `EuiCheckbox` ([#1108](https://github.com/elastic/eui/pull/1108))

## [`3.4.0`](https://github.com/elastic/eui/tree/v3.4.0)

Expand Down
19 changes: 19 additions & 0 deletions src-docs/src/views/form_controls/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ export default class extends Component {

<EuiSpacer size="m" />

<EuiCheckbox
id={makeId()}
label="I am a checked checkbox"
checked={!this.state.checked}
onChange={this.onChange}
/>

<EuiSpacer size="m" />

<EuiCheckbox
id={makeId()}
label="I am an indeterminate checkbox"
checked={this.state.checked}
onChange={this.onChange}
indeterminate={!this.state.checked}
/>

<EuiSpacer size="m" />

<EuiCheckbox
id={makeId()}
label="I am a disabled checkbox"
Expand Down
131 changes: 80 additions & 51 deletions src/components/form/checkbox/checkbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

Expand All @@ -8,60 +8,89 @@ const typeToClassNameMap = {

export const TYPES = Object.keys(typeToClassNameMap);

export const EuiCheckbox = ({
className,
id,
checked,
label,
onChange,
type,
disabled,
compressed,
...rest
}) => {
const classes = classNames(
'euiCheckbox',
typeToClassNameMap[type],
{
'euiCheckbox--noLabel': !label,
'euiCheckbox--compressed': compressed
},
className
);

let optionalLabel;

if (label) {
optionalLabel = (
<label
className="euiCheckbox__label"
htmlFor={id}
export class EuiCheckbox extends Component {
componentDidMount() {
this.invalidateIndeterminate(this.props);
}

componentWillReceiveProps(nextProps) {
Copy link
Contributor

Choose a reason for hiding this comment

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

componentWillReceiveProps is deprecated, use componentDidUpdate instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.

this.invalidateIndeterminate(nextProps);
}

render() {
const {
className,
id,
checked,
label,
onChange,
type,
disabled,
compressed,
...rest
} = this.props;

const classes = classNames(
'euiCheckbox',
typeToClassNameMap[type],
{
'euiCheckbox--noLabel': !label,
'euiCheckbox--compressed': compressed
},
className
);

let optionalLabel;

if (label) {
optionalLabel = (
<label
className="euiCheckbox__label"
htmlFor={id}
>
{label}
</label>
);
}

return (
<div
className={classes}
>
{label}
</label>
<input
className="euiCheckbox__input"
type="checkbox"
id={id}
checked={checked}
onChange={onChange}
disabled={disabled}
ref={this.setInputRef}
{...rest}
/>

<div className="euiCheckbox__square" />

{optionalLabel}
</div>
);
}

return (
<div
className={classes}
>
<input
className="euiCheckbox__input"
type="checkbox"
id={id}
checked={checked}
onChange={onChange}
disabled={disabled}
{...rest}
/>

<div className="euiCheckbox__square" />

{optionalLabel}
</div>
);
};
setInputRef = (input) => {
this.inputRef = input;

if (this.props.inputRef) {
this.props.inputRef(input);
}

if (input) {
this.invalidateIndeterminate(this.props);
}
}

invalidateIndeterminate({ indeterminate }) {
this.inputRef.indeterminate = indeterminate;
}
}

EuiCheckbox.propTypes = {
className: PropTypes.string,
Expand Down