-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.ios.js
97 lines (81 loc) · 3.07 KB
/
index.ios.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
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import RNTextInput from '../RNTextInput';
import themeColors from '../../styles/themes/default';
import CONST from '../../CONST';
/**
* On native layers we like to have the Text Input not focused so the user can read new chats without they keyboard in
* the way of the view
* On Android, the selection prop is required on the TextInput but this prop has issues on IOS
* https://github.com/facebook/react-native/issues/29063
*/
const propTypes = {
/** If the input should clear, it actually gets intercepted instead of .clear() */
shouldClear: PropTypes.bool,
/** A ref to forward to the text input */
forwardedRef: PropTypes.func,
/** When the input has cleared whoever owns this input should know about it */
onClear: PropTypes.func,
/** Set focus to this component the first time it renders.
* Override this in case you need to set focus on one field out of many, or when you want to disable autoFocus */
autoFocus: PropTypes.bool,
/** Prevent edits and interactions like focus for this input. */
isDisabled: PropTypes.bool,
/** Selection Object */
selection: PropTypes.shape({
start: PropTypes.number,
end: PropTypes.number,
}),
};
const defaultProps = {
shouldClear: false,
onClear: () => {},
autoFocus: false,
isDisabled: false,
forwardedRef: null,
selection: {
start: 0,
end: 0,
},
};
class TextInputFocusable extends React.Component {
componentDidMount() {
// This callback prop is used by the parent component using the constructor to
// get a ref to the inner textInput element e.g. if we do
// <constructor ref={el => this.textInput = el} /> this will not
// return a ref to the component, but rather the HTML element by default
if (!this.props.forwardedRef || !_.isFunction(this.props.forwardedRef)) {
return;
}
this.props.forwardedRef(this.textInput);
}
componentDidUpdate(prevProps) {
if (prevProps.shouldClear || !this.props.shouldClear) {
return;
}
this.textInput.clear();
this.props.onClear();
}
render() {
// Selection Property not worked in IOS properly, So removed from props.
const propsToPass = _.omit(this.props, 'selection');
return (
<RNTextInput
placeholderTextColor={themeColors.placeholderText}
ref={el => this.textInput = el}
maxHeight={CONST.COMPOSER_MAX_HEIGHT}
rejectResponderTermination={false}
editable={!this.props.isDisabled}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...propsToPass}
/>
);
}
}
TextInputFocusable.propTypes = propTypes;
TextInputFocusable.defaultProps = defaultProps;
export default React.forwardRef((props, ref) => (
/* eslint-disable-next-line react/jsx-props-no-spreading */
<TextInputFocusable {...props} forwardedRef={ref} />
));