Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
38 changes: 34 additions & 4 deletions src/PickerInput/Selector/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
// ========================= Refs =========================
const holderRef = React.useRef<HTMLDivElement>(null);
const inputRef = React.useRef<HTMLInputElement>(null);
// When mousedown get focus, defer selection to mouseUp so click position is used
const mouseDownRef = React.useRef(false);
Comment thread
claytonlin1110 marked this conversation as resolved.

React.useImperativeHandle(ref, () => ({
nativeElement: holderRef.current,
Expand Down Expand Up @@ -153,6 +155,12 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
};

const onFormatPaste: React.ClipboardEventHandler<HTMLInputElement> = (event) => {
// Block paste until selection is set (after mouseUp when focus was by mousedown)
if (mouseDownRef.current) {
event.preventDefault();
return;
}

// Get paste text
const pasteText = event.clipboardData.getData('text');

Expand All @@ -164,8 +172,6 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
// ======================== Mouse =========================
// When `mouseDown` get focus, it's better to not to change the selection
// Since the up position maybe not is the first cell
const mouseDownRef = React.useRef(false);

const onFormatMouseDown: React.MouseEventHandler<HTMLInputElement> = () => {
mouseDownRef.current = true;
};
Expand Down Expand Up @@ -225,6 +231,25 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {

const { key } = event;

// Block mask editing until selection is set (after mouseUp when focus was by mousedown).
// Still allow shared handlers (onKeyDown, Enter submit) above to run.
if (mouseDownRef.current) {
Comment thread
claytonlin1110 marked this conversation as resolved.
const shouldBlock =
key.length === 1 ||
key === 'Backspace' ||
key === 'Delete' ||
key === 'ArrowLeft' ||
key === 'ArrowRight' ||
key === 'ArrowUp' ||
key === 'ArrowDown';

if (shouldBlock) {
event.preventDefault();
}

return;
}

// Save the cache with cell text
let nextCellText: string = null;

Expand Down Expand Up @@ -334,16 +359,21 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
const rafRef = React.useRef<number>();

useLayoutEffect(() => {
if (!focused || !format || mouseDownRef.current) {
if (!focused || !format) {
Comment thread
claytonlin1110 marked this conversation as resolved.
Outdated
return;
}

// Reset with format if not match
// Reset with format if not match (always apply when focused so mask works when focusing by mousedown)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这里和下面没有还原呀…………
你在 github 的 diff view 可以看的:

Image

另外,CI 还是失败的哈~

https://github.com/react-component/picker/actions/runs/22935988370/job/66567899243?pr=959

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just updated, please re-run CI

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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();
});
Comment thread
claytonlin1110 marked this conversation as resolved.

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

Expand Down