diff --git a/cypress/e2e/components/AsyncSelect.spec.ts b/cypress/e2e/components/AsyncSelect.spec.ts index f21b462e8..674d0932c 100644 --- a/cypress/e2e/components/AsyncSelect.spec.ts +++ b/cypress/e2e/components/AsyncSelect.spec.ts @@ -26,16 +26,30 @@ describe("AsyncSelect", () => { getMultiselect().contains("Mexico"); }); - it("clears all selected values", () => { - getMultiselect().click(); + describe("clears selected values", () => { + it("clears all multiselect values", () => { + getMultiselect().click(); - cy.focused().type("cana"); - assertDropDownIsOpen().contains("Canada"); - cy.focused().type("{enter}"); + cy.focused().type("cana"); + assertDropDownIsOpen().contains("Canada"); + cy.focused().type("{enter}"); + + getClearButton().click(); + + getMultiselect().contains("Select countries"); + }); + + it("clears single-select values", () => { + getSelectComponent().click(); + + cy.focused().type("cana"); + assertDropDownIsOpen().contains("Canada"); + cy.focused().type("{enter}"); - getClearButton().click(); + getClearButton().click(); - getMultiselect().contains("Please select a countries"); + getMultiselect().contains("Select countries"); + }); }); it("does not show the dropdown arrow", () => { diff --git a/cypress/e2e/components/Select.spec.ts b/cypress/e2e/components/Select.spec.ts index 521f5cbde..22a7e009d 100644 --- a/cypress/e2e/components/Select.spec.ts +++ b/cypress/e2e/components/Select.spec.ts @@ -129,6 +129,21 @@ describe("Select", () => { }); }); + describe("with clear button", () => { + beforeEach(() => { + cy.renderFromStorybook("select--with-a-clear-button"); + }); + + it("clears single-select values", () => { + getSelectComponent().click(); + cy.focused().type("{downarrow}").type("{enter}"); + + getClearButton().click(); + + getSelectComponent().contains("Please select inventory status"); + }); + }); + describe("with state", () => { beforeEach(() => { cy.renderFromStorybook("select--with-state"); diff --git a/src/AsyncSelect/AsyncSelect.story.tsx b/src/AsyncSelect/AsyncSelect.story.tsx index eede06673..37e29a499 100644 --- a/src/AsyncSelect/AsyncSelect.story.tsx +++ b/src/AsyncSelect/AsyncSelect.story.tsx @@ -59,7 +59,17 @@ export const WithDefaultOptions = () => ( classNamePrefix="SelectTest" labelText="Country" onInputChange={action("typed input value changed")} - defaultOptions={northAmericanCountries} + isClearable + defaultOptions={[ + { + value: "Canada", + label: "Canada", + }, + { + value: "United States", + label: "United States", + }, + ]} loadOptions={loadMatchingCountries} /> ); @@ -88,7 +98,7 @@ WithADefaultValue.story = { export const Multiselect = () => ( ( /> ); -Multiselect.story = { - name: "Multiselect", -}; +export const WithAClearButton = () => ( + +); export const UsingRefToControlFocus = () => { const ref = useRef(null); diff --git a/src/AsyncSelect/AsyncSelect.tsx b/src/AsyncSelect/AsyncSelect.tsx index f8a1925ac..d94251773 100644 --- a/src/AsyncSelect/AsyncSelect.tsx +++ b/src/AsyncSelect/AsyncSelect.tsx @@ -21,6 +21,7 @@ import { getSubset } from "../utils/subset"; type AsyncSelectProps = { windowThreshold?: number; + isClearable?: boolean; filterOption?: (...args: any[]) => any; autocomplete?: boolean; disabled?: boolean; @@ -92,6 +93,7 @@ const AsyncSelect = forwardRef( cacheOptions = false, defaultOptions, loadOptions, + isClearable, ...props }: AsyncSelectProps, ref @@ -150,6 +152,7 @@ const AsyncSelect = forwardRef( cacheOptions={cacheOptions} defaultOptions={defaultOptions} loadOptions={loadOptions} + isClearable={isClearable} /> diff --git a/src/Select/Select.story.tsx b/src/Select/Select.story.tsx index 29528137a..80864ada0 100644 --- a/src/Select/Select.story.tsx +++ b/src/Select/Select.story.tsx @@ -368,6 +368,18 @@ Required.story = { name: "set to required", }; +export const WithAClearButton = () => ( +