Skip to content

Commit 1388d6a

Browse files
authored
fix: caret position (#281)
1 parent 5d29b67 commit 1388d6a

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/InputNumber.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class InputNumber extends React.Component<Partial<InputNumberProps>, InputNumber
170170
) {
171171
// If not match any of then, let's just keep the position
172172
// TODO: Logic should not reach here, need check if happens
173-
let pos = this.cursorStart + 1;
173+
let pos = this.getInputDisplayValue(this.state).length;
174174

175175
// If not have last string, just position to the end
176176
if (!this.cursorAfter) {

tests/index.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,63 @@ describe('InputNumber', () => {
15631563
});
15641564
});
15651565

1566+
// https://github.com/ant-design/ant-design/issues/28366
1567+
describe('cursor position when last string exists', () => {
1568+
const setUpCursorTest = (initValue, prependValue) => {
1569+
class Demo extends React.Component {
1570+
state = {
1571+
value: initValue,
1572+
};
1573+
1574+
onChange = value => {
1575+
this.setState({ value });
1576+
};
1577+
1578+
render() {
1579+
return (
1580+
<InputNumber
1581+
ref="inputNum"
1582+
value={this.state.value}
1583+
onChange={this.onChange}
1584+
formatter={value => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
1585+
parser={value => value.replace(/\$\s?|(,*)/g, '')}
1586+
/>
1587+
);
1588+
}
1589+
}
1590+
example = ReactDOM.render(<Demo />, container);
1591+
inputNumber = example.refs.inputNum;
1592+
inputNumber.input.selectionStart = 0;
1593+
inputNumber.input.selectionEnd = 0;
1594+
inputElement = ReactDOM.findDOMNode(inputNumber.input);
1595+
Simulate.focus(inputElement);
1596+
for (let i = 0; i < prependValue.length; i += 1) {
1597+
Simulate.keyDown(inputElement, { keyCode: keyCode.ONE });
1598+
}
1599+
Simulate.change(inputElement, { target: { value: prependValue + initValue } });
1600+
};
1601+
it('shold fix caret position on case 1', () => {
1602+
// '$ 1'
1603+
setUpCursorTest('', '1');
1604+
expect(inputNumber.input.selectionStart).to.be(3);
1605+
});
1606+
it('shold fix caret position on case 2', () => {
1607+
// '$ 111'
1608+
setUpCursorTest('', '111');
1609+
expect(inputNumber.input.selectionStart).to.be(5);
1610+
});
1611+
it('shold fix caret position on case 3', () => {
1612+
// '$ 111'
1613+
setUpCursorTest('1', '11');
1614+
expect(inputNumber.input.selectionStart).to.be(4);
1615+
});
1616+
it('shold fix caret position on case 4', () => {
1617+
// '$ 123,456'
1618+
setUpCursorTest('456', '123');
1619+
expect(inputNumber.input.selectionStart).to.be(6);
1620+
});
1621+
});
1622+
15661623
describe(`required prop`, () => {
15671624
it(`should add required attr to the input tag when get passed as true`, () => {
15681625
ReactDOM.render(<InputNumber id="required-input-test" required />, container);

0 commit comments

Comments
 (0)