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: caret position when last string exists #281

Merged
merged 1 commit into from
Dec 17, 2020
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
2 changes: 1 addition & 1 deletion src/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class InputNumber extends React.Component<Partial<InputNumberProps>, InputNumber
) {
// If not match any of then, let's just keep the position
// TODO: Logic should not reach here, need check if happens
let pos = this.cursorStart + 1;
let pos = this.getInputDisplayValue(this.state).length;

// If not have last string, just position to the end
if (!this.cursorAfter) {
Expand Down
57 changes: 57 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,63 @@ describe('InputNumber', () => {
});
});

// https://github.com/ant-design/ant-design/issues/28366
describe('cursor position when last string exists', () => {
const setUpCursorTest = (initValue, prependValue) => {
class Demo extends React.Component {
state = {
value: initValue,
};

onChange = value => {
this.setState({ value });
};

render() {
return (
<InputNumber
ref="inputNum"
value={this.state.value}
onChange={this.onChange}
formatter={value => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
parser={value => value.replace(/\$\s?|(,*)/g, '')}
/>
);
}
}
example = ReactDOM.render(<Demo />, container);
inputNumber = example.refs.inputNum;
inputNumber.input.selectionStart = 0;
inputNumber.input.selectionEnd = 0;
inputElement = ReactDOM.findDOMNode(inputNumber.input);
Simulate.focus(inputElement);
for (let i = 0; i < prependValue.length; i += 1) {
Simulate.keyDown(inputElement, { keyCode: keyCode.ONE });
}
Simulate.change(inputElement, { target: { value: prependValue + initValue } });
};
it('shold fix caret position on case 1', () => {
// '$ 1'
setUpCursorTest('', '1');
expect(inputNumber.input.selectionStart).to.be(3);
});
it('shold fix caret position on case 2', () => {
// '$ 111'
setUpCursorTest('', '111');
expect(inputNumber.input.selectionStart).to.be(5);
});
it('shold fix caret position on case 3', () => {
// '$ 111'
setUpCursorTest('1', '11');
expect(inputNumber.input.selectionStart).to.be(4);
});
it('shold fix caret position on case 4', () => {
// '$ 123,456'
setUpCursorTest('456', '123');
expect(inputNumber.input.selectionStart).to.be(6);
});
});

describe(`required prop`, () => {
it(`should add required attr to the input tag when get passed as true`, () => {
ReactDOM.render(<InputNumber id="required-input-test" required />, container);
Expand Down