Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
fix: Implement Placeholder in Select Component (#165)
Browse files Browse the repository at this point in the history
* select placeholder option

* placeholder setup

* placeholder tests

* setup in story

* hiding placeholder tests

* placeholder option

* clean up unused imports
  • Loading branch information
Tiernebre committed Jul 29, 2021
1 parent c14a229 commit 3159a82
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
97 changes: 97 additions & 0 deletions src/components/form/select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,100 @@ it("can be registered as a React Hook Form uncontrolled component", async () =>
name: optionToChoose,
});
});

it("displays a placeholder option that is chosen by default if placeholder is provided", () => {
const placeholder = "Select from the MANY options below!";
render(
<Select placeholder={placeholder}>
<option value="A">A</option>
<option value="B">B</option>
</Select>
);
const placeholderOption = screen.getByRole("option", { name: placeholder });
expect(placeholderOption).toBeInTheDocument();
const select = screen.getByRole("combobox");
expect(select.firstChild?.isEqualNode(placeholderOption)).toEqual(true);
expect(select).toHaveValue("");
});

it("does not display a placeholder option if placeholder is not provided", () => {
render(
<Select>
<option value="A">A</option>
<option value="B">B</option>
</Select>
);
const select = screen.getByRole("combobox");
const optionA = screen.getByRole("option", { name: "A" });
expect(select).toHaveValue("A");
expect(select.firstChild?.isEqualNode(optionA)).toEqual(true);
});

it("hides the placeholder option if the user chose a valid option", () => {
const placeholder = "Select from the MANY options below!";
render(
<Select placeholder={placeholder} hidePlaceholderAfterChange>
<option value="A">A</option>
<option value="B">B</option>
</Select>
);
const placeholderOption = screen.getByRole("option", { name: placeholder });
expect(placeholderOption).toBeInTheDocument();
expect(placeholderOption).toBeVisible();
expect(screen.getAllByRole("option")).toHaveLength(3);
const select = screen.getByRole("combobox");
expect(select.firstChild?.isEqualNode(placeholderOption)).toEqual(true);
expect(select).toHaveValue("");
user.selectOptions(select, ["A"]);
const removedPlaceholderOption = screen.queryByRole("option", {
name: placeholder,
});
expect(removedPlaceholderOption).toBeNull();
expect(screen.getAllByRole("option")).toHaveLength(2);
});

it("does not hide the placeholder option if the user chose a valid option but hiding was disabled", () => {
const placeholder = "Select from the MANY options below!";
render(
<Select placeholder={placeholder} hidePlaceholderAfterChange={false}>
<option value="A">A</option>
<option value="B">B</option>
</Select>
);
const placeholderOption = screen.getByRole("option", { name: placeholder });
expect(placeholderOption).toBeInTheDocument();
expect(placeholderOption).toBeVisible();
expect(screen.getAllByRole("option")).toHaveLength(3);
const select = screen.getByRole("combobox");
expect(select.firstChild?.isEqualNode(placeholderOption)).toEqual(true);
expect(select).toHaveValue("");
user.selectOptions(select, ["A"]);
const removedPlaceholderOption = screen.queryByRole("option", {
name: placeholder,
});
expect(removedPlaceholderOption).not.toBeNull();
expect(screen.getAllByRole("option")).toHaveLength(3);
});

it("does not hide the placeholder option if the user chose a valid option by default", () => {
const placeholder = "Select from the MANY options below!";
render(
<Select placeholder={placeholder}>
<option value="A">A</option>
<option value="B">B</option>
</Select>
);
const placeholderOption = screen.getByRole("option", { name: placeholder });
expect(placeholderOption).toBeInTheDocument();
expect(placeholderOption).toBeVisible();
expect(screen.getAllByRole("option")).toHaveLength(3);
const select = screen.getByRole("combobox");
expect(select.firstChild?.isEqualNode(placeholderOption)).toEqual(true);
expect(select).toHaveValue("");
user.selectOptions(select, ["A"]);
const removedPlaceholderOption = screen.queryByRole("option", {
name: placeholder,
});
expect(removedPlaceholderOption).not.toBeNull();
expect(screen.getAllByRole("option")).toHaveLength(3);
});
20 changes: 19 additions & 1 deletion src/components/form/select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SelectHTMLAttributes } from "react";
import { SelectHTMLAttributes, useState } from "react";
import { UseFormRegisterReturn } from "react-hook-form";
import { CommonFormInputAttributes, Size } from "../../../types";
import {
Expand All @@ -16,6 +16,7 @@ export type SelectProps = HTMLSelectAttributes &
CommonFormInputAttributes & {
size?: Size;
register?: UseFormRegisterReturn;
hidePlaceholderAfterChange?: boolean;
};

const classNameMap: ClassNameTransformMap<SelectProps> = new Map([
Expand All @@ -32,14 +33,29 @@ export const Select = ({
describedBy,
register,
invalid = false,
placeholder,
hidePlaceholderAfterChange,
...props
}: SelectProps): JSX.Element => {
const [interactedWith, setInteractedWith] = useState(false);
const className = createClassNameFromProps(
classNameMap,
{ color, size, multiple } as Partial<SelectProps>,
["select"]
);

const onInput = () => {
if (hidePlaceholderAfterChange) {
setInteractedWith(true);
}
};

const placeholderOption = placeholder && (
<option disabled selected value="" hidden={interactedWith}>
{placeholder}
</option>
);

return (
<div className={className}>
<select
Expand All @@ -48,7 +64,9 @@ export const Select = ({
{...props}
aria-invalid={invalid}
aria-describedby={describedBy}
onInput={onInput}
>
{placeholderOption}
{children}
</select>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/stories/form/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export default {
type: "boolean",
},
},
placeholder: {
control: {
type: "text",
},
},
},
} as Meta<SelectProps>;

Expand Down

0 comments on commit 3159a82

Please sign in to comment.