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

fix: Selection not keep in sync #52

Merged
merged 4 commits into from
Sep 28, 2023
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
18 changes: 18 additions & 0 deletions src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
const formatValue =
value === undefined || value === null ? '' : String(value);

// =================== Select Range ===================
const [selection, setSelection] = React.useState<
[start: number, end: number] | null
>(null);

// ====================== Count =======================
const countConfig = useCount(count, showCount);
const mergedMax = countConfig.max || maxLength;
Expand Down Expand Up @@ -104,6 +109,13 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
cutValue = countConfig.exceedFormatter(currentValue, {
max: countConfig.max,
});

if (currentValue !== cutValue) {
setSelection([
inputRef.current?.selectionStart || 0,
inputRef.current?.selectionEnd || 0,
]);
}
}
setValue(cutValue);

Expand All @@ -112,6 +124,12 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
}
};

React.useEffect(() => {
if (selection) {
inputRef.current?.setSelectionRange(...selection);
}
}, [selection]);

const onInternalChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
triggerChange(e, e.target.value);
};
Expand Down
26 changes: 26 additions & 0 deletions tests/count.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ describe('Input.Count', () => {
expect(onCompositionEnd).toHaveBeenCalled();
});

it('exceedFormatter selection', () => {
const { container } = render(
<Input
count={{
show: true,
max: 3,
exceedFormatter: (val, { max }) => val.slice(0, max),
}}
defaultValue={'123'}
/>,
);

const input = container.querySelector('input')!;
const setSelectionRange = jest.spyOn(input, 'setSelectionRange');

fireEvent.change(input, {
target: {
selectionStart: 2,
selectionEnd: 2,
value: '1a23',
},
});

expect(setSelectionRange).toHaveBeenCalledWith(2, 2);
});

describe('cls', () => {
it('raw', () => {
const { container } = render(
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "ESNext",
"moduleResolution": "node",
"baseUrl": "./",
"lib": ["dom", "es2017", "es2022"],
"lib": ["dom", "es2017", "es2022", "esnext"],
"jsx": "react",
"strict": true,
"esModuleInterop": true,
Expand Down
Loading