Skip to content

Commit

Permalink
fix(Select): display label for empty string value (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariser authored Feb 12, 2025
1 parent 970547e commit 3657279
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
40 changes: 40 additions & 0 deletions src/components/Select/SingleSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,46 @@ describe("Select", () => {
expect(getByTestId("select-trigger")).toHaveTextContent("Content0");
});

it("should render initially selected option with empty value", () => {
const OPTION_TEXT = "Empty option";

const { getByTestId } = renderSelect({
value: "",
options: [
{
value: "",
label: OPTION_TEXT,
},
],
});

const selectedValue = getByTestId("select-trigger");
expect(selectedValue.textContent).toBe(OPTION_TEXT);
});

it("should render manually selected option with empty value", () => {
const OPTION_TEXT = "Empty option";

const { queryByText, getByTestId } = renderSelect({
options: [
{
value: "",
label: OPTION_TEXT,
},
],
});

const selectTrigger = getByTestId("select-trigger");
expect(selectTrigger).not.toBeNull();
expect(selectTrigger.textContent).toBe("Select an option");
selectTrigger && fireEvent.click(selectTrigger);

const item = queryByText(OPTION_TEXT);
expect(item).not.toBeNull();
item && fireEvent.click(item);
expect(selectTrigger.textContent).toBe(OPTION_TEXT);
});

it("should close select on selecting item", () => {
const { queryByText } = renderSelect({});
const selectTrigger = queryByText("Select an option");
Expand Down
10 changes: 7 additions & 3 deletions src/components/Select/SingleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export const Select = ({
...props
}: SelectProps) => {
const [selectedValues, setSelectedValues] = useState<Array<string>>(
valueProp ? [valueProp] : defaultValue ? [defaultValue] : []
typeof valueProp === "string"
? [valueProp]
: typeof defaultValue === "string"
? [defaultValue]
: []
);
const [open, setOpen] = useState(false);

Expand Down Expand Up @@ -69,7 +73,7 @@ export const Select = ({
);

useUpdateEffect(() => {
setSelectedValues(valueProp ? [valueProp] : []);
setSelectedValues(typeof valueProp === "string" ? [valueProp] : []);
}, [valueProp]);

const conditionalProps: Partial<SelectOptionProp> = {};
Expand All @@ -82,7 +86,7 @@ export const Select = ({
return (
<InternalSelect
onChange={onChange}
value={valueProp ? [valueProp] : selectedValues}
value={typeof valueProp === "string" ? [valueProp] : selectedValues}
open={open}
onOpenChange={onOpenChange}
onSelect={onSelect}
Expand Down
5 changes: 3 additions & 2 deletions src/components/Select/SingleSelectValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ const SingleSelectValue = ({
valueNode?: SelectItemProps;
value: string;
}) => {
const { icon, iconDir, children, label } = valueNode ?? {};
if (!value) {
if (value === undefined || value === null) {
return null;
}

const { icon, iconDir, children, label } = valueNode ?? {};

return (
<SelectValueContainer>
<IconWrapper
Expand Down

0 comments on commit 3657279

Please sign in to comment.