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

LG-3896, LG-3898: Date Picker input selection #2186

Merged
merged 36 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
716f099
wip
shaneeza Jan 25, 2024
d7ebcfa
wip - on click
shaneeza Jan 25, 2024
2e33e15
fix to only delete if its a number
shaneeza Jan 25, 2024
bff84a5
comment
shaneeza Jan 26, 2024
707524d
switch to HTMLInputElement
shaneeza Jan 26, 2024
e6b97d6
change back to HTMLElement
shaneeza Jan 26, 2024
f77b235
wip updating tests
shaneeza Jan 30, 2024
015bd08
wip, tests
shaneeza Jan 30, 2024
cba99bd
wip - testing onChange
shaneeza Jan 30, 2024
8305c83
dont propagate up when the backspace clears a value
shaneeza Jan 30, 2024
77587a4
a little clean up, needs more
shaneeza Jan 30, 2024
1441531
some more cleanup
shaneeza Jan 30, 2024
620c1b7
remove comments
shaneeza Jan 30, 2024
9f68176
comment
shaneeza Jan 30, 2024
b4b8ec8
add changeset
shaneeza Jan 30, 2024
8b3f4d8
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-3896-…
shaneeza Jan 30, 2024
4824a8b
wip - addressing comments
shaneeza Feb 2, 2024
cbc1aaf
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-3896-…
shaneeza Feb 2, 2024
374bbe2
more feedback
shaneeza Feb 2, 2024
6962167
remove only
shaneeza Feb 2, 2024
6e03eb6
wip
shaneeza Feb 6, 2024
d5c6635
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-3896-…
shaneeza Feb 14, 2024
29358b5
revert Typo change
shaneeza Feb 14, 2024
626d53f
some feedback
shaneeza Feb 14, 2024
8963d76
wip
shaneeza Feb 15, 2024
c383103
remove comments
shaneeza Feb 15, 2024
9c7858e
fix failing tests, still wip
shaneeza Feb 15, 2024
5c8512b
update test
shaneeza Feb 15, 2024
9c6e552
update tests
shaneeza Feb 16, 2024
2f491c5
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-3896-…
shaneeza Feb 16, 2024
38945f4
update changeset
shaneeza Feb 16, 2024
cf2c541
changeset updates
shaneeza Feb 16, 2024
0a3ed89
more changeset changes
shaneeza Feb 16, 2024
4619910
comments
shaneeza Feb 16, 2024
0824bfc
clean up tests
shaneeza Feb 16, 2024
38f15d8
feedback
shaneeza Feb 20, 2024
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
5 changes: 5 additions & 0 deletions .changeset/eight-beds-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/date-picker': minor
---

