Skip to content

Commit

Permalink
Settings menu items unavailable when none already selected. (PP-102…
Browse files Browse the repository at this point in the history
…2) (#103)
  • Loading branch information
tdilauro authored Feb 29, 2024
1 parent 14d20cd commit d899ec1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/components/InputList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ export interface InputListProps {
labelAndDescription?: (setting: SettingData) => JSX.Element[];
setting: SettingData | CustomListsSetting;
disabled: boolean;
value: Array<string | {} | JSX.Element>;
value: Array<string | object | JSX.Element>;
altValue?: string;
additionalData?: any;
onSubmit?: any;
onEmpty?: string;
title?: string;
capitalize?: boolean;
onChange?: (value: any) => {};
onChange?: (value: any) => object;
readOnly?: boolean;
disableButton?: boolean;
}

export interface InputListState {
listItems: Array<string | {}>;
listItems: Array<string | object>;
newItem?: string;
options?: JSX.Element[];
}
Expand Down Expand Up @@ -225,7 +225,7 @@ export default class InputList extends React.Component<
}
}

renderToolTip(item: {} | string, format: string) {
renderToolTip(item: object | string, format: string) {
const icons = {
geographic: <LocatorIcon />,
};
Expand Down Expand Up @@ -262,14 +262,13 @@ export default class InputList extends React.Component<
}
}
// Items that have already been selected, and should be eliminated from the menu.
const listItems = this.state
? this.state.listItems
: this.props.value || this.props.setting.default;
const listItems =
this.state?.listItems ||
this.props.value ||
this.props.setting.default ||
[];
// Items that haven't been selected yet.
const remainingOptions = listItems
? allOptions.filter((o) => listItems.indexOf(o.props.value) < 0)
: [];
return remainingOptions;
return allOptions.filter((o) => !listItems.includes(o.props.value));
}

updateNewItem() {
Expand All @@ -280,7 +279,7 @@ export default class InputList extends React.Component<
this.setState({ ...this.state, ...{ newItem: (ref as any).getValue() } });
}

async removeListItem(listItem: string | {}) {
async removeListItem(listItem: string | object) {
await this.setState({
listItems: this.state.listItems.filter(
(stateListItem) => stateListItem !== listItem
Expand Down
18 changes: 18 additions & 0 deletions src/components/__tests__/InputList-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,24 @@ describe("InputList", () => {
);
expect(wrapper.state()["listItems"].includes("Option 1")).to.be.true;
});
it("doesn't lose options when nothing is selected", () => {
const setting = {
...wrapper.prop("setting"),
...{ type: "menu", default: null, format: "narrow" },
};
wrapper = mount(
<InputList
createEditableInput={createEditableInput}
labelAndDescription={labelAndDescription}
value={null}
setting={setting}
disabled={false}
/>,
{ context }
);
const options = wrapper.find("option");
expect(options.length).to.equal(3);
});
it("optionally eliminates already-selected options from the menu", () => {
let options = wrapper.find("option");
expect(options.length).to.equal(3);
Expand Down

0 comments on commit d899ec1

Please sign in to comment.