Visitar URL original
Time Picker: Preserve initial input value in Time Picker component by aduth · Pull Request #4488 · uswds/uswds · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions spec/unit/time-picker/time-picker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ describe("time picker component", () => {
"",
"transfers required attribute to combo box"
);
assert.strictEqual(
select.value,
"13:00",
"transfers value attribute to combo box"
);
assert.strictEqual(
input.value,
"1:00pm",
"transfers value attribute to combo box"
);
assert.strictEqual(
select.getAttribute("name"),
"appointment-time",
Expand All @@ -71,9 +81,6 @@ describe("time picker component", () => {
null,
"should not transfer name attribute to combo box"
);

assert.strictEqual(select.value, "", "the select value should be empty");
assert.strictEqual(input.value, "", "the input should be empty");
});

it("should focus the first item found in the list from the query when pressing down from the input", () => {
Expand Down
1 change: 1 addition & 0 deletions spec/unit/time-picker/time-picker.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
id="appointment-time"
name="appointment-time"
type="text"
value="1:00pm"
required
/>
</div>
Expand Down
10 changes: 10 additions & 0 deletions src/components/time-picker/time-picker.config.yml
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
preview: "@uswds-form"

variants:
- name: default
label: Default

- name: default-value
label: Default Value
context:
timePicker:
defaultValue: "1:00pm"
9 changes: 8 additions & 1 deletion src/components/time-picker/time-picker.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<label class="usa-label" id="appointment-time-label" for="appointment-time">Appointment time</label>
<div class="usa-hint" id="appointment-time-hint">hh:mm</div>
<div class="usa-time-picker">
<input class="usa-input" id="appointment-time" name="appointment-time" type="text" aria-describedby="appointment-time-label appointment-time-hint">
<input
class="usa-input"
id="appointment-time"
name="appointment-time"
type="text"
aria-describedby="appointment-time-label appointment-time-hint"
{% if timePicker.defaultValue %}value="{{ timePicker.defaultValue }}"{% endif %}
>
</div>
</div>
5 changes: 5 additions & 0 deletions src/js/components/time-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ const transformTimePicker = (el) => {
Math.max(MIN_STEP, timePickerEl.dataset.step || DEFAULT_STEP)
);

let defaultValue;
for (let time = minTime; time <= maxTime; time += step) {
const { minute, hour24, hour12, ampm } = getTimeContext(time);

const option = document.createElement("option");
option.value = `${padZeros(hour24, 2)}:${padZeros(minute, 2)}`;
option.text = `${hour12}:${padZeros(minute, 2)}${ampm}`;
if (option.text === initialInputEl.value) {
defaultValue = option.value;
}
selectEl.appendChild(option);
}

Expand All @@ -113,6 +117,7 @@ const transformTimePicker = (el) => {
timePickerEl.dataset[key] = FILTER_DATASET[key];
});
timePickerEl.dataset.disableFiltering = "true";
timePickerEl.dataset.defaultValue = defaultValue;

timePickerEl.appendChild(selectEl);
initialInputEl.style.display = "none";
Expand Down