Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
feat(Picky): Support disabled state
Browse files Browse the repository at this point in the history
Picky was missing a disabled state

fix #112
  • Loading branch information
Aidurber committed Mar 25, 2019
1 parent 12e0aa3 commit fb3f1d8
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/picky.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
box-sizing: border-box;
height: 38px;
}

.picky__input--disabled,
.picky__input--disabled + .picky__dropdown .option {
color: rgb(95, 94, 94);
}
.picky__input::after {
position: absolute;
width: 0;
Expand Down
9 changes: 7 additions & 2 deletions src/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const Option = props => {
style,
multiple,
tabIndex,
disabled,
} = props;
const cssClass = isSelected ? 'option selected' : 'option';
const body = isDataObject(item, labelKey, valueKey) ? item[labelKey] : item;
const inputType = multiple ? 'checkbox' : 'radio';
const select = () => selectValue(item);
const select = () => !disabled && selectValue(item);
return (
<div
tabIndex={tabIndex}
Expand All @@ -30,13 +31,16 @@ const Option = props => {
onClick={select}
onKeyPress={e => {
e.preventDefault();
selectValue(item);
if (!disabled) {
selectValue(item);
}
}}
>
<input
type={inputType}
readOnly
tabIndex={-1}
disabled={disabled}
checked={isSelected}
aria-label={body}
data-testid={'option-checkbox'}
Expand All @@ -60,5 +64,6 @@ Option.propTypes = {
selectValue: PropTypes.func.isRequired,
multiple: PropTypes.bool,
tabIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
disabled: PropTypes.bool,
};
export default Option;
5 changes: 5 additions & 0 deletions src/Picky.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
box-sizing: border-box;
height: 38px;
}

.picky__input--disabled,
.picky__input--disabled + .picky__dropdown .option {
color: rgb(95, 94, 94);
}
.picky__input::after {
position: absolute;
width: 0;
Expand Down
21 changes: 19 additions & 2 deletions src/Picky.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class Picky extends React.PureComponent {
* @memberof Picky
*/
toggleSelectAll() {
if (this.props.disabled) return;
this.setState(
state => {
return {
Expand Down Expand Up @@ -179,6 +180,7 @@ class Picky extends React.PureComponent {
render,
tabIndex,
renderList,
disabled,
} = this.props;
if (renderList) {
return renderList({
Expand All @@ -188,6 +190,7 @@ class Picky extends React.PureComponent {
tabIndex,
getIsSelected: this.isItemSelected,
selectValue: this.selectValue,
disabled,
});
}
return items.map((item, index) => {
Expand All @@ -205,6 +208,7 @@ class Picky extends React.PureComponent {
labelKey: labelKey,
valueKey: valueKey,
multiple: multiple,
disabled,
});
} else {
// Render a simple option
Expand All @@ -218,6 +222,7 @@ class Picky extends React.PureComponent {
valueKey={valueKey}
multiple={multiple}
tabIndex={tabIndex}
disabled={disabled}
id={this.state.id + '-option-' + index}
/>
);
Expand Down Expand Up @@ -281,7 +286,11 @@ class Picky extends React.PureComponent {
if (this.node && this.node.contains(e.target) && keepOpen) {
return;
}
if (this.filter && this.filter.filterInput && this.filter.filterInput.contains(e.target)) {
if (
this.filter &&
this.filter.filterInput &&
this.filter.filterInput.contains(e.target)
) {
return;
}
this.toggleDropDown();
Expand Down Expand Up @@ -344,6 +353,7 @@ class Picky extends React.PureComponent {
dropdownHeight,
renderSelectAll,
filterPlaceholder,
disabled,
} = this.props;
const { open } = this.state;
let ariaOwns = '';
Expand All @@ -369,7 +379,10 @@ class Picky extends React.PureComponent {
<button
id={`${this.state.id}__button`}
type="button"
className="picky__input"
className={[
'picky__input',
disabled ? 'picky__input--disabled' : '',
].join(' ')}
data-testid="picky-input"
onClick={this.toggleDropDown}
>
Expand Down Expand Up @@ -410,6 +423,7 @@ class Picky extends React.PureComponent {
toggleSelectAll: this.toggleSelectAll,
tabIndex,
multiple,
disabled,
})}
{!renderSelectAll &&
includeSelectAll &&
Expand All @@ -426,6 +440,7 @@ class Picky extends React.PureComponent {
this.state.allSelected ? 'option selected' : 'option'
}
onClick={this.toggleSelectAll}
disabled={disabled}
onKeyPress={this.toggleSelectAll}
>
<input
Expand All @@ -435,6 +450,7 @@ class Picky extends React.PureComponent {
tabIndex={-1}
checked={this.state.allSelected}
aria-label="select all"
disabled={disabled}
/>
<span data-testid="select-all-text">
{this.props.selectAllText}
Expand Down Expand Up @@ -491,6 +507,7 @@ Picky.propTypes = {
className: PropTypes.string,
renderList: PropTypes.func,
filterPlaceholder: PropTypes.string,
disabled: PropTypes.bool,
};

export default Picky;
26 changes: 26 additions & 0 deletions tests/Picky.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,30 @@ describe('Picky', () => {
expect(getByTestId('selectall-checkbox').checked).toEqual(true);
});
});

it('should be disabled when disabled prop supplied', () => {
const options = [1, 2];
const { getByTestId, rerender, debug } = rtlRender(
<Picky
options={options}
includeSelectAll
open={true}
multiple
disabled
value={[]}
/>
);

const selectAllCheckbox = getByTestId('selectall-checkbox');
expect(selectAllCheckbox.checked).toEqual(false);
fireEvent(
selectAllCheckbox,
new MouseEvent('click', {
bubbles: true,
cancelable: true,
})
);

expect(selectAllCheckbox.checked).toEqual(false);
});
});
Loading

0 comments on commit fb3f1d8

Please sign in to comment.