FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(PF Checkbox): Add 3rd state to checkbox controlled by consumer by karelhala · Pull Request #2252 · patternfly/patternfly-react · GitHub

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

Filter by extension

Filter by extension .md  (1) .scss  (1) .snap  (1) .tsx  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
    • Checkbox.test.tsx
    • Checkbox.tsx
      • Checkbox.md
      • checkbox.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ test('controlled', () => {
expect(view).toMatchSnapshot();
});

test('controlled - 3rd state', () => {
const view = shallow(<Checkbox isChecked={null} id="check" aria-label="check" />);
expect(view).toMatchSnapshot();
});

test('uncontrolled', () => {
const view = shallow(<Checkbox id="check" aria-label="check" />);
expect(view).toMatchSnapshot();
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface CheckboxProps
/** Id of the checkbox. */
id: string;
/** Aria-label of the checkbox. */
'aria-label': string;
'aria-label'?: string;
}

// tslint:disable-next-line:no-empty
Expand Down Expand Up @@ -67,6 +67,8 @@ export class Checkbox extends React.Component<CheckboxProps> {
if ([false, true].includes(defaultChecked)) {
checkedProps.defaultChecked = defaultChecked;
}

checkedProps.checked = checkedProps.checked === null ? false : checkedProps.checked;
return (
<div className={css(styles.check, className)}>
<input
Expand All @@ -77,6 +79,7 @@ export class Checkbox extends React.Component<CheckboxProps> {
aria-invalid={!isValid}
aria-label={ariaLabel}
disabled={isDisabled}
ref={elem => elem && (elem.indeterminate = isChecked === null)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What's this ref needed for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This prop is can't be set trough react prop for some reason [1]. However plain js api allows to set it trough element, so this ref is to access checkox element. It's probably because indeterminate is a flag and not real prop though.

[1] react/react#1798

{...checkedProps}
/>
{label && (
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`controlled - 3rd state 1`] = `
<div
className="pf-c-check"
>
<input
aria-invalid={false}
aria-label="check"
className="pf-c-check__input"
disabled={false}
id="check"
onChange={[Function]}
type="checkbox"
/>
</div>
`;

exports[`controlled 1`] = `
<div
className="pf-c-check"
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ propComponents: ['Checkbox']
---

import { Checkbox } from '@patternfly/react-core';
import './checkbox.scss';

## Controlled checkbox
```js
Expand All @@ -17,7 +18,9 @@ class ControlledCheckbox extends React.Component {
super(props);
this.state = {
check1: false,
check2: false
check2: false,
check3: false,
check4: false
};
this.handleChange = (checked, event) => {
const target = event.target;
Expand All @@ -27,25 +30,58 @@ class ControlledCheckbox extends React.Component {
};
}

componentDidUpdate(_prevProps, prevState) {
if (prevState.check1 !== this.state.check1 && this.state.check1 !== null) {
this.setState({
check2: this.state.check1,
check3: this.state.check1,
})
}

if (prevState.check2 !== this.state.check2 || prevState.check3 !== this.state.check3) {
this.setState({
check1: (this.state.check2 && this.state.check3) || (this.state.check2 || this.state.check3 ? null : false)
})
}
}

render() {
return (
<React.Fragment>
<Checkbox
label="Controlled CheckBox"
label="Parent CheckBox"
isChecked={this.state.check1}
onChange={this.handleChange}
aria-label="controlled checkbox example"
id="check-1"
name="check1"
/>
<Checkbox
label="Controlled CheckBox"
className="nested"
label="Child CheckBox 1"
isChecked={this.state.check2}
onChange={this.handleChange}
aria-label="controlled checkbox example"
id="check-2"
name="check2"
/>
<Checkbox

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

would be good to wrap 2 checkboxes inside the checkbox and show the indeterminate state live. And can also get rid of several other duplicate examples

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah! That's a good point. I like it!

className="nested"
label="Child CheckBox 2"
isChecked={this.state.check3}
onChange={this.handleChange}
aria-label="controlled checkbox example"
id="check-3"
name="check3"
/>
<Checkbox
label="Controlled CheckBox"
isChecked={this.state.check4}
onChange={this.handleChange}
aria-label="controlled checkbox example"
id="check-4"
name="check4"
/>
</React.Fragment>
);
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.ws-preview {
& .pf-c-check.nested {
padding-left: var(--pf-global--spacer--md);
}
}

Back | FazBrowse Home | New Git URL