Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Date Field add data-placeholder attribute and delete improv #808

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
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
77 changes: 64 additions & 13 deletions packages/bits-ui/src/lib/bits/date-field/date-field.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,20 +894,30 @@ class DateFieldDaySegmentState {
}
}

if (isBackspace(e.key)) {
if (isBackspace(e.key) || isDelete(e.key)) {
this.#root.states.day.hasLeftFocus = false;
let moveToPrev = false;
this.#updateSegment("day", (prev) => {
this.#root.states.day.hasLeftFocus = false;
if (prev === null) {
this.#announcer.announce(null);
moveToPrev = true;
return null;
}
if (prev.length === 2 && prev.startsWith("0")) {

// Handle CTRL+A+Backspace or full selection + Backspace
if (document.getSelection()?.toString() === prev) {
this.#announcer.announce("Empty");
return null;
}

const str = prev.toString();
if (str.length === 1) return null;
return str.slice(0, -1);
if (str.length === 1) {
this.#announcer.announce(null);
return null;
}
const next = Number.parseInt(str.slice(0, -1));
this.#announcer.announce(next);
return `${next}`;
});

if (moveToPrev) {
Expand Down Expand Up @@ -945,6 +955,7 @@ class DateFieldDaySegmentState {
"aria-valuemax": getDaysInMonth(toDate(date)),
"aria-valuenow": date.day,
"aria-valuetext": this.#root.segmentValues.day === null ? "Empty" : `${date.day}`,
"data-placeholder": this.#root.segmentValues.day == null ? "true" : undefined,
onkeydown: this.#onkeydown,
onfocusout: this.#onfocusout,
onclick: this.#root.handleSegmentClick,
Expand Down Expand Up @@ -1149,7 +1160,7 @@ class DateFieldMonthSegmentState {
}
}

if (isBackspace(e.key)) {
if (isBackspace(e.key) || isDelete(e.key)) {
this.#root.states.month.hasLeftFocus = false;
let moveToPrev = false;
this.#updateSegment("month", (prev) => {
Expand All @@ -1159,8 +1170,9 @@ class DateFieldMonthSegmentState {
return null;
}

if (prev.length === 2 && prev.startsWith("0")) {
this.#announcer.announce(null);
// Handle CTRL+A+Backspace or full selection + Backspace
if (document.getSelection()?.toString() === prev) {
this.#announcer.announce("Empty");
return null;
}

Expand Down Expand Up @@ -1213,6 +1225,7 @@ class DateFieldMonthSegmentState {
this.#root.segmentValues.month === null
? "Empty"
: `${date.month} - ${this.#root.formatter.fullMonth(toDate(date))}`,
"data-placeholder": this.#root.segmentValues.month === null ? "true" : undefined,
onkeydown: this.#onkeydown,
onfocusout: this.#onfocusout,
onclick: this.#root.handleSegmentClick,
Expand Down Expand Up @@ -1383,7 +1396,7 @@ class DateFieldYearSegmentState {
}
}

if (isBackspace(e.key)) {
if (isBackspace(e.key) || isDelete(e.key)) {
this.#pressedKeys = [];
this.#incrementBackspaceCount();
let moveToPrev = false;
Expand All @@ -1394,6 +1407,15 @@ class DateFieldYearSegmentState {
this.#announcer.announce(null);
return null;
}

// Handle CTRL+A+Backspace

if (document.getSelection()?.toString() !== "") {
this.#root.states.year.hasLeftFocus = false;
this.#announcer.announce("Empty");
return null;
}

const str = prev.toString();
if (str.length === 1) {
this.#announcer.announce(null);
Expand Down Expand Up @@ -1450,6 +1472,7 @@ class DateFieldYearSegmentState {
onkeydown: this.#onkeydown,
onclick: this.#root.handleSegmentClick,
onfocusout: this.#onfocusout,
"data-placeholder": isEmpty ? "true" : undefined,
...this.#root.getBaseSegmentAttrs("year", this.#id.current),
};
});
Expand Down Expand Up @@ -1658,7 +1681,7 @@ class DateFieldHourSegmentState {
}
}

if (isBackspace(e.key)) {
if (isBackspace(e.key) || isDelete(e.key)) {
this.#root.states.hour.hasLeftFocus = false;
let moveToPrev = false;
this.#updateSegment("hour", (prev) => {
Expand All @@ -1667,6 +1690,13 @@ class DateFieldHourSegmentState {
moveToPrev = true;
return null;
}

// Handle CTRL+A+Backspace or full selection + Backspace
if (document.getSelection()?.toString() === prev) {
this.#announcer.announce("Empty");
return null;
}

const str = prev.toString();
if (str.length === 1) {
this.#announcer.announce(null);
Expand Down Expand Up @@ -1716,6 +1746,7 @@ class DateFieldHourSegmentState {
onkeydown: this.#onkeydown,
onfocusout: this.#onfocusout,
onclick: this.#root.handleSegmentClick,
"data-placeholder": isEmpty ? "true" : undefined,
...this.#root.getBaseSegmentAttrs("hour", this.#id.current),
};
});
Expand Down Expand Up @@ -1902,7 +1933,7 @@ class DateFieldMinuteSegmentState {
return;
}

if (isBackspace(e.key)) {
if (isBackspace(e.key) || isDelete(e.key)) {
this.#root.states.minute.hasLeftFocus = false;
let moveToPrev = false;
this.#updateSegment("minute", (prev) => {
Expand All @@ -1911,6 +1942,13 @@ class DateFieldMinuteSegmentState {
this.#announcer.announce("Empty");
return null;
}

// Handle CTRL+A+Backspace or full selection + Backspace
if (document.getSelection()?.toString() === prev) {
this.#announcer.announce("Empty");
return null;
}

const str = prev.toString();
if (str.length === 1) {
this.#announcer.announce("Empty");
Expand Down Expand Up @@ -1961,6 +1999,7 @@ class DateFieldMinuteSegmentState {
onkeydown: this.#onkeydown,
onfocusout: this.#onfocusout,
onclick: this.#root.handleSegmentClick,
"data-placeholder": isEmpty ? "true" : undefined,
...this.#root.getBaseSegmentAttrs("minute", this.#id.current),
};
});
Expand Down Expand Up @@ -2146,7 +2185,7 @@ class DateFieldSecondSegmentState {
}
}

if (isBackspace(e.key)) {
if (isBackspace(e.key) || isDelete(e.key)) {
this.#root.states.second.hasLeftFocus = false;
let moveToPrev = false;
this.#updateSegment("second", (prev) => {
Expand All @@ -2155,6 +2194,13 @@ class DateFieldSecondSegmentState {
this.#announcer.announce(null);
return null;
}

// Handle CTRL+A+Backspace or full selection + Backspace
if (document.getSelection()?.toString() === prev) {
this.#announcer.announce("Empty");
return null;
}

const str = prev.toString();
if (str.length === 1) {
this.#announcer.announce(null);
Expand Down Expand Up @@ -2203,6 +2249,7 @@ class DateFieldSecondSegmentState {
onkeydown: this.#onkeydown,
onfocusout: this.#onfocusout,
onclick: this.#root.handleSegmentClick,
"data-placeholder": isEmpty ? "true" : undefined,
...this.#root.getBaseSegmentAttrs("second", this.#id.current),
};
});
Expand Down Expand Up @@ -2253,7 +2300,7 @@ class DateFieldDayPeriodSegmentState {
return;
}

if (isBackspace(e.key)) {
if (isBackspace(e.key) || isDelete(e.key)) {
this.#root.states.dayPeriod.hasLeftFocus = false;
this.#updateSegment("dayPeriod", () => {
const next = "AM";
Expand Down Expand Up @@ -2395,6 +2442,10 @@ function isBackspace(key: string) {
return key === kbd.BACKSPACE;
}

function isDelete(key: string) {
return key === kbd.DELETE;
}

const [setDateFieldRootContext, getDateFieldRootContext] =
createContext<DateFieldRootState>("DateField.Root");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ export function isAcceptableSegmentKey(key: string) {
kbd.ARROW_LEFT,
kbd.ARROW_RIGHT,
kbd.BACKSPACE,
kbd.DELETE,
kbd.SPACE,
];
if (acceptableSegmentKeys.includes(key)) return true;
Expand Down
70 changes: 70 additions & 0 deletions packages/tests/src/tests/date-field/date-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,76 @@ describe("date field", () => {
await user.keyboard(kbd.ARROW_UP);
expect(input).toHaveValue(value.add({ years: 1 }).toString());
});

it("should clear date segments when fully selected with Backspace or Delete", async () => {
const { user, day, month, year, getByTestId } = setup({
value: new CalendarDateTime(2023, 10, 12, 12, 30, 30, 0),
granularity: "second",
});

const { getHour, getMinute, getSecond } = getTimeSegments(getByTestId);
const segments = [month, day, year, getHour(), getMinute(), getSecond()];

// Test Backspace
for (const segment of segments.slice(0, 3)) {
await user.click(segment);
await user.keyboard("{Control>}a{/Control}");
await user.keyboard("{Backspace}");
}

// Test Delete
for (const segment of segments.slice(3)) {
await user.click(segment);
await user.keyboard("{Control>}a{/Control}");
await user.keyboard("{Delete}");
}

// Verify all segments are cleared
expect(day).toHaveTextContent("dd");
expect(month).toHaveTextContent("mm");
expect(year).toHaveTextContent("yyyy");
expect(getHour()).toHaveTextContent(TIME_PLACEHOLDER);
expect(getMinute()).toHaveTextContent(TIME_PLACEHOLDER);
expect(getSecond()).toHaveTextContent(TIME_PLACEHOLDER);
});

it("should apply data-placeholder attribute to empty segments", async () => {
const { user, day, year, month, getByTestId } = setup({
value: undefined,
granularity: "second",
});

const { getHour, getMinute, getSecond } = getTimeSegments(getByTestId);
const segments = [month, day, year, getHour(), getMinute(), getSecond()];

// Check initial state
segments.forEach((segment) => {
expect(segment).toHaveAttribute("data-placeholder", "true");
});

// Fill in date and time
const fillSequence = ["12", "15", "2023", "12", "30", "30"];
for (let i = 0; i < fillSequence.length; i++) {
await user.click(segments[i]);
await user.keyboard(fillSequence[i]);
expect(segments[i]).not.toHaveAttribute("data-placeholder");
}

// Clear and check each segment
for (const segment of segments) {
await user.click(segment);
await user.keyboard("{Control>}a{/Control}{Backspace}");
expect(segment).toHaveAttribute("data-placeholder", "true");
}

// Test partial fill
await user.click(year);
await user.keyboard("20");
expect(year).not.toHaveAttribute("data-placeholder");
await user.keyboard("{Control>}a{/Control}");
await user.keyboard("{Backspace}");
expect(year).toHaveAttribute("data-placeholder", "true");
});
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{:else}
<DateField.Segment
{part}
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 aria-[valuetext=Empty]:text-muted-foreground data-[invalid]:text-destructive"
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 data-[invalid]:text-destructive data-[placeholder]:text-muted-foreground"
>
{value}
</DateField.Segment>
Expand Down
2 changes: 1 addition & 1 deletion sites/docs/src/lib/components/demos/date-field-demo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{:else}
<DateField.Segment
{part}
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 aria-[valuetext=Empty]:text-muted-foreground data-[invalid]:text-destructive"
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 data-[invalid]:text-destructive data-[placeholder]:text-muted-foreground"
>
{value}
</DateField.Segment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{:else}
<DatePicker.Segment
{part}
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 aria-[valuetext=Empty]:text-muted-foreground"
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 data-[placeholder]:text-muted-foreground"
>
{value}
</DatePicker.Segment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{:else}
<DateRangeField.Segment
{part}
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 aria-[valuetext=Empty]:text-muted-foreground"
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 data-[placeholder]:text-muted-foreground"
>
{value}
</DateRangeField.Segment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{:else}
<DateRangePicker.Segment
{part}
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 aria-[valuetext=Empty]:text-muted-foreground"
class="rounded-5px px-1 py-1 hover:bg-muted focus:bg-muted focus:text-foreground focus-visible:!ring-0 focus-visible:!ring-offset-0 data-[placeholder]:text-muted-foreground"
>
{value}
</DateRangePicker.Segment>
Expand Down