Skip to content

Commit

Permalink
should trigger onChange when min or max changed
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Sep 8, 2018
1 parent a2383c2 commit ffdd76e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-input-number",
"version": "4.0.12",
"version": "4.0.13",
"description": "React input-number component",
"keywords": [
"react",
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ export default class InputNumber extends React.Component {
inputValue: this.inputting ? value : this.toPrecisionAsStep(value),
});
}

// Trigger onChange when max or min change
// https://github.com/ant-design/ant-design/issues/11574
const nextValue = 'value' in nextProps ? nextProps.value : this.state.value;
const { onChange, max, min } = this.props;
if ('max' in nextProps && nextProps.max !== max && nextValue > nextProps.max && onChange) {
onChange(nextProps.max);
}
if ('min' in nextProps && nextProps.min !== min && nextValue < nextProps.min && onChange) {
onChange(nextProps.min);
}
}

componentDidUpdate() {
Expand Down
44 changes: 44 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,50 @@ describe('inputNumber', () => {
expect(onBlurCallCount).to.be(1);
expect(ReactDOM.findDOMNode(inputNumber).className.indexOf('focused') > 0).to.be(false);
});

// https://github.com/ant-design/ant-design/issues/11574
it('should trigger onChange when max or min change', () => {
const onChange = sinon.spy();
class Demo extends Component {
state = {
value: 10,
min: 0,
max: 20,
}
setMax(max) {
this.setState({ max });
}
setMin(min) {
this.setState({ min });
}
setValue(value) {
this.setState({ value });
}
render() {
return (
<InputNumber
ref="inputNum"
value={this.state.value}
onChange={onChange}
max={this.state.max}
min={this.state.min}
/>
);
}
}
example = ReactDOM.render(<Demo />, container);
inputNumber = example.refs.inputNum;
inputElement = ReactDOM.findDOMNode(inputNumber.input);
example.setMin(11);
expect(inputElement.value).to.be('11');
expect(onChange.calledWith(11)).to.be(true);

example.setValue(15);

example.setMax(14);
expect(inputElement.value).to.be('14');
expect(onChange.calledWith(14)).to.be(true);
});
});

describe(`required prop`, () => {
Expand Down

0 comments on commit ffdd76e

Please sign in to comment.