/
ArrayCountWidget.jsx
78 lines (66 loc) · 2.1 KB
/
ArrayCountWidget.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React from 'react';
export default class ArrayCountWidget extends React.Component {
constructor(props) {
super(props);
this.state = { userCount: props.value
? props.value.length - (this.props.options.countOffset || 0)
: undefined };
}
componentDidUpdate(prevProps, prevState) {
if (prevState.userCount !== this.state.userCount) {
let count = parseInt(this.state.userCount, 10);
if (isNaN(count)) {
count = 0;
}
// Too high of a count can crash the browser. We’ve been using
// this for marriage counts and 29 is the record, so 29 seems like
// a good upper limit. Filling out more than 29 pages is probably
// not reasonable in a form anyway
if (count > 29) {
count = 29;
}
this.props.onChange(this.getValue(count, this.props.value));
}
}
// We’re expanding or contracting the array based on the count
// and returning undefined if the array should be empty
getValue = (count, value = []) => {
if (count === 0) {
return undefined;
}
const intCount = count + (this.props.options.countOffset || 0);
if (intCount < 0) {
return undefined;
}
if (intCount < value.length) {
return value.slice(0, intCount);
}
return Array(intCount - value.length).fill({}).concat(value);
}
updateArrayLength = (event) => {
this.setState({ userCount: event.target.value });
}
render() {
const props = this.props;
if (props.formContext.reviewMode) {
return (
<div className="review-row">
<dt>{props.uiSchema['ui:title']}</dt><dd>{this.state.userCount}</dd>
</div>
);
}
return (
<input type="number"
step="1"
min="1"
id={props.id}
name={props.id}
disabled={props.disabled}
autoComplete={props.options.autocomplete || false}
className={props.options.widgetClassNames}
value={typeof this.state.userCount === 'undefined' ? '' : this.state.userCount}
onBlur={() => props.onBlur(props.id)}
onChange={this.updateArrayLength}/>
);
}
}