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 | */
|
16 | import { __assign, __extends, __rest } from "tslib";
|
17 | import * as React from "react";
|
18 | import { AbstractPureComponent2, DISPLAYNAME_PREFIX } from "../../common";
|
19 | /**
|
20 | * A stateful wrapper around the low-level <input> component which works around a
|
21 | * [React bug](https://github.com/facebook/react/issues/3926). This bug is reproduced when an input
|
22 | * receives CompositionEvents (for example, through IME composition) and has its value prop updated
|
23 | * asychronously. This might happen if a component chooses to do async validation of a value
|
24 | * returned by the input's `onChange` callback.
|
25 | *
|
26 | * Note: this component does not apply any Blueprint-specific styling.
|
27 | */
|
28 | var AsyncControllableInput = /** @class */ (function (_super) {
|
29 | __extends(AsyncControllableInput, _super);
|
30 | function AsyncControllableInput() {
|
31 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
32 | _this.state = {
|
33 | hasPendingUpdate: false,
|
34 | isComposing: false,
|
35 | nextValue: _this.props.value,
|
36 | value: _this.props.value,
|
37 | };
|
38 | _this.cancelPendingCompositionEnd = null;
|
39 | _this.handleCompositionStart = function (e) {
|
40 | var _a, _b, _c;
|
41 | (_a = _this.cancelPendingCompositionEnd) === null || _a === void 0 ? void 0 : _a.call(_this);
|
42 | _this.setState({ isComposing: true });
|
43 | (_c = (_b = _this.props).onCompositionStart) === null || _c === void 0 ? void 0 : _c.call(_b, e);
|
44 | };
|
45 | _this.handleCompositionEnd = function (e) {
|
46 | var _a, _b;
|
47 | // In some non-latin languages, a keystroke can end a composition event and immediately afterwards start another.
|
48 | // This can lead to unexpected characters showing up in the text input. In order to circumvent this problem, we
|
49 | // use a timeout which creates a delay which merges the two composition events, creating a more natural and predictable UX.
|
50 | // `this.state.nextValue` will become "locked" (it cannot be overwritten by the `value` prop) until a delay (10ms) has
|
51 | // passed without a new composition event starting.
|
52 | _this.cancelPendingCompositionEnd = _this.setTimeout(function () { return _this.setState({ isComposing: false }); }, AsyncControllableInput.COMPOSITION_END_DELAY);
|
53 | (_b = (_a = _this.props).onCompositionEnd) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
54 | };
|
55 | _this.handleChange = function (e) {
|
56 | var _a, _b;
|
57 | var value = e.target.value;
|
58 | _this.setState({ nextValue: value });
|
59 | (_b = (_a = _this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
60 | };
|
61 | return _this;
|
62 | }
|
63 | AsyncControllableInput.getDerivedStateFromProps = function (nextProps, nextState) {
|
64 | if (nextState.isComposing || nextProps.value === undefined) {
|
65 | // don't derive anything from props if:
|
66 | // - in uncontrolled mode, OR
|
67 | // - currently composing, since we'll do that after composition ends
|
68 | return null;
|
69 | }
|
70 | var userTriggeredUpdate = nextState.nextValue !== nextState.value;
|
71 | if (userTriggeredUpdate) {
|
72 | if (nextProps.value === nextState.nextValue) {
|
73 | // parent has processed and accepted our update
|
74 | if (nextState.hasPendingUpdate) {
|
75 | return { value: nextProps.value, hasPendingUpdate: false };
|
76 | }
|
77 | else {
|
78 | return { value: nextState.nextValue };
|
79 | }
|
80 | }
|
81 | else {
|
82 | if (nextProps.value === nextState.value) {
|
83 | // we have sent the update to our parent, but it has not been processed yet. just wait.
|
84 | // DO NOT set nextValue here, since that will temporarily render a potentially stale controlled value,
|
85 | // causing the cursor to jump once the new value is accepted
|
86 | return { hasPendingUpdate: true };
|
87 | }
|
88 | // accept controlled update overriding user action
|
89 | return { value: nextProps.value, nextValue: nextProps.value, hasPendingUpdate: false };
|
90 | }
|
91 | }
|
92 | else {
|
93 | // accept controlled update, could be confirming or denying user action
|
94 | return { value: nextProps.value, nextValue: nextProps.value, hasPendingUpdate: false };
|
95 | }
|
96 | };
|
97 | AsyncControllableInput.prototype.render = function () {
|
98 | var _a = this.state, isComposing = _a.isComposing, hasPendingUpdate = _a.hasPendingUpdate, value = _a.value, nextValue = _a.nextValue;
|
99 | var _b = this.props, inputRef = _b.inputRef, restProps = __rest(_b, ["inputRef"]);
|
100 | return (React.createElement("input", __assign({}, restProps, { ref: inputRef,
|
101 | // render the pending value even if it is not confirmed by a parent's async controlled update
|
102 | // so that the cursor does not jump to the end of input as reported in
|
103 | // https://github.com/palantir/blueprint/issues/4298
|
104 | value: isComposing || hasPendingUpdate ? nextValue : value, onCompositionStart: this.handleCompositionStart, onCompositionEnd: this.handleCompositionEnd, onChange: this.handleChange })));
|
105 | };
|
106 | AsyncControllableInput.displayName = "".concat(DISPLAYNAME_PREFIX, ".AsyncControllableInput");
|
107 | /**
|
108 | * The amount of time (in milliseconds) which the input will wait after a compositionEnd event before
|
109 | * unlocking its state value for external updates via props. See `handleCompositionEnd` for more details.
|
110 | */
|
111 | AsyncControllableInput.COMPOSITION_END_DELAY = 10;
|
112 | return AsyncControllableInput;
|
113 | }(AbstractPureComponent2));
|
114 | export { AsyncControllableInput };
|
115 | //# sourceMappingURL=asyncControllableInput.js.map |
\ | No newline at end of file |