-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (96 loc) · 2.62 KB
/
index.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
98
99
100
101
import React, {useRef, useState} from 'react';
import {View, Text, TextInput, TouchableOpacity} from 'react-native';
const renderIt = (choice) => {
return (
<View
style={{
//backgroundColor: constants.RED,
backgroundColor: 'rgb(0,122,255)',
borderRadius: 10,
paddingTop: 2,
paddingBottom: 2,
paddingLeft: 5,
paddingRight: 5,
marginLeft: 2,
marginRight: 2,
marginTop: 4,
}}>
<Text
style={{
color: 'white',
//fontFamily: 'Nunito-Light',
borderRadius: 5,
}}>
{choice}
</Text>
</View>
);
};
const BubbleInput = ({
data,
renderItem = renderIt,
addData = (item) => {
return item;
},
placeholder = 'Add choices here',
}) => {
const [dummyState, setDummyState] = useState(false);
const ref = useRef(null);
const renderChoices = (data) => {
return data.map(renderItem);
};
return (
<View style={{flexDirection: 'row', flexWrap: 'wrap', flex: 1}}>
{renderChoices(data)}
<TextInput
ref={ref}
onChange={(event) => {
ref.current.textInputValue = event.nativeEvent.text;
}}
blurOnSubmit={false}
placeholder={data.length > 0 ? '' : placeholder}
onKeyPress={({nativeEvent}) => {
if (nativeEvent.key === 'Backspace') {
//console.log(item.ref.current.textInputValue);
if (
ref.current.textInputValue === '' ||
ref.current.textInputValue === undefined
) {
data.pop();
setDummyState(!dummyState);
}
}
}}
onBlur={(event) => {
if (event.nativeEvent.text.trim() !== '') {
data.push(addData(event.nativeEvent.text));
setDummyState(!dummyState);
ref.current.clear();
ref.current.textInputValue = '';
}
}}
onSubmitEditing={(event) => {
//console.log(event.nativeEvent.text);
if (event.nativeEvent.text.trim() === '') {
ref.current.clear();
ref.current.textInputValue = '';
return;
}
data.push(addData(event.nativeEvent.text));
ref.current.clear();
setTimeout(() => {
setDummyState(!dummyState);
}, 100);
ref.current.textInputValue = '';
}}
style={{
flex: 1,
paddingTop: 2,
paddingBottom: 2,
marginTop: 4,
backgroundColor: 'transparent',
}}></TextInput>
</View>
);
};
module.exports = BubbleInput;