add item delete function

This commit is contained in:
thewisefarmerr
2019-11-20 15:20:06 +05:00
parent 46b7ec625a
commit ffb2457606

View File

@@ -44,7 +44,11 @@ export function useForceUpdate() {
const ListsEditor = ({navigation}) => { const ListsEditor = ({navigation}) => {
const [colors, setColors] = useState(COLOR_SCHEME); const [colors, setColors] = useState(COLOR_SCHEME);
const [listData, setListData] = useState(['']); const [listData, setListData] = useState(['']);
const [deleteItemsList, setDeleteItemsList] = useState([]);
const [deleteMode, setDeleteMode] = useState(false);
const forceUpdate = useForceUpdate(); const forceUpdate = useForceUpdate();
const _textRender = createRef(); const _textRender = createRef();
let prevItem = null; let prevItem = null;
let prevIndex = null; let prevIndex = null;
@@ -108,6 +112,25 @@ const ListsEditor = ({navigation}) => {
<ListItem <ListItem
item={item} item={item}
index={index} index={index}
deleteItemsList={deleteItemsList}
deleteMode={deleteMode}
addToDeleteList={index => {
let items = deleteItemsList;
console.log(items);
if (items.includes(index)) {
let i = items.indexOf(index);
items.splice(i, 1);
setDeleteItemsList(items);
console.log(JSON.stringify(deleteItemsList), 'splice');
} else {
items[items.length] = index;
console.log(JSON.stringify(items), 'here');
setDeleteItemsList(items);
}
items = null;
forceUpdate();
}}
onSubmit={(text, index, willFocus = true) => { onSubmit={(text, index, willFocus = true) => {
let oldData = listData; let oldData = listData;
oldData[index] = text; oldData[index] = text;
@@ -202,6 +225,7 @@ const ListItem = props => {
</Text> </Text>
<TextInput <TextInput
ref={inputRef} ref={inputRef}
editable={props.deleteMode ? false : true}
placeholder={'List item - tap to edit'} placeholder={'List item - tap to edit'}
style={{ style={{
paddingVertical: 5, paddingVertical: 5,
@@ -230,7 +254,7 @@ const ListItem = props => {
} }
}} }}
/> />
{showSubmit ? ( {showSubmit && !props.deleteMode ? (
<TouchableOpacity <TouchableOpacity
activeOpacity={opacity} activeOpacity={opacity}
onPress={() => props.onSubmit(text, props.index)} onPress={() => props.onSubmit(text, props.index)}
@@ -250,6 +274,37 @@ const ListItem = props => {
) : ( ) : (
undefined undefined
)} )}
{props.deleteMode ? (
<TouchableOpacity
activeOpacity={opacity}
onPress={() => props.addToDeleteList(props.index)}
style={{
paddingVertical: 5,
height: 40,
minWidth: w * 0.9 * 0.1,
justifyContent: 'center',
alignItems: 'center',
color: 'white',
backgroundColor: props.deleteItemsList.includes(props.index)
? colors.accent
: colors.navbg,
borderRadius: 5,
borderColor: props.deleteItemsList.includes(props.index)
? colors.accent
: 'transparent',
borderWidth: 1,
marginLeft: 5,
}}>
{props.deleteItemsList.includes(props.index) ? (
<Icon name="ios-checkmark" color="white" size={SIZE.xl} />
) : (
undefined
)}
</TouchableOpacity>
) : (
undefined
)}
</View> </View>
); );
}; };