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

Change dataset prop names #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import {
Keyboard
} from 'react-native';

const defaultItemValue = {
name: '', id: 0
};

export default class SearchableDropDown extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -66,13 +62,17 @@ export default class SearchableDropDown extends Component {
} else {
this.setState({ listItems });
}
this.setState({
idValue: this.props.idValue || 'id',
nameValue: this.props.nameValue || 'name'
});
};

searchedItems = searchedText => {
let setSort = this.props.setSort;
if (!setSort && typeof setSort !== 'function') {
setSort = (item, searchedText) => {
return item.name.toLowerCase().indexOf(searchedText.toLowerCase()) > -1
return item[this.state.nameValue].toLowerCase().indexOf(searchedText.toLowerCase()) > -1
};
}
var ac = this.props.items.filter((item) => {
Expand All @@ -94,11 +94,11 @@ export default class SearchableDropDown extends Component {
renderItems = (item, index) => {
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[this.state.idValue] === item[this.state.idValue])
?
<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[this.state.nameValue] }</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}}>
Expand All @@ -116,7 +116,7 @@ export default class SearchableDropDown extends Component {
}}
style={{ ...this.props.itemStyle, flex: 1, flexDirection: 'row' }}>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'flex-start' }}>
<Text>{ item.name }</Text>
<Text>{ item[this.state.nameValue] }</Text>
</View>
</TouchableOpacity>
)
Expand All @@ -130,18 +130,18 @@ export default class SearchableDropDown extends Component {
setTimeout(() => {
this.props.onItemSelect(item);
if (this.props.resetValue) {
this.setState({ focus: true, item: defaultItemValue });
this.setState({ focus: true, item: {[this.state.nameValue]: '', [this.state.idValue]: 0} });
this.input.focus();
}
}, 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[this.state.idValue] === item[this.state.idValue])
?
<Text style={{ ...this.props.itemTextStyle }}>{item.name}</Text>
<Text style={{ ...this.props.itemTextStyle }}>{item[this.state.nameValue]}</Text>
:
<Text style={{ ...this.props.itemTextStyle }}>{item.name}</Text>
<Text style={{ ...this.props.itemTextStyle }}>{item[this.state.nameValue]}</Text>
}
</TouchableOpacity>
);
Expand All @@ -164,7 +164,7 @@ export default class SearchableDropDown extends Component {
this.props.onFocus && this.props.onFocus()
this.setState({
focus: true,
item: defaultItemValue,
item: {[this.state.nameValue]: '', [this.state.idValue]: 0},
listItems: this.props.items
});
}
Expand All @@ -178,7 +178,7 @@ export default class SearchableDropDown extends Component {
},
{
key: 'value',
val: this.state.item.name
val: this.state.item[this.state.nameValue]
},
{
key: 'style',
Expand Down Expand Up @@ -232,7 +232,7 @@ export default class SearchableDropDown extends Component {
{ items.map((item, index) => {
return (
<View key={index} style={{
width: (item.name.length * 8) + 60,
width: (item[this.state.nameValue].length * 8) + 60,
justifyContent: 'center',
flex: 0,
backgroundColor: '#eee',
Expand All @@ -242,7 +242,7 @@ export default class SearchableDropDown extends Component {
padding: 8,
borderRadius: 15,
}}>
<Text style={{ color: '#555' }}>{item.name}</Text>
<Text style={{ color: '#555' }}>{item[this.state.nameValue]}</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>
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ npm install --save react-native-searchable-dropdown
<td>onRemoveItem</td>
<td>{ (item, index) => { } } note: work when if multi prop is true</td>
</tr>
<tr>
<td>idValue</td>
<td>prop name of the dataset id value, default 'id'</td>
</tr>
<tr>
<td>nameValue</td>
<td>prop name of the dataset name value, default 'name'</td>
</tr>

</table>

# Example
Expand Down