Skip to content

Commit 060034c

Browse files
committed
feat(time-picker-select): support readonly state
1 parent 762a0c6 commit 060034c

11 files changed

Lines changed: 67 additions & 15 deletions

File tree

css/_time-picker.scss

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,21 @@
3636

3737
// The chevron in `TimePickerSelect` is purely decorative when the time
3838
// picker is read-only. The native `<select>` remains focusable so the value
39-
// is announced, but pointer-events are disabled to prevent opening the menu.
40-
.#{$prefix}--time-picker--readonly .#{$prefix}--select-input {
39+
// is announced, but the menu is suppressed via mousedown/keydown handlers.
40+
// Apply the same styling whether readonly is set on the parent `TimePicker`
41+
// or directly on a `TimePickerSelect` (for standalone use).
42+
.#{$prefix}--time-picker--readonly .#{$prefix}--select-input,
43+
.#{$prefix}--time-picker__select.#{$prefix}--select--readonly .#{$prefix}--select-input {
4144
border-bottom-color: $disabled-02;
42-
pointer-events: none;
45+
cursor: default;
46+
47+
&:hover {
48+
background-color: transparent;
49+
}
4350
}
4451

45-
.#{$prefix}--time-picker--readonly .#{$prefix}--select__arrow {
52+
.#{$prefix}--time-picker--readonly .#{$prefix}--select__arrow,
53+
.#{$prefix}--time-picker__select.#{$prefix}--select--readonly .#{$prefix}--select__arrow {
4654
fill: $disabled-02;
4755
}
4856
}

css/all.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/g10.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/g100.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/g80.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/g90.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/white.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/pages/components/TimePicker.svx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ Disable the time picker to prevent user interaction by setting `disabled` to `tr
130130

131131
## Read-only state
132132

133-
Set `readonly` to `true` to display a value while preventing edits. Unlike `disabled`, the input remains focusable and its value is submitted with the form.
133+
Set `readonly` to `true` to display a value while preventing edits. Unlike `disabled`, the input remains focusable and its value is submitted with the form. `TimePickerSelect` also accepts a `readonly` prop so the period and timezone selectors match.
134134

135135
<TimePicker readonly labelText="Cron job" placeholder="hh:mm" value="10:30">
136-
<TimePickerSelect value="pm">
136+
<TimePickerSelect readonly value="pm">
137137
<SelectItem value="am" text="AM" />
138138
<SelectItem value="pm" text="PM" />
139139
</TimePickerSelect>
140-
<TimePickerSelect value="pdt">
140+
<TimePickerSelect readonly value="pdt">
141141
<SelectItem value="pdt" text="PDT" />
142142
<SelectItem value="gmt" text="GMT" />
143143
</TimePickerSelect>

src/TimePicker/TimePickerSelect.svelte

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
/** Set to `true` to disable the select */
99
export let disabled = false;
1010
11+
/** Set to `true` for the select to be read-only */
12+
export let readonly = false;
13+
1114
/** Specify the ARIA label for the chevron icon */
1215
export let iconDescription = "Open list of options";
1316
@@ -46,6 +49,7 @@
4649
<div
4750
class:bx--select={true}
4851
class:bx--time-picker__select={true}
52+
class:bx--select--readonly={readonly}
4953
{...$$restProps}
5054
on:click
5155
on:mouseover
@@ -64,6 +68,7 @@
6468
{name}
6569
{disabled}
6670
{value}
71+
aria-readonly={readonly || undefined}
6772
class:bx--select-input={true}
6873
on:change={({ target }) => {
6974
selectedValue.set(target.value);
@@ -72,6 +77,22 @@
7277
on:input
7378
on:focus
7479
on:blur
80+
on:mousedown={(e) => {
81+
if (readonly) {
82+
e.preventDefault();
83+
e.currentTarget.focus();
84+
}
85+
}}
86+
on:keydown={(e) => {
87+
if (
88+
readonly &&
89+
e.key !== "Tab" &&
90+
e.key !== "Shift" &&
91+
!(e.altKey && e.key === "ArrowDown")
92+
) {
93+
e.preventDefault();
94+
}
95+
}}
7596
>
7697
<slot />
7798
</select>

tests/TimePicker/TimePicker.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,25 @@ describe("TimePicker", () => {
303303
const customLabel = screen.getByText("Custom label content");
304304
expect(customLabel).toBeInTheDocument();
305305
});
306+
307+
it("should support readonly on TimePickerSelect", async () => {
308+
const { container } = render(TimePickerCustom, {
309+
props: { selectReadonly: true },
310+
});
311+
312+
const selects = screen.getAllByRole("combobox") as HTMLSelectElement[];
313+
for (const select of selects) {
314+
expect(select).toHaveAttribute("aria-readonly", "true");
315+
expect(select.closest(".bx--time-picker__select")).toHaveClass(
316+
"bx--select--readonly",
317+
);
318+
}
319+
320+
const [select] = selects;
321+
const initialValue = select.value;
322+
await user.click(select);
323+
await user.keyboard("{ArrowDown}");
324+
expect(select.value).toBe(initialValue);
325+
container.remove();
326+
});
306327
});

0 commit comments

Comments
 (0)