I have this code which change the button's name from (KEYWORD) to different one (KEYNOS) each time I click on it. How can I make it to change to third value (KEYCH). where the default name is (A, B, C... etc) and the first click shows Numbers (1,2...etc) and second click shows the Roman Number (I, II ... etc). I've created three Lists for Latin Letters, Numbers and Roman Numbers.
import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
const KEYWORDS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K'];
const KEYNOS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
const KEYNCH = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'];
export default class App extends Component {
state = {
keywordsList: [],
keynosList: [],
keychList: [],
};
toggleKeyword = keyword => {
const { keywordsList } = this.state;
let list = keywordsList;
let index = -1;
if ((index = keywordsList.indexOf(keyword)) != -1) {
list.splice(index, 1);
} else {
list.push(keyword);
}
this.setState({ keywordsList: list });
};
render() {
const { keywordsList } = this.state;
const { container, selectedKeywordStyle, buttonStyle, textStyle } = styles;
return (
<View style={container}>
{KEYWORDS.map((item, index) => {
let style, text;
if (keywordsList.find(element => element === item)) {
style = selectedKeywordStyle;
text = KEYNOS[index];
} else {
style = buttonStyle;
text = item;
}
return (
<TouchableOpacity
key={index} // make sure you assign a unique key for each item
style={style}
onPress={() => this.toggleKeyword(item)}>
<Text style={textStyle}>{text}</Text>
</TouchableOpacity>
);
})}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
justifyContent: 'space-around',
flexWrap: 'wrap',
paddingTop: 50,
},
textStyle: {
color: 'white',
fontSize: 16,
padding: 8,
textAlign: 'center',
},
buttonStyle: {
width: '30%',
backgroundColor: 'green',
borderRadius: 15,
margin: 5,
},
selectedKeywordStyle: {
width: '30%',
backgroundColor: 'red',
borderRadius: 15,
margin: 5,
},
});