-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberAndDateRangeInputs.tsx
executable file
·296 lines (283 loc) · 9.29 KB
/
NumberAndDateRangeInputs.tsx
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import React, { useState, useEffect, useMemo } from 'react';
import { Typography } from '@material-ui/core';
import { DARKEST_GRAY, MEDIUM_GRAY } from '../../constants/colors';
import { NumberInput, DateInput } from './NumberAndDateInputs';
import Button from './Button';
import Notification from './Notification';
import { NumberRange, DateRange, NumberOrDateRange } from '../../types/general';
import { propTypes } from 'react-bootstrap/esm/Image';
export type BaseProps<M extends NumberOrDateRange> = {
/** Externally controlled range. */
range?: M;
/** If true, warn about empty lower or upper values. Default is false */
required?: boolean;
/** Function to invoke when range changes. */
onRangeChange: (newRange?: NumberOrDateRange) => void;
/** When true, allow undefined min or max. Default is true
* When false, and rangeBounds is given, use the min or max of rangeBounds to fill in the missing value.
* */
allowPartialRange?: boolean;
/** Minimum and maximum allowed values for the user-inputted range. Optional. */
rangeBounds?: M;
/** Optional validator function. Should return {validity: true, message: ''} if value is allowed.
* If a validator is provided, `required` is no longer useful, and
* rangeBounds will only be used for auto-filling empty inputs.
*/
validator?: (newRange?: NumberOrDateRange) => {
validity: boolean;
message: string;
};
/** UI Label for the widget. Optional */
label?: string;
/** Label for lower bound widget. Optional. Default is Min */
lowerLabel?: string;
/** Label for upper bound widget. Optional. Default is Max */
upperLabel?: string;
/** Additional styles for component container. Optional. */
containerStyles?: React.CSSProperties;
/** Show cancel/clear button */
showClearButton?: boolean;
/** Text to adorn the clear button; Default is 'Clear' */
clearButtonLabel?: string;
/** add disabled prop to disable input fields */
disabled?: boolean;
/** specify the height of the input element */
inputHeight?: number;
};
export type NumberRangeInputProps = BaseProps<NumberRange> & { step?: number };
export function NumberRangeInput(props: NumberRangeInputProps) {
return <BaseInput {...props} valueType="number" />;
}
export type DateRangeInputProps = BaseProps<DateRange>;
export function DateRangeInput(props: DateRangeInputProps) {
return <BaseInput {...props} valueType="date" />;
}
type BaseInputProps =
| (NumberRangeInputProps & {
valueType: 'number';
})
| (DateRangeInputProps & {
valueType: 'date'; // another possibility is 'datetime-local', but the Material UI TextField doesn't provide a date picker
});
/**
* Paired input fields taking values we can do < > <= => comparisons with
* i.e. number or date.
* Not currently exported. But could be if needed.
*/
function BaseInput({
range,
required = false,
rangeBounds,
validator,
onRangeChange,
allowPartialRange = true,
label,
lowerLabel = '',
upperLabel = '',
valueType,
containerStyles,
showClearButton = false,
clearButtonLabel = 'Clear',
// add disabled prop to disable input fields
disabled = false,
inputHeight,
...props
}: BaseInputProps) {
if (validator && required)
console.log(
'WARNING: NumberRangeInput or DateRangeInput will ignore `required` prop because validator was provided.'
);
const [localRange, setLocalRange] =
useState<NumberRange | DateRange | undefined>(range);
const [isReceiving, setIsReceiving] = useState<boolean>(false);
const [validationWarning, setValidationWarning] = useState<string>('');
// handle incoming value changes
useEffect(() => {
setIsReceiving(true);
setLocalRange(range);
}, [range]);
// if we are not currently receiving incoming data
// pass localRange (if it differs from `range`) out to consumer
// respecting `allowPartialRange`
useEffect(() => {
if (!isReceiving) {
if (
localRange &&
(allowPartialRange ||
(localRange.min != null && localRange.max != null))
) {
const { validity, message } = validator
? validator(localRange)
: { validity: true, message: '' };
if (validity) {
// communicate the change if there is a change
if (localRange.min !== range?.min || localRange.max !== range?.max) {
onRangeChange(localRange);
}
setValidationWarning('');
} else {
setValidationWarning(message);
}
} else if (
localRange?.min == null &&
localRange?.max == null &&
range?.min != null &&
range?.max != null
) {
onRangeChange(undefined);
} else if (
// fill in the min or max for a partially entered range
localRange &&
rangeBounds &&
!allowPartialRange
) {
if (localRange.min == null) {
setLocalRange({
min: rangeBounds.min,
max: localRange.max,
} as NumberOrDateRange);
} else if (localRange.max == null) {
setLocalRange({
min: localRange.min,
max: rangeBounds.max,
} as NumberOrDateRange);
}
}
}
}, [
localRange,
range,
isReceiving,
onRangeChange,
allowPartialRange,
rangeBounds,
validator,
]);
const { min, max } = localRange ?? {};
const step = 'step' in props ? props.step : undefined;
return (
<div style={{ ...containerStyles }}>
{label && (
<Typography
variant="button"
style={{ color: disabled ? MEDIUM_GRAY : DARKEST_GRAY }}
>
{label}
</Typography>
)}
<div
style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}
>
{valueType === 'number' ? (
<NumberInput
value={min as number}
minValue={validator ? undefined : (rangeBounds?.min as number)}
maxValue={
validator ? undefined : ((max ?? rangeBounds?.max) as number)
}
label={lowerLabel}
required={validator ? undefined : required}
onValueChange={(newValue) => {
setIsReceiving(false);
setLocalRange({ min: newValue, max } as NumberRange);
}}
// add disabled prop to disable input fields
disabled={disabled}
step={step}
inputHeight={inputHeight}
/>
) : (
<DateInput
value={min as string}
minValue={validator ? undefined : (rangeBounds?.min as string)}
maxValue={
validator ? undefined : ((max ?? rangeBounds?.max) as string)
}
label={lowerLabel}
required={validator ? undefined : required}
onValueChange={(newValue) => {
setIsReceiving(false);
setLocalRange({ min: newValue, max } as DateRange);
}}
// add disabled prop to disable input fields
disabled={disabled}
inputHeight={inputHeight}
/>
)}
<div style={{ display: 'flex', flexDirection: 'row' }}>
{/* change margin */}
<div
style={{
margin: 'auto 10px',
}}
>
<Typography
variant="button"
style={{ color: disabled ? MEDIUM_GRAY : DARKEST_GRAY }}
>
to
</Typography>
</div>
</div>
{valueType === 'number' ? (
<NumberInput
value={max as number}
minValue={
validator ? undefined : ((min ?? rangeBounds?.min) as number)
}
maxValue={validator ? undefined : (rangeBounds?.max as number)}
label={upperLabel}
required={validator ? undefined : required}
onValueChange={(newValue) => {
setIsReceiving(false);
setLocalRange({ min, max: newValue } as NumberRange);
}}
// add disabled prop to disable input fields
disabled={disabled}
step={step}
inputHeight={inputHeight}
/>
) : (
<DateInput
value={max as string}
minValue={
validator ? undefined : ((min ?? rangeBounds?.min) as string)
}
maxValue={validator ? undefined : (rangeBounds?.max as string)}
label={upperLabel}
required={validator ? undefined : required}
onValueChange={(newValue) => {
setIsReceiving(false);
setLocalRange({ min, max: newValue } as DateRange);
}}
// add disabled prop to disable input fields
disabled={disabled}
inputHeight={inputHeight}
/>
)}
{showClearButton && (
<Button
type={'outlined'}
text={clearButtonLabel}
onClick={() => {
setIsReceiving(false);
setLocalRange(undefined);
}}
containerStyles={{
paddingLeft: '20px',
}}
/>
)}
</div>
{validationWarning ? (
<Notification
title="Warning"
text={validationWarning}
onAcknowledgement={() => {
setValidationWarning('');
}}
/>
) : null}
</div>
);
}