remove selected item code
This commit is contained in:
@@ -49,7 +49,6 @@ export function TagInput({
|
|||||||
|
|
||||||
const [value, setValue] = React.useState('')
|
const [value, setValue] = React.useState('')
|
||||||
const [tags, setTags] = React.useState<string[]>([])
|
const [tags, setTags] = React.useState<string[]>([])
|
||||||
const [selectedItemIndex, setSelectedItemIndex] = React.useState(0)
|
|
||||||
const [suggestions, setSuggestions] = React.useState<string[]>([])
|
const [suggestions, setSuggestions] = React.useState<string[]>([])
|
||||||
const [isInitialLoad, setIsInitialLoad] = React.useState(true)
|
const [isInitialLoad, setIsInitialLoad] = React.useState(true)
|
||||||
|
|
||||||
@@ -57,9 +56,8 @@ export function TagInput({
|
|||||||
setValue('')
|
setValue('')
|
||||||
model.setActive(false)
|
model.setActive(false)
|
||||||
model.clear()
|
model.clear()
|
||||||
setSelectedItemIndex(0)
|
|
||||||
setSuggestions([])
|
setSuggestions([])
|
||||||
}, [model, setValue, setSelectedItemIndex, setSuggestions])
|
}, [model, setValue, setSuggestions])
|
||||||
|
|
||||||
const addTags = React.useCallback(
|
const addTags = React.useCallback(
|
||||||
(_tags: string[]) => {
|
(_tags: string[]) => {
|
||||||
@@ -85,16 +83,17 @@ export function TagInput({
|
|||||||
addTags(uniq([...tags, tag]).slice(0, max))
|
addTags(uniq([...tags, tag]).slice(0, max))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
setValue('')
|
setValue('')
|
||||||
input.current?.focus()
|
input.current?.focus()
|
||||||
|
}, 1)
|
||||||
},
|
},
|
||||||
[max, tags, setValue, addTags],
|
[max, tags, setValue, addTags],
|
||||||
)
|
)
|
||||||
|
|
||||||
const onSubmitEditing = React.useCallback(() => {
|
const onSubmitEditing = React.useCallback(() => {
|
||||||
const item = suggestions[selectedItemIndex]
|
addTagAndReset(value)
|
||||||
addTagAndReset(item || value)
|
}, [value, addTagAndReset])
|
||||||
}, [value, suggestions, selectedItemIndex, addTagAndReset])
|
|
||||||
|
|
||||||
const onKeyPress = React.useCallback(
|
const onKeyPress = React.useCallback(
|
||||||
(e: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
|
(e: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
|
||||||
@@ -106,29 +105,8 @@ export function TagInput({
|
|||||||
e.preventDefault() // prevents an additional space on web
|
e.preventDefault() // prevents an additional space on web
|
||||||
addTagAndReset(value)
|
addTagAndReset(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === 'Escape') {
|
|
||||||
reset()
|
|
||||||
} else if (key === 'ArrowUp') {
|
|
||||||
e.preventDefault()
|
|
||||||
setSelectedItemIndex(
|
|
||||||
(selectedItemIndex + suggestions.length - 1) % suggestions.length,
|
|
||||||
)
|
|
||||||
} else if (key === 'ArrowDown') {
|
|
||||||
e.preventDefault()
|
|
||||||
setSelectedItemIndex((selectedItemIndex + 1) % suggestions.length)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[
|
[value, tags, addTags, addTagAndReset],
|
||||||
value,
|
|
||||||
tags,
|
|
||||||
selectedItemIndex,
|
|
||||||
suggestions.length,
|
|
||||||
reset,
|
|
||||||
setSelectedItemIndex,
|
|
||||||
addTags,
|
|
||||||
addTagAndReset,
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const onChangeText = React.useCallback(
|
const onChangeText = React.useCallback(
|
||||||
@@ -206,10 +184,11 @@ export function TagInput({
|
|||||||
<BottomSheet
|
<BottomSheet
|
||||||
ref={sheet}
|
ref={sheet}
|
||||||
index={-1}
|
index={-1}
|
||||||
snapPoints={[200]}
|
snapPoints={['90%']}
|
||||||
enablePanDownToClose
|
enablePanDownToClose
|
||||||
|
keyboardBehavior="extend"
|
||||||
|
backgroundStyle={{backgroundColor: 'transparent'}}
|
||||||
android_keyboardInputMode="adjustResize"
|
android_keyboardInputMode="adjustResize"
|
||||||
keyboardBlurBehavior="restore"
|
|
||||||
backdropComponent={props => (
|
backdropComponent={props => (
|
||||||
<BottomSheetBackdrop
|
<BottomSheetBackdrop
|
||||||
appearsOnIndex={0}
|
appearsOnIndex={0}
|
||||||
@@ -236,9 +215,16 @@ export function TagInput({
|
|||||||
))}
|
))}
|
||||||
|
|
||||||
<BottomSheetTextInput
|
<BottomSheetTextInput
|
||||||
|
autoCapitalize="none"
|
||||||
|
autoComplete="off"
|
||||||
placeholder="Add tags..."
|
placeholder="Add tags..."
|
||||||
value={value}
|
value={value}
|
||||||
style={styles.input}
|
style={[
|
||||||
|
styles.input,
|
||||||
|
{
|
||||||
|
placeholderTextColor: pal.textLight.color,
|
||||||
|
},
|
||||||
|
]}
|
||||||
onChangeText={onChangeText}
|
onChangeText={onChangeText}
|
||||||
onKeyPress={onKeyPress}
|
onKeyPress={onKeyPress}
|
||||||
onSubmitEditing={onSubmitEditing}
|
onSubmitEditing={onSubmitEditing}
|
||||||
|
|||||||
Reference in New Issue
Block a user