Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is happening in the SINGLE case. #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 97 additions & 82 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ export default class SearchableDropDown extends Component {
if (this.state.focus) {
const flatListPorps = { ...this.props.listProps };
const oldSupport = [
{ key: 'keyboardShouldPersistTaps', val: 'always' },
{ key: 'nestedScrollEnabled', val : false },
{ key: 'style', val : { ...this.props.itemsContainerStyle } },
{ key: 'data', val : this.state.listItems },
{ key: 'keyExtractor', val : (item, index) => index.toString() },
{ key: 'renderItem', val : ({ item, index }) => this.renderItems(item, index) },
{ key: 'keyboardShouldPersistTaps', val: 'always' },
{ key: 'nestedScrollEnabled', val: false },
{ key: 'style', val: { ...this.props.itemsContainerStyle } },
{ key: 'data', val: this.state.listItems },
{ key: 'keyExtractor', val: (item, index) => index.toString() },
{ key: 'renderItem', val: ({ item, index }) => this.renderItems(item, index) },
];
oldSupport.forEach((kv) => {
if(!Object.keys(flatListPorps).includes(kv.key)) {
if (!Object.keys(flatListPorps).includes(kv.key)) {
flatListPorps[kv.key] = kv.val;
} else {
if(kv.key === 'style') {
if (kv.key === 'style') {
flatListPorps['style'] = kv.val;
}
}
});
return (
<FlatList
{ ...flatListPorps }
{...flatListPorps}
/>
);
}
Expand All @@ -70,9 +70,9 @@ export default class SearchableDropDown extends Component {
searchedItems = searchedText => {
let setSort = this.props.setSort;
if (!setSort && typeof setSort !== 'function') {
setSort = (item, searchedText) => {
return item.name.toLowerCase().indexOf(searchedText.toLowerCase()) > -1
};
setSort = (item, searchedText) => {
return item.name.toLowerCase().indexOf(searchedText.toLowerCase()) > -1
};
}
var ac = this.props.items.filter((item) => {
return setSort(item, searchedText);
Expand All @@ -91,31 +91,31 @@ export default class SearchableDropDown extends Component {
};

renderItems = (item, index) => {
if(this.props.multi && this.props.selectedItems && this.props.selectedItems.length > 0) {
if (this.props.multi && this.props.selectedItems && this.props.selectedItems.length > 0) {
return (
this.props.selectedItems.find(sitem => sitem.id === item.id)
?
this.props.selectedItems.find(sitem => sitem.id === item.id)
?
<TouchableOpacity style={{ ...this.props.itemStyle, flex: 1, flexDirection: 'row' }}>
<View style={{ flex: 0.9, flexDirection: 'row', alignItems: 'flex-start' }}>
<Text>{ item.name }</Text>
<Text>{item.name}</Text>
</View>
<View style={{ flex: 0.1, flexDirection: 'row', alignItems: 'flex-end' }}>
<TouchableOpacity onPress={() => setTimeout(() => { this.props.onRemoveItem(item, index) }, 0) } style={{ backgroundColor: '#f16d6b', alignItems: 'center', justifyContent: 'center', width: 25, height: 25, borderRadius: 100, marginLeft: 10}}>
<TouchableOpacity onPress={() => setTimeout(() => { this.props.onRemoveItem(item, index) }, 0)} style={{ backgroundColor: '#f16d6b', alignItems: 'center', justifyContent: 'center', width: 25, height: 25, borderRadius: 100, marginLeft: 10 }}>
<Text>X</Text>
</TouchableOpacity>
</View>
</TouchableOpacity>
:
:
<TouchableOpacity
onPress={() => {
this.setState({ item: item });
setTimeout(() => {
this.props.onItemSelect(item);
}, 0);
}}
style={{ ...this.props.itemStyle, flex: 1, flexDirection: 'row' }}>
onPress={() => {
this.setState({ item: item });
setTimeout(() => {
this.props.onItemSelect(item);
}, 0);
}}
style={{ ...this.props.itemStyle, flex: 1, flexDirection: 'row' }}>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'flex-start' }}>
<Text>{ item.name }</Text>
<Text>{item.name}</Text>
</View>
</TouchableOpacity>
)
Expand All @@ -135,11 +135,11 @@ export default class SearchableDropDown extends Component {
}, 0);
}}
>
{
this.props.selectedItems && this.props.selectedItems.length > 0 && this.props.selectedItems.find(x => x.id === item.id)
?
{
this.props.selectedItems && this.props.selectedItems.length > 0 && this.props.selectedItems.find(x => x.id === item.id)
?
<Text style={{ ...this.props.itemTextStyle }}>{item.name}</Text>
:
:
<Text style={{ ...this.props.itemTextStyle }}>{item.name}</Text>
}
</TouchableOpacity>
Expand All @@ -154,20 +154,29 @@ export default class SearchableDropDown extends Component {
renderTextInput = () => {
const textInputProps = { ...this.props.textInputProps };
const oldSupport = [
{ key: 'ref', val: e => (this.input = e) },
{ key: 'onTextChange', val: (text) => { this.searchedItems(text) } },
{ key: 'underlineColorAndroid', val: this.props.underlineColorAndroid },
{
key: 'onFocus',
{ key: 'ref', val: e => (this.input = e) },
{ key: 'onTextChange', val: (text) => { this.searchedItems(text) } },
{ key: 'underlineColorAndroid', val: this.props.underlineColorAndroid },
{
key: 'onFocus',
val: () => {
this.props.onFocus && this.props.onFocus()
this.setState({
focus: true,
item: defaultItemValue,
listItems: this.props.items
});
}
},
if (typeof (this.props.multi) == 'undefined' || (typeof (this.props.multi) != 'undefined' && this.props.multi == false)) {
const lastValue = this.props.selectedItems[0]
this.setState({
focus: true,
item: !lastValue?.name.trim() ? defaultItemValue : lastValue,
listItems: this.props.items
});
} else {
this.setState({
focus: true,
item: defaultItemValue,
listItems: this.props.items
});
}
}
},
{
key: 'onBlur',
val: () => {
Expand All @@ -193,31 +202,37 @@ export default class SearchableDropDown extends Component {
}
];
oldSupport.forEach((kv) => {
if(!Object.keys(textInputProps).includes(kv.key)) {
if(kv.key === 'onTextChange' || kv.key === 'onChangeText') {
if (!Object.keys(textInputProps).includes(kv.key)) {
if (kv.key === 'onTextChange' || kv.key === 'onChangeText') {
textInputProps['onChangeText'] = kv.val;
} else {
textInputProps[kv.key] = kv.val;
}
} else {
if(kv.key === 'onTextChange' || kv.key === 'onChangeText') {
if (kv.key === 'onTextChange' || kv.key === 'onChangeText') {
textInputProps['onChangeText'] = kv.val;
}
}
});
return (
<TextInput
{ ...textInputProps }
onBlur={(e) => {
if (this.props.onBlur) {
this.props.onBlur(e);
{...textInputProps}
onBlur={(e) => {
if (this.props.onBlur) {
this.props.onBlur(e);
}
if (this.props.textInputProps && this.props.textInputProps.onBlur) {
this.props.textInputProps.onBlur(e);
}

if ((typeof (this.props.multi) == 'undefined') || (typeof (this.props.multi) != 'undefined' && this.props.multi == false)) {
this.setState({ focus: false, item: this.props.selectedItems[0] });
} else {
this.setState({ focus: false, item: this.props.selectedItems });
}

}
if (this.props.textInputProps && this.props.textInputProps.onBlur) {
this.props.textInputProps.onBlur(e);
}
this.setState({ focus: false, item: this.props.selectedItems });
}
}
/>
)
}
Expand All @@ -228,38 +243,38 @@ export default class SearchableDropDown extends Component {
keyboardShouldPersist="always"
style={{ ...this.props.containerStyle }}
>
{ this.renderSelectedItems() }
{ this.renderTextInput() }
{this.renderSelectedItems()}
{this.renderTextInput()}
{this.renderListType()}
</View>
);
};
renderSelectedItems(){
renderSelectedItems() {
let items = this.props.selectedItems || [];
if(items !== undefined && items.length > 0 && this.props.chip && this.props.multi){
return <View style={{flexDirection: 'row', flexWrap: 'wrap', paddingBottom: 10, marginTop: 5 }}>
{ items.map((item, index) => {
return (
<View key={index} style={{
width: (item.name.length * 8) + 60,
justifyContent: 'center',
flex: 0,
backgroundColor: '#eee',
flexDirection: 'row',
alignItems: 'center',
margin: 5,
padding: 8,
borderRadius: 15,
}}>
<Text style={{ color: '#555' }}>{item.name}</Text>
<TouchableOpacity onPress={() => setTimeout(() => { this.props.onRemoveItem(item, index) }, 0) } style={{ backgroundColor: '#f16d6b', alignItems: 'center', justifyContent: 'center', width: 25, height: 25, borderRadius: 100, marginLeft: 10}}>
<Text>X</Text>
</TouchableOpacity>
</View>
)
})
}
</View>
if (items !== undefined && items.length > 0 && this.props.chip && this.props.multi) {
return <View style={{ flexDirection: 'row', flexWrap: 'wrap', paddingBottom: 10, marginTop: 5 }}>
{items.map((item, index) => {
return (
<View key={index} style={{
width: (item.name.length * 8) + 60,
justifyContent: 'center',
flex: 0,
backgroundColor: '#eee',
flexDirection: 'row',
alignItems: 'center',
margin: 5,
padding: 8,
borderRadius: 15,
}}>
<Text style={{ color: '#555' }}>{item.name}</Text>
<TouchableOpacity onPress={() => setTimeout(() => { this.props.onRemoveItem(item, index) }, 0)} style={{ backgroundColor: '#f16d6b', alignItems: 'center', justifyContent: 'center', width: 25, height: 25, borderRadius: 100, marginLeft: 10 }}>
<Text>X</Text>
</TouchableOpacity>
</View>
)
})
}
</View>
}
}
}
}