Skip to content

Commit 61ea8dd

Browse files
authored
fix(select): falsy item text should fallback to value (#2152)
1 parent 436dea4 commit 61ea8dd

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

COMPONENT_INDEX.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,14 +3221,14 @@ None.
32213221

32223222
### Props
32233223

3224-
| Prop name | Required | Kind | Reactive | Type | Default value | Description |
3225-
| :-------- | :------- | :--------------- | :------- | --------------------------------- | ---------------------- | ----------------------------------------- |
3226-
| value | No | <code>let</code> | No | <code>string &#124; number</code> | <code>""</code> | Specify the option value |
3227-
| text | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the option text |
3228-
| hidden | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to hide the option |
3229-
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the option |
3230-
| class | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify the class of the `option` element |
3231-
| style | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify the style of the `option` element |
3224+
| Prop name | Required | Kind | Reactive | Type | Default value | Description |
3225+
| :-------- | :------- | :--------------- | :------- | --------------------------------- | ---------------------- | ---------------------------------------------------------------------------------- |
3226+
| value | No | <code>let</code> | No | <code>string &#124; number</code> | <code>""</code> | Specify the option value |
3227+
| text | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify the option text<br />If not specified, the value will be used as the text. |
3228+
| hidden | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to hide the option |
3229+
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the option |
3230+
| class | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify the class of the `option` element |
3231+
| style | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify the style of the `option` element |
32323232

32333233
### Slots
32343234

docs/src/COMPONENT_API.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12531,9 +12531,8 @@
1253112531
{
1253212532
"name": "text",
1253312533
"kind": "let",
12534-
"description": "Specify the option text",
12534+
"description": "Specify the option text\nIf not specified, the value will be used as the text.",
1253512535
"type": "string",
12536-
"value": "\"\"",
1253712536
"isFunction": false,
1253812537
"isFunctionDeclaration": false,
1253912538
"isRequired": false,

src/Select/SelectItem.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
*/
66
export let value = "";
77
8-
/** Specify the option text */
9-
export let text = "";
8+
/**
9+
* Specify the option text
10+
* If not specified, the value will be used as the text.
11+
* @type {string}
12+
*/
13+
export let text = undefined;
1014
1115
/** Set to `true` to hide the option */
1216
export let hidden = false;

tests/Select/Select.falsy.test.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@
77
<SelectItem value={0} text="Zero" />
88
<SelectItem value={1} text="One" />
99
</Select>
10+
11+
<Select labelText="Undefined text">
12+
<SelectItem value={2} />
13+
<SelectItem value={0} text="Zero" />
14+
<SelectItem value={1} text="One" />
15+
</Select>

tests/Select/Select.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,17 @@ describe("Select", () => {
240240
expect(skeleton.children[0]).toHaveClass("bx--skeleton");
241241
});
242242

243-
it("renders value if `text` is falsy", () => {
243+
it("renders `text` instead of `value` if `text` is an empty string", () => {
244244
render(SelectFalsy);
245245

246246
expect(screen.getByLabelText("Falsy text")).toHaveValue("-1");
247-
expect(screen.getByDisplayValue("")).toBeInTheDocument();
247+
expect(screen.getByRole("option", { name: "" })).toBeInTheDocument();
248+
});
249+
250+
it("renders value if `text` is undefined", () => {
251+
render(SelectFalsy);
252+
253+
expect(screen.getByLabelText("Undefined text")).toHaveValue("2");
254+
expect(screen.getByRole("option", { name: "2" })).toBeInTheDocument();
248255
});
249256
});

types/Select/SelectItem.svelte.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export type SelectItemProps = {
99

1010
/**
1111
* Specify the option text
12-
* @default ""
12+
* If not specified, the value will be used as the text.
13+
* @default undefined
1314
*/
1415
text?: string;
1516

0 commit comments

Comments
 (0)