Changes the behavior of segments. Add more details.
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion packages/date-picker/src/DatePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const LiveExample: StoryFn<typeof DatePicker> = props => {
onDateChange={v => {
// eslint-disable-next-line no-console
console.log('Storybook: onDateChange', {
value: v!.toUTCString(),
value: v && v.toUTCString(),
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
'value with local browser timezone': v,
});
setValue(v);
Expand All @@ -123,6 +123,10 @@ export const LiveExample: StoryFn<typeof DatePicker> = props => {
'date with local browser timezone': date,
})
}
onChange={e =>
// eslint-disable-next-line no-console
console.log('Storybook: onChange🚨', { value: e.target.value })
}
/>
<br />
<Overline>Current value</Overline>
Expand Down
83 changes: 27 additions & 56 deletions packages/date-picker/src/DatePicker/DatePicker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1423,16 +1423,21 @@ describe('packages/date-picker', () => {
test('deletes any value in the input', () => {
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
const { dayInput } = renderDatePicker();
userEvent.type(dayInput, '26{backspace}');
expect(dayInput.value).toBe('2');
userEvent.tab();
expect(dayInput.value).toBe('02');
expect(dayInput.value).toBe('');
});

test('deletes the whole value on multiple presses', () => {
test('keeps the focus in the current input', () => {
const { monthInput } = renderDatePicker();
userEvent.type(monthInput, '11');
userEvent.type(monthInput, '{backspace}');
expect(monthInput).toHaveFocus();
});

test('focuses the previous segment after pressing backspace twice', () => {
const { monthInput, yearInput } = renderDatePicker();
userEvent.type(monthInput, '11');
userEvent.type(monthInput, '{backspace}{backspace}');
expect(monthInput.value).toBe('');
expect(yearInput).toHaveFocus();
});

test('focuses the previous segment if current segment is empty', () => {
Expand All @@ -1455,39 +1460,12 @@ describe('packages/date-picker', () => {
expect(yearInput).toHaveFocus();
});

test('moves the cursor when the segment has a value', () => {
const { monthInput } = renderDatePicker({
value: testToday,
});
userEvent.click(monthInput);
userEvent.keyboard('{arrowleft}');
expect(monthInput).toHaveFocus();
});

test('moves the cursor when the value starts with 0', () => {
const { monthInput } = renderDatePicker({});
userEvent.type(monthInput, '04{arrowleft}{arrowleft}');
expect(monthInput).toHaveFocus();
});

test('moves the cursor when the value is 0', () => {
const { monthInput } = renderDatePicker({});
userEvent.type(monthInput, '0{arrowleft}');
expect(monthInput).toHaveFocus();
});

test('moves the cursor to the previous segment when the value is 0', () => {
const { yearInput, monthInput } = renderDatePicker({});
userEvent.type(monthInput, '0{arrowleft}{arrowleft}');
expect(yearInput).toHaveFocus();
});

test('focuses the previous segment if the cursor is at the start of the input text', () => {
test('focuses the previous segment when the segment has a value', () => {
const { yearInput, monthInput } = renderDatePicker({
value: testToday,
});
userEvent.click(monthInput);
userEvent.keyboard('{arrowleft}{arrowleft}{arrowleft}');
userEvent.keyboard('{arrowleft}');
expect(yearInput).toHaveFocus();
});
});
Expand All @@ -1500,7 +1478,7 @@ describe('packages/date-picker', () => {
expect(monthInput).toHaveFocus();
});

test('focuses the next segment if the cursor is at the start of the input text', () => {
test('focuses the next segment when the segment has a value', () => {
const { yearInput, monthInput } = renderDatePicker({
value: testToday,
});
Expand All @@ -1509,13 +1487,10 @@ describe('packages/date-picker', () => {
expect(monthInput).toHaveFocus();
});

test('moves the cursor when the segment has a value', () => {
const { yearInput } = renderDatePicker({
value: testToday,
});
userEvent.click(yearInput);
userEvent.keyboard('{arrowleft}{arrowright}');
expect(yearInput).toHaveFocus();
test('focuses the next segment when the value starts with 0', () => {
const { monthInput, dayInput } = renderDatePicker({});
userEvent.type(monthInput, '0{arrowright}');
expect(dayInput).toHaveFocus();
});
});

Expand Down Expand Up @@ -3029,7 +3004,6 @@ describe('packages/date-picker', () => {
userEvent.type(monthInput, '7');
userEvent.type(dayInput, '4');

yearInput.setSelectionRange(0, 4);
userEvent.type(yearInput, '{backspace}');
userEvent.type(yearInput, '2');
expect(yearInput).toHaveValue('2');
Expand All @@ -3041,27 +3015,26 @@ describe('packages/date-picker', () => {
userEvent.type(monthInput, '7');
userEvent.type(dayInput, '4');

userEvent.type(yearInput, '{backspace}{backspace}');
expect(yearInput).toHaveValue('20');
userEvent.type(yearInput, '{backspace}');
expect(yearInput).toHaveValue('');
});
});

describe('typing new characters', () => {
test('even if the resulting value is valid, keeps the input as-is', async () => {
test('updates the value', async () => {
const { monthInput } = renderDatePicker({});
userEvent.type(monthInput, '1');
userEvent.tab();
await waitFor(() => expect(monthInput).toHaveValue('01'));
userEvent.type(monthInput, '2');
await waitFor(() => expect(monthInput).toHaveValue('01'));
await waitFor(() => expect(monthInput).toHaveValue('02'));
});

test('if the resulting value is not valid, keeps the input as-is', async () => {
test('if the resulting value is not valid, clears the input', async () => {
const { monthInput } = renderDatePicker({});
userEvent.type(monthInput, '6');
await waitFor(() => expect(monthInput).toHaveValue('06'));
userEvent.type(monthInput, '9');
await waitFor(() => expect(monthInput).toHaveValue('06'));
userEvent.type(monthInput, '0');
userEvent.tab();
await waitFor(() => expect(monthInput).toHaveValue(''));
TheSonOfThomp marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
Expand Down Expand Up @@ -3551,15 +3524,13 @@ describe('packages/date-picker', () => {
userEvent.tab();
errorElement = queryByTestId('lg-form_field-error_message');
expect(errorElement).toHaveTextContent(
'2020-02-30 is not a valid date',
'2020-02- is not a valid date',
);

userEvent.type(dayInput, '{backspace}{backspace}');
userEvent.tab();
errorElement = queryByTestId('lg-form_field-error_message');
expect(errorElement).toHaveTextContent(
'2020-02- is not a valid date',
);
expect(errorElement).toHaveTextContent('2020-- is not a valid date');
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
});

test('Clearing the input after an invalid date error message is displayed removes the message', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const DatePickerInput = forwardRef<HTMLDivElement, DatePickerInputProps>(
});

segmentToFocus?.focus();
segmentToFocus?.select();
shaneeza marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand All @@ -108,40 +109,38 @@ export const DatePickerInput = forwardRef<HTMLDivElement, DatePickerInputProps>(

const isSegmentEmpty = !target.value;

const { selectionStart, selectionEnd } = target;

switch (key) {
case keyMap.ArrowLeft: {
// Without this, the input does not select all the text
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still a bit unclear on how preventing default keydown behavior prevents selection

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure how to explain this -- without preventDefault, the input ignores .select(), and I'm unsure why.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, If you have time, this would be an interesting thing to investigate why

e.preventDefault();
// if input is empty,
// or the cursor is at the beginning of the input
// set focus to prev. input (if it exists)
if (selectionStart === 0) {
const segmentToFocus = getRelativeSegmentRef('prev', {
segment: target,
formatParts,
segmentRefs,
});

segmentToFocus?.current?.focus();
}
const segmentToFocus = getRelativeSegmentRef('prev', {
segment: target,
formatParts,
segmentRefs,
});

segmentToFocus?.current?.focus();
segmentToFocus?.current?.select();
// otherwise, use default behavior

break;
}

case keyMap.ArrowRight: {
// Without this, the input does not select all the text
e.preventDefault();
// if input is empty,
// or the cursor is at the end of the input
// set focus to next. input (if it exists)
if (selectionEnd === target.value.length) {
const segmentToFocus = getRelativeSegmentRef('next', {
segment: target,
formatParts,
segmentRefs,
});

segmentToFocus?.current?.focus();
}
const segmentToFocus = getRelativeSegmentRef('next', {
segment: target,
formatParts,
segmentRefs,
});

segmentToFocus?.current?.focus();
segmentToFocus?.current?.select();
// otherwise, use default behavior

break;
Expand All @@ -157,12 +156,14 @@ export const DatePickerInput = forwardRef<HTMLDivElement, DatePickerInputProps>(
if (isSegmentEmpty) {
// prevent the backspace in the previous segment
e.preventDefault();

const segmentToFocus = getRelativeSegmentRef('prev', {
segment: target,
formatParts,
segmentRefs,
});
segmentToFocus?.current?.focus();
segmentToFocus?.current?.select();
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getSegmentToFocus = ({
target,
formatParts,
segmentRefs,
}: GetSegmentToFocusProps): HTMLElement | undefined | null => {
}: GetSegmentToFocusProps): HTMLInputElement | undefined | null => {
if (
isUndefined(target) ||
isUndefined(formatParts) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,18 @@ describe('packages/date-picker/shared/date-input-box', () => {
expect(dayInput.value).toBe('02');
});

test('backspace deletes characters', () => {
test('backspace resets the input', () => {
const { dayInput, yearInput } = renderDateInputBox(
{ value: null },
testContext,
);
userEvent.type(dayInput, '21');
userEvent.type(dayInput, '{backspace}');
expect(dayInput.value).toBe('2');
expect(dayInput.value).toBe('');

userEvent.type(yearInput, '1993');
userEvent.type(yearInput, '{backspace}');
expect(yearInput.value).toBe('199');
expect(yearInput.value).toBe('');
});

test('segment change handler is called when typing into a segment', () => {
Expand Down Expand Up @@ -255,17 +255,19 @@ describe('packages/date-picker/shared/date-input-box', () => {
userEvent.type(dayInput, '21');
userEvent.type(dayInput, '{backspace}');
expect(onSegmentChange).toHaveBeenCalledWith(
expect.objectContaining({ value: '2' }),
expect.objectContaining({ value: '' }),
);
});

test('value setter is not called when deleting from a single segment', () => {
test('value setter is called when pressing backspace in a single segment', () => {
const setValue = jest.fn();

const { dayInput } = renderDateInputBox({ setValue }, testContext);
userEvent.type(dayInput, '21');
userEvent.type(dayInput, '{backspace}');
expect(setValue).not.toHaveBeenCalled();
expect(setValue).toHaveBeenCalledWith(
expect.objectContaining({ value: null }),
);
});
});

Expand Down Expand Up @@ -313,11 +315,11 @@ describe('packages/date-picker/shared/date-input-box', () => {
},
testContext,
);
userEvent.type(dayInput, '{backspace}5');
userEvent.type(dayInput, '{backspace}');
expect(setValue).toHaveBeenCalledWith(
expect.objectContaining(newUTC(1993, Month.December, 25)),
expect.objectContaining(new Date('invalid')),
);
expect(dayInput).toHaveValue('25');
expect(dayInput).toHaveValue('');
});

test('value setter is _not_ called when new input is ambiguous', () => {
Expand All @@ -330,8 +332,8 @@ describe('packages/date-picker/shared/date-input-box', () => {
testContext,
);
userEvent.type(dayInput, '{backspace}');
expect(setValue).not.toHaveBeenCalled();
expect(dayInput).toHaveValue('2');
expect(setValue).toHaveBeenCalled();
expect(dayInput).toHaveValue('');
});

test('value setter is called when the input is cleared', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const DateInputBox = React.forwardRef<HTMLDivElement, DateInputBoxProps>(
if (nextSegmentName) {
const nextSegmentRef = segmentRefs[nextSegmentName];
nextSegmentRef?.current?.focus();
nextSegmentRef?.current?.select();
}
}

Expand Down
Loading
Loading