-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.native.js
64 lines (54 loc) · 1.97 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
import React from 'react';
import {TextInput} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
/**
* 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.isRequired,
// When the input has cleared whoever owns this input should know about it
onClear: PropTypes.func,
};
const defaultProps = {
shouldClear: false,
onClear: () => {},
};
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() {
return (
<TextInput
ref={el => this.textInput = el}
maxHeight={116}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...this.props}
/>
);
}
}
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} />
));