This repository has been archived by the owner on May 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
DatNumber.js
167 lines (147 loc) · 3.76 KB
/
DatNumber.js
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
/* eslint-disable no-param-reassign */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import isFinite from 'lodash.isfinite';
import isString from 'lodash.isstring';
import result from 'lodash.result';
import cx from 'classnames';
import Slider from './Slider';
import { isInteger, toNumber } from './utils';
const applyConstraints = ({ value, min, max, step }) => {
const [hasMin, hasMax, hasStep] = [
isFinite(min),
isFinite(max),
isFinite(step)
];
const decimalPlaces =
hasStep && !isInteger(step) ? step.toString().split('.')[1].length : 0;
let [isMin, isMax] = [false, false];
value = toNumber(value);
if (hasMin && value <= min) {
value = min;
isMin = true;
}
if (hasMax && value >= max) {
value = max;
isMax = true;
}
if (!isMin && !isMax) {
if (hasStep && step !== 0) {
value = Math.round(value / step) * step;
}
}
return value.toFixed(decimalPlaces);
};
export default class DatNumber extends Component {
static propTypes = {
className: PropTypes.string,
style: PropTypes.object,
min: PropTypes.number,
max: PropTypes.number,
step: PropTypes.number,
data: PropTypes.object.isRequired,
path: PropTypes.string,
label: PropTypes.string,
labelWidth: PropTypes.string.isRequired,
_onUpdateValue: PropTypes.func.isRequired,
disableSlider: PropTypes.bool
};
static defaultProps = {
className: null,
style: null,
min: null,
max: null,
step: null,
path: null,
label: null,
disableSlider: null
};
constructor() {
super();
this.state = { value: null };
}
static getDerivedStateFromProps(nextProps) {
const { min, max, step } = nextProps;
const nextValue = applyConstraints({
value: result(nextProps.data, nextProps.path),
min,
max,
step
});
return {
value: nextValue
};
}
handleChange = event => {
const { value } = event.target;
this.update(value);
};
handleSliderUpdate = value => {
const { min, max, step } = this.props;
this.update(applyConstraints({ value, min, max, step }));
};
update = value => {
const { _onUpdateValue, path } = this.props;
_onUpdateValue(path, toNumber(value));
};
renderSlider(width) {
const { min, max } = this.props;
const { value } = this.state;
return (
<Slider
value={value}
min={min}
max={max}
width={width}
onUpdate={this.handleSliderUpdate}
/>
);
}
render() {
const {
min,
max,
path,
label,
labelWidth,
step,
disableSlider,
className,
style
} = this.props;
const labelText = isString(label) ? label : path;
const hasSlider = isFinite(min) && isFinite(max);
const controlsWidth = 100;
const inputWidth =
hasSlider && disableSlider !== true
? Math.round(controlsWidth / 3)
: controlsWidth;
const sliderWidth = controlsWidth - inputWidth;
return (
<li className={cx('cr', 'number', className)} style={style}>
<label>
<span className="label-text" style={{ width: labelWidth }}>
{labelText}
</span>
<span
style={{ display: 'inherit', width: `calc(100% - ${labelWidth})` }}
>
{hasSlider && disableSlider !== true
? this.renderSlider(sliderWidth)
: null}
<input
type="number"
step={step}
min={min}
max={max}
inputMode="numeric"
value={this.state.value}
style={{ width: `${inputWidth}%` }}
onChange={this.handleChange}
/>
</span>
</label>
</li>
);
}
}