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

Add Search Input on select.component.tsx #1834

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
64 changes: 57 additions & 7 deletions src/components/ui/select/select.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
TextStyle,
View,
ViewProps,
TextInput,
} from 'react-native';
import {
ChildrenWithProps,
Expand Down Expand Up @@ -75,6 +76,8 @@ export type SelectElement = React.ReactElement<SelectProps>;

interface State {
listVisible: boolean;
searchQuery: string;
filteredOptions: string[];
}

const CHEVRON_DEG_COLLAPSED = -180;
Expand Down Expand Up @@ -219,6 +222,8 @@ export class Select extends React.Component<SelectProps, State> {

public state: State = {
listVisible: false,
searchQuery: '',
filteredOptions: []
};

private service: SelectService = new SelectService();
Expand All @@ -229,9 +234,16 @@ export class Select extends React.Component<SelectProps, State> {
}

private get data(): Array<Exclude<ReactNode, boolean | null | undefined>> {
return React.Children.toArray(this.props.children || []);
const options = React.Children.toArray(this.props.children || []);
if (this.state.searchQuery) {
return options.filter((option) =>
option.toString().toLowerCase().includes(this.state.searchQuery.toLowerCase())
);
}
return options;
}


private get selectedIndices(): IndexPath[] {
if (!this.props.selectedIndex) {
return [];
Expand All @@ -258,10 +270,25 @@ export class Select extends React.Component<SelectProps, State> {
return this.state.listVisible;
};


public onSearch = (): void => {
const { searchQuery, filteredOptions } = this.state;
const filtered = filteredOptions.filter((option: string) =>
option.toLowerCase().includes(searchQuery.toLowerCase())
);
this.setState({ filteredOptions: filtered });
};


public handleSearch = (query: string): void => {
this.setState({ searchQuery: query }, this.onSearch);
};

public clear = (): void => {
this.props.onSelect?.(null);
};


private onMouseEnter = (event: NativeSyntheticEvent<TargetedEvent>): void => {
this.props.eva.dispatch([Interaction.HOVER]);
this.props.onMouseEnter?.(event);
Expand Down Expand Up @@ -305,6 +332,7 @@ export class Select extends React.Component<SelectProps, State> {
});
};


private onListInvisible = (): void => {
this.props.eva.dispatch([]);
this.createExpandAnimation(CHEVRON_DEG_EXPANDED).start(() => {
Expand Down Expand Up @@ -473,6 +501,20 @@ export class Select extends React.Component<SelectProps, State> {
);
};

private renderSearchInput = (): React.ReactElement => {
return (
<TextInput
style={styles.searchInput}
placeholder="Search..."
value={this.state.searchQuery}
onChangeText={this.handleSearch}
/>
);
};




public render(): React.ReactElement<ViewProps> {
const { eva, style, label, caption, children, ...touchableProps } = this.props;
const evaStyle = this.getComponentStyle(eva.style);
Expand All @@ -490,12 +532,15 @@ export class Select extends React.Component<SelectProps, State> {
anchor={() => this.renderInputElement(touchableProps, evaStyle)}
onBackdropPress={this.onBackdropPress}
>
<List
style={styles.list}
data={this.data}
bounces={false}
renderItem={this.renderItem}
/>
<>
{this.renderSearchInput()}
<List
style={styles.list}
data={this.data}
bounces={false}
renderItem={this.renderItem}
/>
</>
</Popover>
<FalsyText
style={[styles.caption, evaStyle.caption]}
Expand Down Expand Up @@ -528,4 +573,9 @@ const styles = StyleSheet.create({
caption: {
textAlign: 'left',
},
searchInput: {
padding: 10,
borderBottomWidth: 1,
borderBottomColor: '#e0e0e0',
},
});