Skip to content

Commit d02b7a9

Browse files
committed
fix(listbox): render 0 as a valid display value
1 parent 561c4ba commit d02b7a9

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/form/src/select/__tests__/utils.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createElement } from "react";
2-
import { getOptionId, getOptionLabel } from "../utils";
2+
import { getOptionId, getOptionLabel, getDisplayLabel } from "../utils";
33

44
describe("getOptionId", () => {
55
it("should return the correct id starting from 1 instead of 0", () => {
@@ -35,3 +35,37 @@ describe("getOptionLabel", () => {
3535
expect(getOptionLabel({ value: "A" }, "label")).toBe(null);
3636
});
3737
});
38+
39+
describe("getDisplayLabel", () => {
40+
it("should return null if there is no option", () => {
41+
expect(getDisplayLabel(null, "label", true)).toBe(null);
42+
expect(getDisplayLabel(null, "label", false)).toBe(null);
43+
expect(getDisplayLabel("", "label", true)).toBe(null);
44+
expect(getDisplayLabel("", "label", false)).toBe(null);
45+
});
46+
47+
it("should return the label if the includeLeft option is false or the option is not an object", () => {
48+
expect(getDisplayLabel(0, "label", true)).toBe(0);
49+
expect(getDisplayLabel(0, "label", false)).toBe(0);
50+
expect(getDisplayLabel("0", "label", true)).toBe("0");
51+
expect(getDisplayLabel("0", "label", false)).toBe("0");
52+
expect(getDisplayLabel("Hello", "label", true)).toBe("Hello");
53+
expect(getDisplayLabel("Hello", "label", false)).toBe("Hello");
54+
});
55+
56+
it("should return the TextIconSpacing component when includeLeft is enabled and the option is an prop object", () => {
57+
const option = {
58+
leftAddon: "Addon",
59+
label: "Some Words",
60+
};
61+
62+
const result = getDisplayLabel(option, "label", true);
63+
expect(result).toMatchInlineSnapshot(`
64+
<TextIconSpacing
65+
icon="Addon"
66+
>
67+
Some Words
68+
</TextIconSpacing>
69+
`);
70+
});
71+
});

packages/form/src/select/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function getDisplayLabel(
107107
labelKey: string,
108108
includeLeft: boolean
109109
): ReactNode {
110-
if (!option) {
110+
if (!option && option !== 0) {
111111
return null;
112112
}
113113

0 commit comments

Comments
 (0)