From 7f97b2501b45201bf3636f3874f7bd85c173bd07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0=20Phan?= Date: Sun, 5 Mar 2023 21:11:32 +0700 Subject: [PATCH] feat: add props searchField --- example/src/dropdown/Dropdown1.tsx | 17 +++++++++-------- package.json | 5 ----- src/components/Dropdown/index.tsx | 7 ++++--- src/components/Dropdown/model.ts | 1 + src/components/MultiSelect/index.tsx | 7 ++++--- src/components/MultiSelect/model.ts | 1 + 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/example/src/dropdown/Dropdown1.tsx b/example/src/dropdown/Dropdown1.tsx index dc591ab..f90b235 100644 --- a/example/src/dropdown/Dropdown1.tsx +++ b/example/src/dropdown/Dropdown1.tsx @@ -4,14 +4,14 @@ import { StyleSheet, Text, View } from 'react-native'; import { Dropdown } from 'react-native-element-dropdown'; const data = [ - { label: 'Item 1', value: '1' }, - { label: 'Item 2', value: '2' }, - { label: 'Item 3', value: '3' }, - { label: 'Item 4', value: '4' }, - { label: 'Item 5', value: '5' }, - { label: 'Item 6', value: '6' }, - { label: 'Item 7', value: '7' }, - { label: 'Item 8', value: '8' }, + { label: 'Item 1', value: '1', search: 'a' }, + { label: 'Item 2', value: '2', search: 'b' }, + { label: 'Item 3', value: '3', search: 'c' }, + { label: 'Item 4', value: '4', search: 'd' }, + { label: 'Item 5', value: '5', search: 'e' }, + { label: 'Item 6', value: '6', search: 'f' }, + { label: 'Item 7', value: '7', search: 'g' }, + { label: 'Item 8', value: '8', search: 'h' }, ]; const DropdownComponent = () => { @@ -44,6 +44,7 @@ const DropdownComponent = () => { minHeight={100} labelField="label" valueField="value" + searchField="search" placeholder={!isFocus ? 'Dropdown 1' : '...'} searchPlaceholder="Search..." value={value} diff --git a/package.json b/package.json index 21c9235..7372e05 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,8 @@ "files": [ "src", "lib", - "android", - "ios", "cpp", - "react-native-element-dropdown.podspec", "!lib/typescript/example", - "!android/build", - "!ios/build", "!**/__tests__", "!**/__fixtures__", "!**/__mocks__" diff --git a/src/components/Dropdown/index.tsx b/src/components/Dropdown/index.tsx index 5b4e1d3..6199ce8 100644 --- a/src/components/Dropdown/index.tsx +++ b/src/components/Dropdown/index.tsx @@ -57,6 +57,7 @@ const DropdownComponent: ( data = [], labelField, valueField, + searchField, value, activeColor = '#F6F7F8', fontFamily, @@ -296,7 +297,7 @@ const DropdownComponent: ( (text: string) => { if (text.length > 0) { const defaultFilterFunction = (e: any) => { - const item = _.get(e, labelField) + const item = _.get(e, searchField || labelField) ?.toLowerCase() .replace(' ', '') .normalize('NFD') @@ -311,7 +312,7 @@ const DropdownComponent: ( }; const propSearchFunction = (e: any) => { - const labelText = _.get(e, labelField); + const labelText = _.get(e, searchField || labelField); return searchQuery?.(text, labelText); }; @@ -324,7 +325,7 @@ const DropdownComponent: ( setListData(data); } }, - [data, labelField, searchQuery] + [data, searchField, labelField, searchQuery] ); const onSelect = useCallback( diff --git a/src/components/Dropdown/model.ts b/src/components/Dropdown/model.ts index 26fb233..63bb464 100644 --- a/src/components/Dropdown/model.ts +++ b/src/components/Dropdown/model.ts @@ -39,6 +39,7 @@ export interface DropdownProps { placeholder?: string; labelField: string; valueField: string; + searchField?: string; search?: boolean; searchPlaceholder?: string; disable?: boolean; diff --git a/src/components/MultiSelect/index.tsx b/src/components/MultiSelect/index.tsx index 1b70ba3..a9362d9 100644 --- a/src/components/MultiSelect/index.tsx +++ b/src/components/MultiSelect/index.tsx @@ -49,6 +49,7 @@ const MultiSelectComponent: ( style = {}, labelField, valueField, + searchField, selectedStyle, selectedTextStyle, itemContainerStyle, @@ -262,7 +263,7 @@ const MultiSelectComponent: ( (text: string) => { if (text.length > 0) { const defaultFilterFunction = (e: any) => { - const item = _.get(e, labelField) + const item = _.get(e, searchField || labelField) ?.toLowerCase() .replace(' ', '') .normalize('NFD') @@ -277,7 +278,7 @@ const MultiSelectComponent: ( }; const propSearchFunction = (e: any) => { - const labelText = _.get(e, labelField); + const labelText = _.get(e, searchField || labelField); return searchQuery?.(text, labelText); }; @@ -290,7 +291,7 @@ const MultiSelectComponent: ( setListData(data); } }, - [data, labelField, searchQuery] + [data, searchField, labelField, searchQuery] ); const onSelect = useCallback( diff --git a/src/components/MultiSelect/model.ts b/src/components/MultiSelect/model.ts index 1f19851..77c20db 100644 --- a/src/components/MultiSelect/model.ts +++ b/src/components/MultiSelect/model.ts @@ -39,6 +39,7 @@ export interface MultiSelectProps { placeholder?: string; labelField: string; valueField: string; + searchField?: string; search?: boolean; disable?: boolean; showsVerticalScrollIndicator?: boolean;