aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/fields/extensions/NumericField.js
diff options
context:
space:
mode:
authorEmiliano Ciavatta2020-09-29 16:56:00 +0000
committerEmiliano Ciavatta2020-09-29 16:56:00 +0000
commitd994a21a0dfae9ee026e8aa3ccdee6c213c523aa (patch)
treea9d343300d3b9587bdaa664ef9f005ddc9529656 /frontend/src/components/fields/extensions/NumericField.js
parent7f4cc5d3f3f92338a464853c182b9d6a3ea850eb (diff)
Complete rules page
Diffstat (limited to 'frontend/src/components/fields/extensions/NumericField.js')
-rw-r--r--frontend/src/components/fields/extensions/NumericField.js40
1 files changed, 23 insertions, 17 deletions
diff --git a/frontend/src/components/fields/extensions/NumericField.js b/frontend/src/components/fields/extensions/NumericField.js
index 8823c42..ed81ed7 100644
--- a/frontend/src/components/fields/extensions/NumericField.js
+++ b/frontend/src/components/fields/extensions/NumericField.js
@@ -11,25 +11,31 @@ class NumericField extends Component {
};
}
- render() {
- const handler = (value) => {
- value = value.replace(/[^\d]/gi, '');
- let intValue = 0;
- if (value !== "") {
- intValue = parseInt(value);
- }
- const valid =
- (!this.props.validate || (typeof this.props.validate === "function" && this.props.validate(intValue))) &&
- (!this.props.min || (typeof this.props.min === "number" && intValue >= this.props.min)) &&
- (!this.props.max || (typeof this.props.max === "number" && intValue <= this.props.max));
- this.setState({invalid: !valid});
- if (this.props.onChange) {
- this.props.onChange(intValue);
- }
- };
+ componentDidUpdate(prevProps, prevState, snapshot) {
+ if (prevProps.value !== this.props.value) {
+ this.onChange(this.props.value);
+ }
+ }
+ onChange = (value) => {
+ value = value.toString().replace(/[^\d]/gi, '');
+ let intValue = 0;
+ if (value !== "") {
+ intValue = parseInt(value);
+ }
+ const valid =
+ (!this.props.validate || (typeof this.props.validate === "function" && this.props.validate(intValue))) &&
+ (!this.props.min || (typeof this.props.min === "number" && intValue >= this.props.min)) &&
+ (!this.props.max || (typeof this.props.max === "number" && intValue <= this.props.max));
+ this.setState({invalid: !valid});
+ if (typeof this.props.onChange === "function") {
+ this.props.onChange(intValue);
+ }
+ };
+
+ render() {
return (
- <InputField {...this.props} onChange={handler} initialValue={this.props.initialValue || 0}
+ <InputField {...this.props} onChange={this.onChange} defaultValue={this.props.defaultValue || "0"}
invalid={this.state.invalid} />
);
}