UNPKG

7.24 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _activeElement = _interopRequireDefault(require("dom-helpers/activeElement"));
7
8var _canUseDOM = _interopRequireDefault(require("dom-helpers/canUseDOM"));
9
10var _propTypes = _interopRequireDefault(require("prop-types"));
11
12var _react = _interopRequireDefault(require("react"));
13
14var _reactDom = require("react-dom");
15
16var _Input = _interopRequireDefault(require("./Input"));
17
18var CustomPropTypes = _interopRequireWildcard(require("./PropTypes"));
19
20const _excluded = ["disabled", "readOnly", "placeholder", "innerRef", "min", "max", "localizer", "editing"];
21
22function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23
24function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
29
30function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
31
32let isSign = val => (val || '').trim() === '-';
33
34function isPaddedZeros(str, localizer) {
35 let localeChar = localizer.decimalCharacter();
36 let [_, decimals] = str.split(localeChar);
37 return !!(decimals && decimals.match(/0+$/));
38}
39
40function isAtDelimiter(str, localizer) {
41 let localeChar = localizer.decimalCharacter();
42 let lastIndex = str.length - 1;
43 if (str.length < 1) return false;
44 let char = str[lastIndex];
45 return !!(char === localeChar && str.indexOf(char) === lastIndex);
46}
47
48class NumberPickerInput extends _react.default.Component {
49 constructor(...args) {
50 super(...args);
51 this.state = {};
52
53 this.handleBlur = event => {
54 let str = this.state.stringValue;
55 let number = this.parseNumber(str); // if number is below the min
56 // we need to flush low values and decimal stops, onBlur means i'm done inputing
57
58 if (this.isIntermediateValue(number, str)) {
59 if (isNaN(number)) {
60 number = null;
61 }
62
63 this.props.onChange(number, event);
64 }
65 };
66
67 this.handleChange = event => {
68 let {
69 value,
70 onChange
71 } = this.props;
72 let stringValue = event.target.value,
73 numberValue = this.parseNumber(stringValue);
74 let isIntermediate = this.isIntermediateValue(numberValue, stringValue);
75
76 if (stringValue == null || stringValue.trim() === '') {
77 this.setStringValue('');
78 onChange(null, event);
79 return;
80 } // order here matters a lot
81
82
83 if (isIntermediate) {
84 this.setStringValue(stringValue);
85 } else if (numberValue !== value) {
86 onChange(numberValue, event);
87 } else if (stringValue != this.state.stringValue) {
88 this.setStringValue(stringValue);
89 }
90 };
91 }
92
93 getSnapshotBeforeUpdate({
94 editing
95 }) {
96 return {
97 reselectText: !editing && this.props.editing && this.isSelectingAllText()
98 };
99 }
100
101 static getDerivedStateFromProps(nextProps, prevState) {
102 let {
103 value,
104 editing,
105 localizer
106 } = nextProps;
107 let decimal = localizer.decimalCharacter();
108 const stringValue = value == null || isNaN(value) ? '' : editing ? ('' + value).replace('.', decimal) : localizer.formatNumber(value
109 /*, 'default'*/
110 );
111 if (prevState.lastValueFromProps !== stringValue) return {
112 stringValue,
113 lastValueFromProps: stringValue
114 };
115 return null;
116 }
117
118 componentDidUpdate(_, __, {
119 reselectText
120 }) {
121 if (reselectText) (0, _reactDom.findDOMNode)(this).select();
122 } // this intermediate state is for when one runs into
123 // the decimal or are typing the number
124
125
126 setStringValue(stringValue) {
127 this.setState({
128 stringValue
129 });
130 }
131
132 isIntermediateValue(num, str) {
133 let {
134 localizer,
135 min
136 } = this.props;
137 return !!(num < min || isSign(str) || isAtDelimiter(str, localizer) || isPaddedZeros(str, localizer));
138 }
139
140 isSelectingAllText() {
141 const node = _canUseDOM.default && (0, _reactDom.findDOMNode)(this);
142 return _canUseDOM.default && (0, _activeElement.default)() === node && node.selectionStart === 0 && node.selectionEnd === node.value.length;
143 }
144
145 parseNumber(strVal) {
146 let {
147 localizer,
148 parse: userParse
149 } = this.props;
150 if (userParse) return userParse(strVal, localizer);
151 return localizer.parseNumber(strVal);
152 }
153
154 render() {
155 let _this$props = this.props,
156 {
157 disabled,
158 readOnly,
159 placeholder,
160 // eslint-disable-next-line react/prop-types
161 innerRef,
162 min,
163 max
164 } = _this$props,
165 props = _objectWithoutPropertiesLoose(_this$props, _excluded);
166
167 let value = this.state.stringValue;
168 return /*#__PURE__*/_react.default.createElement(_Input.default, _extends({}, props, {
169 ref: innerRef,
170 inputMode: "numeric",
171 className: "rw-widget-input",
172 onChange: this.handleChange,
173 onBlur: this.handleBlur,
174 "aria-valuenow": value
175 /*HACK*/
176 ,
177 "aria-valuemin": isFinite(min) ? min : undefined,
178 "aria-valuemax": isFinite(max) ? max : undefined,
179 disabled: disabled,
180 readOnly: readOnly,
181 placeholder: placeholder,
182 value: value
183 }));
184 }
185
186}
187
188NumberPickerInput.defaultProps = {
189 value: null,
190 editing: false
191};
192NumberPickerInput.propTypes = {
193 value: _propTypes.default.number,
194 editing: _propTypes.default.bool,
195 placeholder: _propTypes.default.string,
196 localizer: _propTypes.default.object.isRequired,
197 parse: _propTypes.default.func,
198 min: _propTypes.default.number,
199 max: _propTypes.default.number,
200 disabled: CustomPropTypes.disabled,
201 readOnly: CustomPropTypes.disabled,
202 onChange: _propTypes.default.func.isRequired
203};
204var _default = NumberPickerInput;
205exports.default = _default;
\No newline at end of file