UNPKG

5.68 kBJavaScriptView Raw
1/* !
2 * Copyright 2020 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __decorate, __extends, __rest } from "tslib";
17import * as React from "react";
18import { polyfill } from "react-lifecycles-compat";
19import { DISPLAYNAME_PREFIX } from "../../common/props";
20/**
21 * A stateful wrapper around the low-level <input> component which works around a
22 * [React bug](https://github.com/facebook/react/issues/3926). This bug is reproduced when an input
23 * receives CompositionEvents (for example, through IME composition) and has its value prop updated
24 * asychronously. This might happen if a component chooses to do async validation of a value
25 * returned by the input's `onChange` callback.
26 *
27 * Note: this component does not apply any Blueprint-specific styling.
28 */
29var AsyncControllableInput = /** @class */ (function (_super) {
30 __extends(AsyncControllableInput, _super);
31 function AsyncControllableInput() {
32 var _this = _super !== null && _super.apply(this, arguments) || this;
33 _this.state = {
34 hasPendingUpdate: false,
35 isComposing: false,
36 nextValue: _this.props.value,
37 value: _this.props.value,
38 };
39 _this.handleCompositionStart = function (e) {
40 var _a, _b;
41 _this.setState({
42 isComposing: true,
43 // Make sure that localValue matches externalValue, in case externalValue
44 // has changed since the last onChange event.
45 nextValue: _this.state.value,
46 });
47 (_b = (_a = _this.props).onCompositionStart) === null || _b === void 0 ? void 0 : _b.call(_a, e);
48 };
49 _this.handleCompositionEnd = function (e) {
50 var _a, _b;
51 _this.setState({ isComposing: false });
52 (_b = (_a = _this.props).onCompositionEnd) === null || _b === void 0 ? void 0 : _b.call(_a, e);
53 };
54 _this.handleChange = function (e) {
55 var _a, _b;
56 var value = e.target.value;
57 _this.setState({ nextValue: value });
58 (_b = (_a = _this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, e);
59 };
60 return _this;
61 }
62 AsyncControllableInput.getDerivedStateFromProps = function (nextProps, nextState) {
63 if (nextState.isComposing || nextProps.value === undefined) {
64 // don't derive anything from props if:
65 // - in uncontrolled mode, OR
66 // - currently composing, since we'll do that after composition ends
67 return null;
68 }
69 var userTriggeredUpdate = nextState.nextValue !== nextState.value;
70 if (userTriggeredUpdate) {
71 if (nextProps.value === nextState.nextValue) {
72 // parent has processed and accepted our update
73 if (nextState.hasPendingUpdate) {
74 return { value: nextProps.value, hasPendingUpdate: false };
75 }
76 else {
77 return { value: nextState.nextValue };
78 }
79 }
80 else {
81 if (nextProps.value === nextState.value) {
82 // we have sent the update to our parent, but it has not been processed yet. just wait.
83 // DO NOT set nextValue here, since that will temporarily render a potentially stale controlled value,
84 // causing the cursor to jump once the new value is accepted
85 return { hasPendingUpdate: true };
86 }
87 // accept controlled update overriding user action
88 return { value: nextProps.value, nextValue: nextProps.value, hasPendingUpdate: false };
89 }
90 }
91 else {
92 // accept controlled update, could be confirming or denying user action
93 return { value: nextProps.value, nextValue: nextProps.value, hasPendingUpdate: false };
94 }
95 };
96 AsyncControllableInput.prototype.render = function () {
97 var _a = this.state, isComposing = _a.isComposing, hasPendingUpdate = _a.hasPendingUpdate, value = _a.value, nextValue = _a.nextValue;
98 var _b = this.props, inputRef = _b.inputRef, restProps = __rest(_b, ["inputRef"]);
99 return (React.createElement("input", __assign({}, restProps, { ref: inputRef,
100 // render the pending value even if it is not confirmed by a parent's async controlled update
101 // so that the cursor does not jump to the end of input as reported in
102 // https://github.com/palantir/blueprint/issues/4298
103 value: isComposing || hasPendingUpdate ? nextValue : value, onCompositionStart: this.handleCompositionStart, onCompositionEnd: this.handleCompositionEnd, onChange: this.handleChange })));
104 };
105 AsyncControllableInput.displayName = DISPLAYNAME_PREFIX + ".AsyncControllableInput";
106 AsyncControllableInput = __decorate([
107 polyfill
108 ], AsyncControllableInput);
109 return AsyncControllableInput;
110}(React.PureComponent));
111export { AsyncControllableInput };
112//# sourceMappingURL=asyncControllableInput.js.map
\No newline at end of file