-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.native.js
91 lines (77 loc) · 2.88 KB
/
index.native.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
import React from 'react';
import {TextInput} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import themeColors from '../../styles/themes/default';
/**
* 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
*/
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)) {
this.props.forwardedRef(this.textInput);
}
}
componentDidUpdate(prevProps) {
if (!prevProps.shouldClear && this.props.shouldClear) {
this.textInput.clear();
this.props.onClear();
}
}
render() {
// Selection Property not worked in IOS properly, So removed from props.
const {selection, ...newProps} = this.props;
return (
<TextInput
placeholderTextColor={themeColors.placeholderText}
ref={el => this.textInput = el}
maxHeight={116}
rejectResponderTermination={false}
editable={!this.props.isDisabled}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...newProps}
/>
);
}
}
TextInputFocusable.displayName = 'TextInputFocusable';
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} />
));