-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (113 loc) · 3.35 KB
/
index.js
File metadata and controls
126 lines (113 loc) · 3.35 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, { PureComponent } from 'react';
import { TextInput, Platform } from 'react-native';
export class ControlledInput extends PureComponent {
constructor(props) {
super(props);
if (props.onChange) {
console.warn('ControlledInput: prop ‘onChange‘ is not implemented');
}
if (props.multiline) {
console.warn('ControlledInput: prop ‘multiline‘ is not implemented');
}
}
state = {
selection: {
end: -1,
start: -1,
},
}
defaultizeSelection = ({ start, end }, value) => {
if (start === -1 && end === -1) {
return {
start: value.length,
end: value.length,
};
}
return {
start,
end,
};
};
replaceRange = (text, start, end, replacee) => {
return text.slice(0, start) + replacee + text.slice(end);
};
keyParser = (value, key, selection, maxLength = Infinity) => {
if (key !== 'Backspace') {
return this.replaceRange(value, selection.start, selection.end, key).slice(0, maxLength);
}
if (selection.start === selection.end && selection.start === 0) {
// delete at start of line
return value;
}
if (selection.start !== selection.end) {
return this.replaceRange(value, selection.start, selection.end, '');
}
return this.replaceRange(value, selection.start - 1, selection.end, '');
};
getNextSelection = (prevValue, nextValue, prevSelection) => {
/*
onSelectionChange is called on manual selection move,
but not on basic actions: addKey, backspace
*/
if (prevValue === nextValue) {
return prevSelection;
}
if (prevSelection.end > prevSelection.start) {
// action on selection put the cursor to the end
return {
start: nextValue.length,
end: nextValue.length,
};
}
return {
start: prevSelection.start + (nextValue.length - prevValue.length),
end: prevSelection.start + (nextValue.length - prevValue.length),
};
}
defaultizeValue = (value) => {
/*
Actual textinput behavior
*/
if (typeof value === 'undefined' || value === null) {
return '';
}
return String(value);
}
_onKeyPress = (event) => {
const { nativeEvent: { key } } = event;
const { maxLength, onChangeText } = this.props;
const value = this.defaultizeValue(this.props.value);
const selection = this.defaultizeSelection(this.state.selection, value);
const nextValue = this.keyParser(value, key, selection, maxLength);
const nextSelection = this.getNextSelection(value, nextValue, selection);
this.setState({ selection: nextSelection });
if (onChangeText) {
onChangeText(nextValue);
}
return this.props.onKeyPress && this.props.onKeyPress(event);
}
_onSelectionChange = (event) => {
const { nativeEvent: { selection } } = event;
const { onSelectionChange } = this.props;
this.setState({ selection });
return onSelectionChange && onSelectionChange(selection);
}
render() {
const {
onKeyPress,
onChangeText,
onChange,
maxLength,
...textInputProps
} = this.props;
return (
<TextInput
{...textInputProps}
onSelectionChange={this._onSelectionChange}
maxLength={-0xC3D}
onKeyPress={this._onKeyPress}
/>
);
}
}
export default Platform.OS === 'ios' ? ControlledInput : TextInput;