-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
197 lines (174 loc) · 6.46 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, { PropTypes, Component } from 'react';
import { View, Text, FlatList, TouchableOpacity, Image } from 'react-native';
import styles from './CategoryStyles';
// Third party imports - icon
import EntypoIcons from 'react-native-vector-icons/Entypo';
import EvilIconsIcons from 'react-native-vector-icons/EvilIcons';
import FontAwesomeIcons from 'react-native-vector-icons/FontAwesome';
import FoundationIcons from 'react-native-vector-icons/Foundation';
import IoniconsIcons from 'react-native-vector-icons/Ionicons';
import MaterialIconsIcons from 'react-native-vector-icons/MaterialIcons';
import OcticonsIcons from 'react-native-vector-icons/Octicons';
import ZocialIcons from 'react-native-vector-icons/Zocial';
const iconSets = {
Entypo: EntypoIcons,
EvilIcons: EvilIconsIcons,
FontAwesome: FontAwesomeIcons,
Foundation: FoundationIcons,
Ionicons: IoniconsIcons,
MaterialIcons: MaterialIconsIcons,
Octicons: OcticonsIcons,
Zocial: ZocialIcons,
};
//type text in item category
export const typeText = {
upper: 'UPPER',
lower: 'LOWER',
capitalize: 'CAPITALIZE'
};
class Category extends Component {
static propTypes = {
data: PropTypes.array.isRequired, //data category default
itemText: PropTypes.string.isRequired, //attribule text show in item -> ex: title
itemSelected: PropTypes.func.isRequired, //func when click item
imageData: PropTypes.array, //imageData category
colorTextDefault: PropTypes.string, //Color text default
colorTextSelected: PropTypes.string, //Color text selected
colorItemDefault: PropTypes.string, //Color view default
colorItemSelected: PropTypes.string, //Color view selected
colorIconDefault: PropTypes.string, //Color icon default
colorIconSelected: PropTypes.string, //Color icon selected
bounces: PropTypes.bool, //Bounces iOS
indexSelected: PropTypes.number, //Set item category selected default
iconSet: PropTypes.string, //Set Type icon
iconSize: PropTypes.number
};
static defaultProps = {
data: [], //data category default
itemText: '',
imageData: [], //imageData category default
colorTextDefault: '#f5f3f4',
colorTextSelected: '#000000',
colorItemDefault: 'rgba(255,255,255,0.2)',
colorItemSelected: '#FF4E50',
colorIconDefault: '#900', //Color icon selected
colorIconSelected: '#FFF', //Color icon selected
bounces: false,
indexSelected: 0, //Item selected default 0
iconSet: 'FontAwesome', //Type icon default
iconSize: 30
};
constructor(props) {
super(props);
let arrCategory = [];
try {
if(this.props.imageData.length === 0) {
this.props.data.map((item, index) => {
let newObject;
index === this.props.indexSelected ?
newObject = Object.assign({isSelected: true }, item):
newObject = Object.assign({isSelected: false}, item);
arrCategory.push(newObject);
});
} else {
this.props.imageData.map((item, index) => {
let newObject;
const nameImage = this.props.imageData[index];
index === this.props.indexSelected ?
newObject = Object.assign({index ,name: nameImage, isSelected: true }):
newObject = Object.assign({index ,name: nameImage, isSelected: false});
arrCategory.push(newObject);
});
}
this.state = {
data: arrCategory,
indexSelected: this.props.indexSelected
};
} catch(error) {
console.error('Data not set - Please set data for Category component');
}
}
componentDidMount() {
let category = this.state.data[this.state.indexSelected];
let onPress = this.props.itemSelected;
typeof onPress === 'function' && onPress(category);
}
//action when click item
handleItemCategoryClick(category, rowID) {
category.isSelected = !category.isSelected;
const dataClone = this.state.data;
const unSelected = dataClone[this.state.indexSelected];
unSelected.isSelected = !unSelected.isSelected;
dataClone[this.state.indexSelected] = unSelected;
dataClone[rowID] = category;
this.setState({
data: dataClone,
indexSelected: rowID
});
//call back item selected
let onPress = this.props.itemSelected;
typeof onPress === 'function' && onPress(category);
}
renderTextCategory(text) {
const type = typeText[this.props.textType];
text = text.toString();
switch (type) {
case 'UPPER':
return text.toUpperCase();
break;
case 'LOWER':
return text.toLowerCase();
break;
case 'CAPITALIZE':
return text.charAt(0).toUpperCase() + text.slice(1);
default:
return text;
}
}
//render Item
renderItemCategory({item, index}) {
const colorTextSelected = item.isSelected ? this.props.colorTextSelected : this.props.colorTextDefault;
const colorItemSelected = item.isSelected ? this.props.colorItemSelected : this.props.colorItemDefault;
const colorIconSelected = item.isSelected ? this.props.colorIconSelected : this.props.colorIconDefault;
if (this.props.imageData.length !== 0 ){
Icon = iconSets[this.props.iconSet];
}
return (
<TouchableOpacity
style={[styles.itemStyles, this.props.itemStyles, { backgroundColor: colorItemSelected }]}
onPress={this.handleItemCategoryClick.bind(this, item, index)}
>
{
this.props.imageData.length != 0 &&
<Icon
name={item.name}
size={this.props.iconSize}
color={colorIconSelected}
/>
}
{
this.props.imageData.length == 0 &&
<Text style={[styles.textItemStyles, {color: colorTextSelected}]}>
{this.renderTextCategory(item[this.props.itemText])}
</Text>
}
</TouchableOpacity>
);
}
render() {
return (
<FlatList
style={[styles.categoryStyles, this.props.style]}
contentContainerStyle={styles.flatListStyles}
horizontal
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
bounces={this.props.bounces}
keyExtractor={(item, index) => index}
renderItem={this.renderItemCategory.bind(this)}
data={this.state.data}
/>
);
}
}
export default Category;