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

[ChoiceList] Disable all choices at once #1758

Merged
merged 1 commit into from
Jul 2, 2019
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
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);
});
});
});
});