Skip to content
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
9 changes: 7 additions & 2 deletions src/PickerInput/Selector/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,21 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
const rafRef = React.useRef<number>();

useLayoutEffect(() => {
if (!focused || !format || mouseDownRef.current) {
if (!focused || !format) {
return;
}

// Reset with format if not match
// Reset with format if not match (always apply when focused so mask works when focusing by mousedown)
if (!maskFormat.match(inputValue)) {
triggerInputChange(format);
return;
}

// When mousedown get focus, defer selection to mouseUp so click position is used
if (mouseDownRef.current) {
return;
}

// Match the selection range
inputRef.current.setSelectionRange(selectionStart, selectionEnd);

Expand Down
13 changes: 13 additions & 0 deletions tests/new-range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,19 @@ describe('NewPicker.Range', () => {
expect(startInput.selectionEnd).toEqual(6);
});

it('focus by mousedown defers selection sync to mouseUp', () => {
const { container } = render(<Demo />);

const startInput = container.querySelectorAll<HTMLInputElement>('input')[0];

fireEvent.mouseDown(startInput);
fireEvent.focus(startInput);

fireEvent.mouseUp(startInput);
expect(startInput.selectionStart).toBeDefined();
expect(startInput.selectionEnd).toBeDefined();
});

it('blur to reset back text', async () => {
const { container } = render(<Demo />);

Expand Down