-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathEditableCell.js
90 lines (80 loc) · 2.01 KB
/
EditableCell.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
/* @flow weak */
/**
* mSupply Mobile
* Sustainable Solutions (NZ) Ltd. 2016
*/
import React from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
TextInput,
View,
ViewPropTypes,
} from 'react-native';
export class EditableCell extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 'N/A',
};
this.componentWillMount = this.componentWillMount.bind(this);
this.onEndEditing = this.onEndEditing.bind(this);
}
componentWillMount() {
this.setState({
value: String(this.props.value),
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.state.value) {
this.setState({
value: String(nextProps.value),
});
}
}
onEndEditing() {
// If the field is cleared, write null to property
const newValue = this.state.value === '' ? null : this.state.value;
this.props.onEndEditing(this.props.target, newValue);
}
render() {
const { style, width, textStyle, refCallback, ...textInputProps } = this.props;
return (
<View style={[defaultStyles.cell, style, { flex: width }]}>
<TextInput
{...textInputProps}
ref={refCallback}
style={[defaultStyles.text, textStyle]}
onChangeText={(text) => this.setState({ value: text })}
onEndEditing={this.onEndEditing}
value={this.state.value}
/>
</View>
);
}
}
EditableCell.propTypes = {
style: ViewPropTypes.style,
refCallback: PropTypes.func,
textStyle: TextInput.propTypes.style,
width: PropTypes.number,
onEndEditing: PropTypes.func,
target: PropTypes.object,
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
};
EditableCell.defaultProps = {
width: 1,
value: 'N/A',
};
const defaultStyles = StyleSheet.create({
cell: {
flex: 1,
justifyContent: 'center',
},
text: {
right: -9, // This is to account for RN issue 1287, see https://github.com/facebook/react-native/issues/1287
},
});