Skip to content

Commit

Permalink
[ChoiceList] Disable all choices at once
Browse files Browse the repository at this point in the history
  • Loading branch information
jtrollia committed Jun 28, 2019
1 parent 22bb8e8 commit 8a26d2e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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 radio buttons **/
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 8a26d2e

Please sign in to comment.