@rn-components-dropdown/dropdown 1.0.34 → 1.0.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -113,15 +113,13 @@ export default function FormScreen() {
|
|
|
113
113
|
<DropdownController
|
|
114
114
|
control={control}
|
|
115
115
|
controller="fruit"
|
|
116
|
-
|
|
117
|
-
data: [
|
|
116
|
+
data={[
|
|
118
117
|
{ id: 1, label: "Apple" },
|
|
119
118
|
{ id: 2, label: "Banana" },
|
|
120
|
-
]
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}}
|
|
119
|
+
]}
|
|
120
|
+
labelField="label"
|
|
121
|
+
valueField= "id"
|
|
122
|
+
placeholder= "Pick a fruit"
|
|
125
123
|
/>
|
|
126
124
|
);
|
|
127
125
|
}
|
|
@@ -136,8 +134,7 @@ export default function FormScreen() {
|
|
|
136
134
|
| control | Control | React Hook Form control object |
|
|
137
135
|
| controller | string | Field name in the form |
|
|
138
136
|
| rules | object | Validation rules (optional) |
|
|
139
|
-
| dropProps
|
|
140
|
-
| defaultValue | any | Initial/default value |
|
|
137
|
+
| ...dropProps | object | All dropdown props |
|
|
141
138
|
|
|
142
139
|
---
|
|
143
140
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { View, Modal, FlatList, StyleSheet, ScrollView, TouchableHighlight, TouchableWithoutFeedback, } from 'react-native';
|
|
2
|
-
import _ from 'lodash';
|
|
3
2
|
import AppText from '../text/AppText';
|
|
4
3
|
import Chip from './component/chip/Chip';
|
|
5
4
|
import React, { useCallback } from 'react';
|
|
@@ -24,7 +23,7 @@ const DropdownComponent = (props) => {
|
|
|
24
23
|
])}/>);
|
|
25
24
|
};
|
|
26
25
|
const _renderDropdown = () => {
|
|
27
|
-
const isSelected = currentValue &&
|
|
26
|
+
const isSelected = currentValue && currentValue?.[valueField];
|
|
28
27
|
return (<TouchableWithoutFeedback testID={testID} accessible={!!accessibilityLabel} accessibilityLabel={accessibilityLabel} onPress={() => (selectionType === 'single' ? showOrClose() : null)}>
|
|
29
28
|
<View style={styles.dropdown}>
|
|
30
29
|
{renderLeftIcon?.(visible)}
|
|
@@ -33,9 +32,7 @@ const DropdownComponent = (props) => {
|
|
|
33
32
|
isSelected !== null ? selectedTextStyle : placeholderStyle,
|
|
34
33
|
font(),
|
|
35
34
|
]} {...selectedTextProps}>
|
|
36
|
-
{isSelected !== null
|
|
37
|
-
? _.get(currentValue, labelField)
|
|
38
|
-
: placeholder}
|
|
35
|
+
{isSelected !== null ? currentValue?.[labelField] : placeholder}
|
|
39
36
|
</AppText>) : currentValue?.length === 0 ? (<AppText style={[styles.textItem, placeholderStyle, font()]} {...selectedTextProps}>
|
|
40
37
|
{placeholder}
|
|
41
38
|
</AppText>) : (<View style={styles.showMultiSelectContainer}>
|
|
@@ -58,8 +55,9 @@ const DropdownComponent = (props) => {
|
|
|
58
55
|
</View>);
|
|
59
56
|
}, [styles]);
|
|
60
57
|
const renderCheckItem = useCallback(({ item, index }) => {
|
|
61
|
-
const isSelected =
|
|
62
|
-
return (<TouchableHighlight key={index.toString()} testID={
|
|
58
|
+
const isSelected = currentValue.some((h) => h?.[valueField] === item?.[valueField]);
|
|
59
|
+
return (<TouchableHighlight key={index.toString()} testID={String(item?.[itemTestIDField] || item?.[labelField])} accessible={!!accessibilityLabel} accessibilityLabel={(item?.[itemAccessibilityLabelField] ||
|
|
60
|
+
item?.[labelField])} underlayColor={activeColor} onPress={() => onCheckPress(item)}>
|
|
63
61
|
<View style={StyleSheet.flatten([itemContainerStyle])}>
|
|
64
62
|
{renderItem ? (renderItem(item, isSelected, labelField, valueField)) : (<View style={styles.item}>
|
|
65
63
|
<SvgButton icon={multiSelectIcon ? (multiSelectIcon) : (<CheckIcon color={colors.white}/>)} onPress={() => onCheckPress(item)} style={[
|
|
@@ -77,7 +75,7 @@ const DropdownComponent = (props) => {
|
|
|
77
75
|
itemTextStyle,
|
|
78
76
|
font(),
|
|
79
77
|
])}>
|
|
80
|
-
{
|
|
78
|
+
{String(item?.[labelField])}
|
|
81
79
|
</AppText>
|
|
82
80
|
</View>)}
|
|
83
81
|
</View>
|
|
@@ -101,10 +99,11 @@ const DropdownComponent = (props) => {
|
|
|
101
99
|
onCheckPress,
|
|
102
100
|
]);
|
|
103
101
|
const _renderItem = useCallback(({ item, index }) => {
|
|
104
|
-
const isSelected = currentValue &&
|
|
105
|
-
const selected =
|
|
106
|
-
|
|
107
|
-
return (<TouchableHighlight key={index.toString()} testID={
|
|
102
|
+
const isSelected = currentValue && currentValue?.[valueField];
|
|
103
|
+
const selected = item?.[valueField] === isSelected;
|
|
104
|
+
Object.assign(item, { _index: index });
|
|
105
|
+
return (<TouchableHighlight key={index.toString()} testID={String(item?.[itemTestIDField] || item?.[labelField])} accessible={!!accessibilityLabel} accessibilityLabel={(item?.[itemAccessibilityLabelField] ||
|
|
106
|
+
item?.[labelField])} underlayColor={activeColor} onPress={() => onSelect(item)}>
|
|
108
107
|
<View style={StyleSheet.flatten([itemContainerStyle])}>
|
|
109
108
|
{renderItem ? (renderItem(item, selected, labelField, valueField)) : (<View style={styles.item}>
|
|
110
109
|
{renderRadio(selected)}
|
|
@@ -113,7 +112,7 @@ const DropdownComponent = (props) => {
|
|
|
113
112
|
itemTextStyle,
|
|
114
113
|
font(),
|
|
115
114
|
])}>
|
|
116
|
-
{
|
|
115
|
+
{String(item?.[labelField])}
|
|
117
116
|
</AppText>
|
|
118
117
|
</View>)}
|
|
119
118
|
</View>
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/* eslint-disable react-hooks/exhaustive-deps */
|
|
2
2
|
import { useRef, useState, useEffect, useCallback, useImperativeHandle, } from 'react';
|
|
3
3
|
import { Keyboard, StatusBar, I18nManager, } from 'react-native';
|
|
4
|
-
import _ from 'lodash';
|
|
5
4
|
import { useDetectDevice } from './useDetectDevice';
|
|
6
5
|
import { useDeviceOrientation } from './useOrientation';
|
|
7
6
|
import { SCREEN_HEIGHT, SCREEN_WIDTH } from '../../../style';
|
|
@@ -110,8 +109,10 @@ const useDropdown = (props) => {
|
|
|
110
109
|
return;
|
|
111
110
|
}
|
|
112
111
|
else {
|
|
113
|
-
const defaultValue = typeof value === 'object'
|
|
114
|
-
|
|
112
|
+
const defaultValue = typeof value === 'object'
|
|
113
|
+
? valueField && value?.[valueField]
|
|
114
|
+
: value;
|
|
115
|
+
const getItem = data.filter(e => JSON.stringify(defaultValue) === JSON.stringify(e?.[valueField]));
|
|
115
116
|
if (getItem.length > 0) {
|
|
116
117
|
setCurrentValue(getItem[0]);
|
|
117
118
|
}
|
|
@@ -125,8 +126,8 @@ const useDropdown = (props) => {
|
|
|
125
126
|
if (autoScroll && data.length > 0 && listData.length === data.length) {
|
|
126
127
|
setTimeout(() => {
|
|
127
128
|
if (refList && refList?.current) {
|
|
128
|
-
const defaultValue = typeof value === 'object' ?
|
|
129
|
-
const index =
|
|
129
|
+
const defaultValue = typeof value === 'object' ? value?.[valueField] : value;
|
|
130
|
+
const index = listData.findIndex((e) => JSON.stringify(defaultValue) === JSON.stringify(e?.[valueField]));
|
|
130
131
|
if (index > -1 && index <= listData.length - 1) {
|
|
131
132
|
refList?.current?.scrollToIndex({
|
|
132
133
|
index: index,
|
|
@@ -174,11 +175,12 @@ const useDropdown = (props) => {
|
|
|
174
175
|
const onSearch = useCallback((text) => {
|
|
175
176
|
if (text.length > 0) {
|
|
176
177
|
const defaultFilterFunction = (e) => {
|
|
177
|
-
const item =
|
|
178
|
-
?.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
178
|
+
const item = e?.[searchField] ||
|
|
179
|
+
e?.[labelField]
|
|
180
|
+
?.toLowerCase()
|
|
181
|
+
.replace(' ', '')
|
|
182
|
+
.normalize('NFD')
|
|
183
|
+
.replace(/[\u0300-\u036f]/g, '');
|
|
182
184
|
const key = text
|
|
183
185
|
.toLowerCase()
|
|
184
186
|
.replace(' ', '')
|
|
@@ -187,7 +189,7 @@ const useDropdown = (props) => {
|
|
|
187
189
|
return item.indexOf(key) >= 0;
|
|
188
190
|
};
|
|
189
191
|
const propSearchFunction = (e) => {
|
|
190
|
-
const labelText =
|
|
192
|
+
const labelText = e?.[searchField] || e?.[labelField];
|
|
191
193
|
return searchQuery?.(text, labelText);
|
|
192
194
|
};
|
|
193
195
|
const dataSearch = data.filter(searchQuery ? propSearchFunction : defaultFilterFunction);
|
|
@@ -221,7 +223,7 @@ const useDropdown = (props) => {
|
|
|
221
223
|
setCurrentValue?.((prev) => {
|
|
222
224
|
const exists = prev?.some?.((i) => i[valueField] === item[valueField]);
|
|
223
225
|
const va = exists
|
|
224
|
-
?
|
|
226
|
+
? prev?.filter?.(i => i?.[valueField] !== item?.[valueField])
|
|
225
227
|
: [...prev, item];
|
|
226
228
|
const v = va?.map(i => i[valueField]);
|
|
227
229
|
onChange?.(item[valueField], selectionType === 'multi' ? v : item[valueField]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-components-dropdown/dropdown",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.35",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"homepage": "https://www.npmjs.com/package/@rn-components-dropdown/dropdown",
|
|
@@ -34,7 +34,6 @@
|
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"description": "Package use for dropdown for single and multi selection with radio and checkbox",
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/lodash": "^4.17.20",
|
|
38
37
|
"@types/react": "^19.2.2",
|
|
39
38
|
"typescript": "^5.9.3"
|
|
40
39
|
},
|
|
@@ -44,7 +43,6 @@
|
|
|
44
43
|
"react-native-svg": ">=15.14.0"
|
|
45
44
|
},
|
|
46
45
|
"dependencies": {
|
|
47
|
-
"lodash": "^4.17.21",
|
|
48
46
|
"react-hook-form": "^7.66.0",
|
|
49
47
|
"react-native-svg": ">=15.14.0"
|
|
50
48
|
}
|
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
TouchableHighlight,
|
|
9
9
|
TouchableWithoutFeedback,
|
|
10
10
|
} from 'react-native';
|
|
11
|
-
import _ from 'lodash';
|
|
12
11
|
import AppText from '../text/AppText';
|
|
13
12
|
import Chip from './component/chip/Chip';
|
|
14
13
|
import React, { useCallback } from 'react';
|
|
@@ -118,7 +117,7 @@ const DropdownComponent = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
118
117
|
};
|
|
119
118
|
|
|
120
119
|
const _renderDropdown = () => {
|
|
121
|
-
const isSelected: boolean = currentValue &&
|
|
120
|
+
const isSelected: boolean = currentValue && currentValue?.[valueField];
|
|
122
121
|
return (
|
|
123
122
|
<TouchableWithoutFeedback
|
|
124
123
|
testID={testID}
|
|
@@ -137,9 +136,7 @@ const DropdownComponent = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
137
136
|
]}
|
|
138
137
|
{...selectedTextProps}
|
|
139
138
|
>
|
|
140
|
-
{isSelected !== null
|
|
141
|
-
? _.get(currentValue, labelField)
|
|
142
|
-
: placeholder}
|
|
139
|
+
{isSelected !== null ? currentValue?.[labelField] : placeholder}
|
|
143
140
|
</AppText>
|
|
144
141
|
) : currentValue?.length === 0 ? (
|
|
145
142
|
<AppText
|
|
@@ -188,18 +185,21 @@ const DropdownComponent = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
188
185
|
|
|
189
186
|
const renderCheckItem = useCallback(
|
|
190
187
|
({ item, index }: { item: T; index: number }) => {
|
|
191
|
-
const isSelected =
|
|
192
|
-
|
|
188
|
+
const isSelected = currentValue.some(
|
|
189
|
+
(h: any) => h?.[valueField] === item?.[valueField],
|
|
193
190
|
);
|
|
191
|
+
|
|
194
192
|
return (
|
|
195
193
|
<TouchableHighlight
|
|
196
194
|
key={index.toString()}
|
|
197
|
-
testID={
|
|
198
|
-
|
|
199
|
-
accessibilityLabel={_.get(
|
|
200
|
-
item,
|
|
201
|
-
itemAccessibilityLabelField || labelField,
|
|
195
|
+
testID={String(
|
|
196
|
+
item?.[itemTestIDField as keyof T] || item?.[labelField],
|
|
202
197
|
)}
|
|
198
|
+
accessible={!!accessibilityLabel}
|
|
199
|
+
accessibilityLabel={
|
|
200
|
+
(item?.[itemAccessibilityLabelField as keyof T] ||
|
|
201
|
+
item?.[labelField]) as string | undefined
|
|
202
|
+
}
|
|
203
203
|
underlayColor={activeColor}
|
|
204
204
|
onPress={() => onCheckPress(item)}
|
|
205
205
|
>
|
|
@@ -235,7 +235,7 @@ const DropdownComponent = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
235
235
|
font(),
|
|
236
236
|
])}
|
|
237
237
|
>
|
|
238
|
-
{
|
|
238
|
+
{String(item?.[labelField])}
|
|
239
239
|
</AppText>
|
|
240
240
|
</View>
|
|
241
241
|
)}
|
|
@@ -265,18 +265,20 @@ const DropdownComponent = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
265
265
|
|
|
266
266
|
const _renderItem = useCallback(
|
|
267
267
|
({ item, index }: { item: T; index: number }) => {
|
|
268
|
-
const isSelected = currentValue &&
|
|
269
|
-
const selected =
|
|
270
|
-
|
|
268
|
+
const isSelected = currentValue && currentValue?.[valueField];
|
|
269
|
+
const selected = item?.[valueField] === isSelected;
|
|
270
|
+
Object.assign(item as object, { _index: index });
|
|
271
271
|
return (
|
|
272
272
|
<TouchableHighlight
|
|
273
273
|
key={index.toString()}
|
|
274
|
-
testID={
|
|
275
|
-
|
|
276
|
-
accessibilityLabel={_.get(
|
|
277
|
-
item,
|
|
278
|
-
itemAccessibilityLabelField || labelField,
|
|
274
|
+
testID={String(
|
|
275
|
+
item?.[itemTestIDField as keyof T] || item?.[labelField],
|
|
279
276
|
)}
|
|
277
|
+
accessible={!!accessibilityLabel}
|
|
278
|
+
accessibilityLabel={
|
|
279
|
+
(item?.[itemAccessibilityLabelField as keyof T] ||
|
|
280
|
+
item?.[labelField]) as string | undefined
|
|
281
|
+
}
|
|
280
282
|
underlayColor={activeColor}
|
|
281
283
|
onPress={() => onSelect(item)}
|
|
282
284
|
>
|
|
@@ -293,7 +295,7 @@ const DropdownComponent = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
293
295
|
font(),
|
|
294
296
|
])}
|
|
295
297
|
>
|
|
296
|
-
{
|
|
298
|
+
{String(item?.[labelField])}
|
|
297
299
|
</AppText>
|
|
298
300
|
</View>
|
|
299
301
|
)}
|
|
@@ -14,7 +14,6 @@ import {
|
|
|
14
14
|
I18nManager,
|
|
15
15
|
KeyboardEvent,
|
|
16
16
|
} from 'react-native';
|
|
17
|
-
import _ from 'lodash';
|
|
18
17
|
import { useDetectDevice } from './useDetectDevice';
|
|
19
18
|
import { useDeviceOrientation } from './useOrientation';
|
|
20
19
|
import { SCREEN_HEIGHT, SCREEN_WIDTH } from '../../../style';
|
|
@@ -181,10 +180,12 @@ const useDropdown = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
181
180
|
return;
|
|
182
181
|
} else {
|
|
183
182
|
const defaultValue =
|
|
184
|
-
typeof value === 'object'
|
|
183
|
+
typeof value === 'object'
|
|
184
|
+
? valueField && value?.[valueField as any]
|
|
185
|
+
: value;
|
|
185
186
|
|
|
186
|
-
const getItem = data.filter(
|
|
187
|
-
|
|
187
|
+
const getItem = data.filter(
|
|
188
|
+
e => JSON.stringify(defaultValue) === JSON.stringify(e?.[valueField]),
|
|
188
189
|
);
|
|
189
190
|
if (getItem.length > 0) {
|
|
190
191
|
setCurrentValue(getItem[0]);
|
|
@@ -201,10 +202,11 @@ const useDropdown = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
201
202
|
setTimeout(() => {
|
|
202
203
|
if (refList && refList?.current) {
|
|
203
204
|
const defaultValue =
|
|
204
|
-
typeof value === 'object' ?
|
|
205
|
+
typeof value === 'object' ? (value as T)?.[valueField] : value;
|
|
205
206
|
|
|
206
|
-
const index =
|
|
207
|
-
|
|
207
|
+
const index = listData.findIndex(
|
|
208
|
+
(e: any) =>
|
|
209
|
+
JSON.stringify(defaultValue) === JSON.stringify(e?.[valueField]),
|
|
208
210
|
);
|
|
209
211
|
if (index > -1 && index <= listData.length - 1) {
|
|
210
212
|
refList?.current?.scrollToIndex({
|
|
@@ -256,11 +258,13 @@ const useDropdown = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
256
258
|
(text: string) => {
|
|
257
259
|
if (text.length > 0) {
|
|
258
260
|
const defaultFilterFunction = (e: any) => {
|
|
259
|
-
const item =
|
|
260
|
-
?.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
261
|
+
const item =
|
|
262
|
+
e?.[searchField] ||
|
|
263
|
+
e?.[labelField]
|
|
264
|
+
?.toLowerCase()
|
|
265
|
+
.replace(' ', '')
|
|
266
|
+
.normalize('NFD')
|
|
267
|
+
.replace(/[\u0300-\u036f]/g, '');
|
|
264
268
|
const key = text
|
|
265
269
|
.toLowerCase()
|
|
266
270
|
.replace(' ', '')
|
|
@@ -271,7 +275,7 @@ const useDropdown = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
271
275
|
};
|
|
272
276
|
|
|
273
277
|
const propSearchFunction = (e: any) => {
|
|
274
|
-
const labelText =
|
|
278
|
+
const labelText = e?.[searchField] || e?.[labelField];
|
|
275
279
|
|
|
276
280
|
return searchQuery?.(text, labelText);
|
|
277
281
|
};
|
|
@@ -319,7 +323,7 @@ const useDropdown = <T, Mode extends SELECTIONS_TYPE>(
|
|
|
319
323
|
(i: any) => i[valueField] === item[valueField],
|
|
320
324
|
);
|
|
321
325
|
const va: T[] = exists
|
|
322
|
-
?
|
|
326
|
+
? prev?.filter?.(i => i?.[valueField] !== item?.[valueField])
|
|
323
327
|
: [...prev, item];
|
|
324
328
|
const v = va?.map(i => i[valueField]);
|
|
325
329
|
onChange?.(
|