Skip to content

Commit

Permalink
Merge pull request #1758 from Shopify/disable-choicelist
Browse files Browse the repository at this point in the history
[ChoiceList] Disable all choices at once
  • Loading branch information
jtrollia authored Jul 2, 2019
2 parents 7f7755a + 40c23d9 commit add1996
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Enhancements

- Added support for disabling all choices in `ChoiceList` ([#1758](https://github.com/Shopify/polaris-react/pull/1758))

### Bug fixes

- Fixed a regression introduced in #1247, where icons inside of `Link` would always be recolored to match the text color ([#1729](https://github.com/Shopify/polaris-react/pull/1729))
Expand Down
7 changes: 5 additions & 2 deletions src/components/ChoiceList/ChoiceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface BaseProps {
titleHidden?: boolean;
/** Display an error message */
error?: Error;
/** Disable all choices **/
disabled?: boolean;
/** Callback when the selected choices change */
onChange?(selected: string[], name: string): void;
}
Expand All @@ -57,6 +59,7 @@ function ChoiceList({
selected,
onChange = noop,
error,
disabled = false,
name = getUniqueID(),
}: CombinedProps) {
// Type asserting to any is required for TS3.2 but can be removed when we update to 3.3
Expand All @@ -75,7 +78,7 @@ function ChoiceList({
) : null;

const choicesMarkup = choices.map((choice) => {
const {value, label, helpText, disabled} = choice;
const {value, label, helpText, disabled: choiceDisabled} = choice;

function handleChange(checked: boolean) {
onChange(
Expand All @@ -98,7 +101,7 @@ function ChoiceList({
name={finalName}
value={value}
label={label}
disabled={disabled}
disabled={choiceDisabled || disabled}
checked={choiceIsSelected(choice, selected)}
helpText={helpText}
onChange={handleChange}
Expand Down
25 changes: 25 additions & 0 deletions src/components/ChoiceList/tests/ChoiceList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('<ChoiceList />', () => {
label: string;
value: string;
helpText?: React.ReactNode;
disabled?: boolean;
renderChildren?(): React.ReactNode;
})[];

Expand Down Expand Up @@ -366,4 +367,28 @@ describe('<ChoiceList />', () => {
expect(element.find(InlineError)).toHaveLength(0);
});
});

describe('disabled', () => {
it('disables choices', () => {
const choiceElements = shallowWithAppProvider(
<ChoiceList selected={[]} choices={choices} disabled />,
).find(RadioButton);

choiceElements.forEach((choiceElement) => {
expect(choiceElement.prop('disabled')).toBe(true);
});
});

it('preserves disabled choices', () => {
choices = [choices[0], choices[1], {...choices[2], disabled: true}];

const choiceElements = shallowWithAppProvider(
<ChoiceList selected={[]} choices={choices} disabled />,
).find(RadioButton);

choiceElements.forEach((choiceElement) => {
expect(choiceElement.prop('disabled')).toBe(true);
});
});
});
});

0 comments on commit add1996

Please sign in to comment.