-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathindex.js
80 lines (69 loc) · 2.81 KB
/
index.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
import React, {useEffect, useRef} from 'react';
import _ from 'underscore';
import styles from '../../styles/styles';
import * as styleConst from './styleConst';
import BaseTextInput from './BaseTextInput';
import * as baseTextInputPropTypes from './baseTextInputPropTypes';
import DomUtils from '../../libs/DomUtils';
import Visibility from '../../libs/Visibility';
import * as Browser from '../../libs/Browser';
function TextInput(props) {
const textInputRef = useRef(null);
const removeVisibilityListenerRef = useRef(null);
useEffect(() => {
if (props.disableKeyboard) {
textInputRef.current.setAttribute('inputmode', 'none');
}
if (props.name) {
textInputRef.current.setAttribute('name', props.name);
}
removeVisibilityListenerRef.current = Visibility.onVisibilityChange(() => {
if (!Browser.isMobileChrome() || !Visibility.isVisible() || !textInputRef.current || DomUtils.getActiveElement() !== textInputRef.current) {
return;
}
textInputRef.current.blur();
textInputRef.current.focus();
});
return () => {
if (!removeVisibilityListenerRef.current) return;
removeVisibilityListenerRef.current();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const isLabeledMultiline = Boolean(props.label.length) && props.multiline;
const labelAnimationStyle = {
'--active-label-translate-y': `${styleConst.ACTIVE_LABEL_TRANSLATE_Y}px`,
'--active-label-scale': `${styleConst.ACTIVE_LABEL_SCALE}`,
'--label-transition-duration': `${styleConst.LABEL_ANIMATION_DURATION}ms`,
};
return (
<BaseTextInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={(el) => {
textInputRef.current = el;
if (!props.innerRef) {
return;
}
if (_.isFunction(props.innerRef)) {
props.innerRef(el);
return;
}
// eslint-disable-next-line no-param-reassign
props.innerRef.current = el;
}}
inputStyle={[styles.baseTextInput, styles.textInputDesktop, isLabeledMultiline ? styles.textInputMultiline : {}, ...props.inputStyle]}
textInputContainerStyles={[labelAnimationStyle, ...props.textInputContainerStyles]}
/>
);
}
TextInput.displayName = 'TextInput';
TextInput.propTypes = baseTextInputPropTypes.propTypes;
TextInput.defaultProps = baseTextInputPropTypes.defaultProps;
export default React.forwardRef((props, ref) => (
<TextInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
));