react-native-input-select 1.1.1 → 1.1.3

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.
Files changed (51) hide show
  1. package/README.md +18 -0
  2. package/lib/commonjs/components/CheckBox/index.js +20 -9
  3. package/lib/commonjs/components/CheckBox/index.js.map +1 -1
  4. package/lib/commonjs/components/CheckBox/types.js.map +1 -1
  5. package/lib/commonjs/components/Dropdown/Dropdown.js +4 -2
  6. package/lib/commonjs/components/Dropdown/Dropdown.js.map +1 -1
  7. package/lib/commonjs/components/Dropdown/DropdownFlatList.js +34 -6
  8. package/lib/commonjs/components/Dropdown/DropdownFlatList.js.map +1 -1
  9. package/lib/commonjs/components/Dropdown/DropdownListItem.js +4 -2
  10. package/lib/commonjs/components/Dropdown/DropdownListItem.js.map +1 -1
  11. package/lib/commonjs/components/Dropdown/DropdownSectionList.js +29 -2
  12. package/lib/commonjs/components/Dropdown/DropdownSectionList.js.map +1 -1
  13. package/lib/commonjs/components/Dropdown/DropdownSelectedItemsView.js +41 -9
  14. package/lib/commonjs/components/Dropdown/DropdownSelectedItemsView.js.map +1 -1
  15. package/lib/commonjs/index.js +64 -16
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/commonjs/types/index.types.js.map +1 -1
  18. package/lib/module/components/CheckBox/index.js +20 -8
  19. package/lib/module/components/CheckBox/index.js.map +1 -1
  20. package/lib/module/components/CheckBox/types.js.map +1 -1
  21. package/lib/module/components/Dropdown/Dropdown.js +4 -2
  22. package/lib/module/components/Dropdown/Dropdown.js.map +1 -1
  23. package/lib/module/components/Dropdown/DropdownFlatList.js +32 -6
  24. package/lib/module/components/Dropdown/DropdownFlatList.js.map +1 -1
  25. package/lib/module/components/Dropdown/DropdownListItem.js +4 -2
  26. package/lib/module/components/Dropdown/DropdownListItem.js.map +1 -1
  27. package/lib/module/components/Dropdown/DropdownSectionList.js +30 -3
  28. package/lib/module/components/Dropdown/DropdownSectionList.js.map +1 -1
  29. package/lib/module/components/Dropdown/DropdownSelectedItemsView.js +42 -10
  30. package/lib/module/components/Dropdown/DropdownSelectedItemsView.js.map +1 -1
  31. package/lib/module/index.js +65 -17
  32. package/lib/module/index.js.map +1 -1
  33. package/lib/module/types/index.types.js.map +1 -1
  34. package/lib/typescript/components/CheckBox/index.d.ts +17 -5
  35. package/lib/typescript/components/CheckBox/types.d.ts +2 -0
  36. package/lib/typescript/components/Dropdown/Dropdown.d.ts +1 -1
  37. package/lib/typescript/components/Dropdown/DropdownFlatList.d.ts +1 -1
  38. package/lib/typescript/components/Dropdown/DropdownListItem.d.ts +1 -1
  39. package/lib/typescript/components/Dropdown/DropdownSectionList.d.ts +1 -1
  40. package/lib/typescript/components/Dropdown/DropdownSelectedItemsView.d.ts +1 -1
  41. package/lib/typescript/types/index.types.d.ts +1 -0
  42. package/package.json +2 -2
  43. package/src/components/CheckBox/index.tsx +42 -22
  44. package/src/components/CheckBox/types.ts +2 -0
  45. package/src/components/Dropdown/Dropdown.tsx +3 -0
  46. package/src/components/Dropdown/DropdownFlatList.tsx +27 -1
  47. package/src/components/Dropdown/DropdownListItem.tsx +2 -0
  48. package/src/components/Dropdown/DropdownSectionList.tsx +30 -1
  49. package/src/components/Dropdown/DropdownSelectedItemsView.tsx +32 -13
  50. package/src/index.tsx +64 -18
  51. package/src/types/index.types.ts +1 -0
@@ -1,3 +1,4 @@
1
+ import React from 'react';
1
2
  import type { ColorValue, ViewStyle, TextStyle } from 'react-native';
2
3
  export type CheckboxProps = {
3
4
  label?: string;
@@ -12,5 +13,6 @@ export type CheckboxProps = {
12
13
  checkboxStyle?: ViewStyle;
13
14
  checkboxLabelStyle?: TextStyle;
14
15
  };
16
+ checkboxComponent?: React.ReactNode;
15
17
  onChange?: (value: boolean | string | number) => void;
16
18
  };
@@ -1,3 +1,3 @@
1
1
  /// <reference types="react" />
2
- declare const Dropdown: ({ label, placeholder, helperText, error, getSelectedItemsLabel, handleToggleModal, isMultiple, selectedItem, selectedItems, dropdownIcon, labelStyle, dropdownStyle, dropdownIconStyle, dropdownContainerStyle, selectedItemStyle, placeholderStyle, multipleSelectedItemStyle, dropdownErrorStyle, dropdownErrorTextStyle, dropdownHelperTextStyle, primaryColor, disabled, }: any) => JSX.Element;
2
+ declare const Dropdown: ({ label, placeholder, helperText, error, getSelectedItemsLabel, handleToggleModal, isMultiple, selectedItem, selectedItems, dropdownIcon, labelStyle, dropdownStyle, dropdownIconStyle, dropdownContainerStyle, selectedItemStyle, placeholderStyle, multipleSelectedItemStyle, dropdownErrorStyle, dropdownErrorTextStyle, dropdownHelperTextStyle, primaryColor, disabled, setIndexOfSelectedItem, }: any) => JSX.Element;
3
3
  export default Dropdown;
@@ -1,3 +1,3 @@
1
1
  /// <reference types="react" />
2
- declare const DropdownFlatList: ({ options, optionLabel, optionValue, isMultiple, isSearchable, selectedItems, selectedItem, handleMultipleSelections, handleSingleSelection, primaryColor, checkboxSize, checkboxStyle, checkboxLabelStyle, checkboxComponentStyles, listComponentStyles, ...rest }: any) => JSX.Element;
2
+ declare const DropdownFlatList: ({ options, optionLabel, optionValue, isMultiple, isSearchable, selectedItems, selectedItem, handleMultipleSelections, handleSingleSelection, primaryColor, checkboxSize, checkboxStyle, checkboxLabelStyle, checkboxComponentStyles, checkboxComponent, listComponentStyles, listIndex, ...rest }: any) => JSX.Element;
3
3
  export default DropdownFlatList;
@@ -1,3 +1,3 @@
1
1
  import React from 'react';
2
- declare const _default: React.MemoExoticComponent<({ item, optionLabel, optionValue, isMultiple, selectedOption, onChange, primaryColor, checkboxSize, checkboxStyle, checkboxLabelStyle, checkboxComponentStyles, }: any) => JSX.Element>;
2
+ declare const _default: React.MemoExoticComponent<({ item, optionLabel, optionValue, isMultiple, selectedOption, onChange, primaryColor, checkboxSize, checkboxStyle, checkboxLabelStyle, checkboxComponentStyles, checkboxComponent, }: any) => JSX.Element>;
3
3
  export default _default;
@@ -1,3 +1,3 @@
1
1
  /// <reference types="react" />
2
- declare const DropdownSectionList: ({ options, optionLabel, optionValue, isMultiple, isSearchable, selectedItems, selectedItem, handleMultipleSelections, handleSingleSelection, primaryColor, checkboxSize, checkboxStyle, checkboxLabelStyle, checkboxComponentStyles, listComponentStyles, ...rest }: any) => JSX.Element;
2
+ declare const DropdownSectionList: ({ options, optionLabel, optionValue, isMultiple, isSearchable, selectedItems, selectedItem, handleMultipleSelections, handleSingleSelection, primaryColor, checkboxSize, checkboxStyle, checkboxLabelStyle, checkboxComponentStyles, checkboxComponent, listComponentStyles, listIndex, ...rest }: any) => JSX.Element;
3
3
  export default DropdownSectionList;
@@ -1,3 +1,3 @@
1
1
  /// <reference types="react" />
2
- declare const DropdownSelectedItemsView: ({ placeholder, error, getSelectedItemsLabel, handleToggleModal, isMultiple, selectedItem, selectedItems, dropdownIcon, dropdownStyle, dropdownIconStyle, selectedItemStyle, placeholderStyle, multipleSelectedItemStyle, dropdownErrorStyle, primaryColor, disabled, }: any) => JSX.Element;
2
+ declare const DropdownSelectedItemsView: ({ placeholder, error, getSelectedItemsLabel, handleToggleModal, isMultiple, selectedItem, selectedItems, dropdownIcon, dropdownStyle, dropdownIconStyle, selectedItemStyle, placeholderStyle, multipleSelectedItemStyle, dropdownErrorStyle, primaryColor, disabled, setIndexOfSelectedItem, }: any) => JSX.Element;
3
3
  export default DropdownSelectedItemsView;
@@ -35,6 +35,7 @@ export type DropdownProps = {
35
35
  checkboxStyle?: ViewStyle;
36
36
  checkboxLabelStyle?: TextStyle;
37
37
  };
38
+ checkboxComponent?: React.ReactNode;
38
39
  placeholderStyle?: TextStyle;
39
40
  listHeaderComponent?: React.ReactNode;
40
41
  listFooterComponent?: React.ReactNode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-input-select",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "A customizable dropdown selection package for react-native for android and iOS with multiple select and search capabilities.",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -26,7 +26,7 @@
26
26
  "typescript": "tsc --noEmit",
27
27
  "lint": "eslint \"**/*.{js,ts,tsx}\"",
28
28
  "prepare": "bob build",
29
- "release": "release-it -- patch --ci",
29
+ "release": "release-it --ci",
30
30
  "example": "yarn --cwd example",
31
31
  "pods": "cd example && pod-install --quiet",
32
32
  "bootstrap": "yarn example && yarn && yarn pods",
@@ -1,4 +1,3 @@
1
- /* eslint-disable react-native/no-inline-styles */
2
1
  import React from 'react';
3
2
  import { Pressable, Text, StyleSheet, Image, View } from 'react-native';
4
3
  import { colors } from '../../styles/colors';
@@ -6,10 +5,22 @@ import { CHECKBOX_SIZE } from '../../constants';
6
5
  import type { CheckboxProps } from './types';
7
6
 
8
7
  /**
9
- *
10
- *`checkboxSize`, `checkboxStyle`,`checkboxLabelStyle` Will be deleted in version 1.0.
11
- * and replaced with `checkboxComponentStyles`
12
- */
8
+ * Individual props `checkboxSize`, `checkboxStyle`, `checkboxLabelStyle` would be replaced in future releases
9
+ * and replaced with a single object `checkboxComponentStyles` e.g
10
+
11
+ ```js
12
+ const checkboxComponentStyles = {
13
+ checkboxSize: 20,
14
+ checkboxStyle: {
15
+ backgroundColor: 'purple',
16
+ borderRadius: 30,
17
+ padding: 10,
18
+ borderColor: 'red',
19
+ },
20
+ checkboxLabelStyle: { color: 'red', fontSize: 20 },
21
+ };
22
+ ```
23
+ */
13
24
 
14
25
  const CheckBox = ({
15
26
  label,
@@ -20,10 +31,11 @@ const CheckBox = ({
20
31
  checkboxStyle,
21
32
  checkboxLabelStyle,
22
33
  checkboxComponentStyles,
34
+ checkboxComponent,
23
35
  onChange,
24
36
  }: CheckboxProps) => {
25
37
  // const { checkboxSize, checkboxStyle, checkboxLabelStyle } =
26
- // checkboxComponentStyles;
38
+ // checkboxComponentStyles || undefined;
27
39
  const fillColor = {
28
40
  backgroundColor: disabled
29
41
  ? '#d3d3d3'
@@ -46,22 +58,30 @@ const CheckBox = ({
46
58
  style={[styles.checkboxContainer]}
47
59
  disabled={disabled}
48
60
  >
49
- <View style={[styles.checkbox, checkboxStyle, fillColor]}>
50
- <Image
51
- source={require('../../asset/check.png')}
52
- style={[
53
- {
54
- height:
55
- checkboxComponentStyles?.checkboxSize ||
56
- checkboxSize ||
57
- CHECKBOX_SIZE,
58
- width:
59
- checkboxComponentStyles?.checkboxSize ||
60
- checkboxSize ||
61
- CHECKBOX_SIZE,
62
- },
63
- ]}
64
- />
61
+ <View
62
+ style={[
63
+ styles.checkbox,
64
+ checkboxComponentStyles?.checkboxStyle || checkboxStyle,
65
+ fillColor,
66
+ ]}
67
+ >
68
+ {checkboxComponent || (
69
+ <Image
70
+ source={require('../../asset/check.png')}
71
+ style={[
72
+ {
73
+ height:
74
+ checkboxComponentStyles?.checkboxSize ||
75
+ checkboxSize ||
76
+ CHECKBOX_SIZE,
77
+ width:
78
+ checkboxComponentStyles?.checkboxSize ||
79
+ checkboxSize ||
80
+ CHECKBOX_SIZE,
81
+ },
82
+ ]}
83
+ />
84
+ )}
65
85
  </View>
66
86
  {label && (
67
87
  <Text
@@ -1,3 +1,4 @@
1
+ import React from 'react';
1
2
  import type { ColorValue, ViewStyle, TextStyle } from 'react-native';
2
3
 
3
4
  export type CheckboxProps = {
@@ -13,5 +14,6 @@ export type CheckboxProps = {
13
14
  checkboxStyle?: ViewStyle;
14
15
  checkboxLabelStyle?: TextStyle;
15
16
  };
17
+ checkboxComponent?: React.ReactNode;
16
18
  onChange?: (value: boolean | string | number) => void;
17
19
  };
@@ -27,12 +27,14 @@ const Dropdown = ({
27
27
  dropdownHelperTextStyle,
28
28
  primaryColor,
29
29
  disabled,
30
+ setIndexOfSelectedItem,
30
31
  }: any) => {
31
32
  return (
32
33
  <View style={[styles.dropdownInputContainer, dropdownContainerStyle]}>
33
34
  {label && label !== '' && (
34
35
  <Text style={[styles.label, labelStyle]}>{label}</Text>
35
36
  )}
37
+
36
38
  <DropdownSelectedItemsView
37
39
  placeholder={placeholder}
38
40
  error={error}
@@ -50,6 +52,7 @@ const Dropdown = ({
50
52
  primaryColor={primaryColor}
51
53
  disabled={disabled}
52
54
  placeholderStyle={placeholderStyle}
55
+ setIndexOfSelectedItem={setIndexOfSelectedItem}
53
56
  />
54
57
 
55
58
  {error && error !== '' && (
@@ -1,8 +1,9 @@
1
1
  /* eslint-disable react-native/no-inline-styles */
2
- import React from 'react';
2
+ import React, { useEffect, useRef } from 'react';
3
3
  import { FlatList, StyleSheet } from 'react-native';
4
4
  import DropdownListItem from './DropdownListItem';
5
5
  import { ItemSeparatorComponent, ListEmptyComponent } from '../Others';
6
+ import { TFlatList } from 'src/types/index.types';
6
7
 
7
8
  const DropdownFlatList = ({
8
9
  options,
@@ -19,9 +20,24 @@ const DropdownFlatList = ({
19
20
  checkboxStyle, // kept for backwards compatibility to be removed in future release
20
21
  checkboxLabelStyle, // kept for backwards compatibility to be removed in future release
21
22
  checkboxComponentStyles,
23
+ checkboxComponent,
22
24
  listComponentStyles,
25
+ listIndex,
23
26
  ...rest
24
27
  }: any) => {
28
+ const flatlistRef = useRef<FlatList<TFlatList>>(null);
29
+
30
+ const scrollToItem = (index: number) => {
31
+ flatlistRef.current?.scrollToIndex({
32
+ index,
33
+ animated: true,
34
+ });
35
+ };
36
+
37
+ useEffect(() => {
38
+ scrollToItem(listIndex.itemIndex);
39
+ }, [listIndex]);
40
+
25
41
  return (
26
42
  <FlatList
27
43
  data={options}
@@ -49,14 +65,22 @@ const DropdownFlatList = ({
49
65
  onChange: isMultiple
50
66
  ? handleMultipleSelections
51
67
  : handleSingleSelection,
68
+ scrollToItem,
52
69
  primaryColor,
53
70
  checkboxSize, // kept for backwards compatibility
54
71
  checkboxStyle, // kept for backwards compatibility
55
72
  checkboxLabelStyle, // kept for backwards compatibility
56
73
  checkboxComponentStyles,
74
+ checkboxComponent
57
75
  })
58
76
  }
59
77
  keyExtractor={(_item, index) => `Options${index}`}
78
+ ref={flatlistRef}
79
+ onScrollToIndexFailed={({ index }) => {
80
+ setTimeout(() => {
81
+ scrollToItem(index);
82
+ }, 500);
83
+ }}
60
84
  {...rest}
61
85
  />
62
86
  );
@@ -75,7 +99,9 @@ const _renderItem = ({ item }: any, props: any) => {
75
99
  checkboxSize={props.checkboxSize}
76
100
  checkboxStyle={props.checkboxStyle}
77
101
  checkboxLabelStyle={props.checkboxLabelStyle}
102
+ scrollToItem={props.scrollToItem}
78
103
  checkboxComponentStyles={props.checkboxComponentStyles}
104
+ checkboxComponent={props.checkboxComponent}
79
105
  />
80
106
  );
81
107
  };
@@ -14,6 +14,7 @@ const DropdownListItem = ({
14
14
  checkboxStyle,
15
15
  checkboxLabelStyle,
16
16
  checkboxComponentStyles,
17
+ checkboxComponent,
17
18
  }: any) => {
18
19
  return (
19
20
  <TouchableOpacity
@@ -37,6 +38,7 @@ const DropdownListItem = ({
37
38
  checkboxComponentStyles?.checkboxLabelStyle || checkboxLabelStyle
38
39
  }
39
40
  disabled={item.disabled}
41
+ checkboxComponent={checkboxComponent}
40
42
  />
41
43
  </TouchableOpacity>
42
44
  );
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable react-native/no-inline-styles */
2
- import React, { useEffect, useState } from 'react';
2
+ import React, { useEffect, useState, useRef } from 'react';
3
3
  import { SectionList, StyleSheet } from 'react-native';
4
4
  import DropdownListItem from './DropdownListItem';
5
5
  import {
@@ -8,6 +8,7 @@ import {
8
8
  SectionHeaderTitle,
9
9
  } from '../Others';
10
10
  import { extractPropertyFromArray } from '../../utils';
11
+ import { TSectionList } from 'src/types/index.types';
11
12
 
12
13
  const DropdownSectionList = ({
13
14
  options,
@@ -24,7 +25,9 @@ const DropdownSectionList = ({
24
25
  checkboxStyle,
25
26
  checkboxLabelStyle,
26
27
  checkboxComponentStyles,
28
+ checkboxComponent,
27
29
  listComponentStyles,
30
+ listIndex,
28
31
  ...rest
29
32
  }: any) => {
30
33
  const [expandedSections, setExpandedSections] = useState(new Set());
@@ -53,6 +56,24 @@ const DropdownSectionList = ({
53
56
  });
54
57
  };
55
58
 
59
+ /**
60
+ * @description Scroll to item location
61
+ */
62
+
63
+ const sectionlistRef = useRef<SectionList<TSectionList>>(null);
64
+
65
+ const scrollToLocation = (listIndex: any) => {
66
+ sectionlistRef.current?.scrollToLocation({
67
+ sectionIndex: listIndex.sectionIndex,
68
+ animated: true,
69
+ itemIndex: listIndex.itemIndex,
70
+ });
71
+ };
72
+
73
+ useEffect(() => {
74
+ scrollToLocation(listIndex);
75
+ }, [listIndex]);
76
+
56
77
  return (
57
78
  <SectionList
58
79
  sections={options}
@@ -85,6 +106,7 @@ const DropdownSectionList = ({
85
106
  checkboxStyle, // kept for backwards compatibility
86
107
  checkboxLabelStyle, // kept for backwards compatibility
87
108
  checkboxComponentStyles,
109
+ checkboxComponent,
88
110
  expandedSections,
89
111
  })
90
112
  }
@@ -100,6 +122,12 @@ const DropdownSectionList = ({
100
122
  }
101
123
  keyExtractor={(_item, index) => `Options${index}`}
102
124
  stickySectionHeadersEnabled={false}
125
+ ref={sectionlistRef}
126
+ onScrollToIndexFailed={() => {
127
+ setTimeout(() => {
128
+ scrollToLocation(listIndex);
129
+ }, 500);
130
+ }}
103
131
  {...rest}
104
132
  />
105
133
  );
@@ -124,6 +152,7 @@ const _renderItem = ({ section: { title }, item }: any, props: any) => {
124
152
  checkboxStyle={props.checkboxStyle}
125
153
  checkboxLabelStyle={props.checkboxLabelStyle}
126
154
  checkboxComponentStyles={props.checkboxComponentStyles}
155
+ checkboxComponent={props.checkboxComponent}
127
156
  />
128
157
  );
129
158
  };
@@ -6,6 +6,7 @@ import {
6
6
  ScrollView,
7
7
  StyleSheet,
8
8
  Image,
9
+ TouchableOpacity,
9
10
  } from 'react-native';
10
11
  import { colors } from '../../styles/colors';
11
12
  import { inputStyles } from '../../styles/input';
@@ -27,6 +28,7 @@ const DropdownSelectedItemsView = ({
27
28
  dropdownErrorStyle,
28
29
  primaryColor,
29
30
  disabled,
31
+ setIndexOfSelectedItem,
30
32
  }: any) => {
31
33
  return (
32
34
  <Pressable
@@ -36,8 +38,7 @@ const DropdownSelectedItemsView = ({
36
38
  ...inputStyles.inputFocusState,
37
39
  borderColor: primaryColor,
38
40
  },
39
- inputStyles.input,
40
- dropdownStyle,
41
+ { ...inputStyles.input, ...dropdownStyle },
41
42
  error && //this must be last
42
43
  error !== '' &&
43
44
  !pressed && {
@@ -57,27 +58,37 @@ const DropdownSelectedItemsView = ({
57
58
  onStartShouldSetResponder={() => true}
58
59
  >
59
60
  {isMultiple ? (
60
- getSelectedItemsLabel()?.map((item: any, i: Number) => (
61
- <Text
61
+ getSelectedItemsLabel()?.map((label: string, i: Number) => (
62
+ <DropdownContent
63
+ onPress={() => {
64
+ handleToggleModal();
65
+ setIndexOfSelectedItem(label); // immediately scrolls to list item with the specified label when modal
66
+ }}
62
67
  key={`react-native-input-select-${Math.random()}-${i}`}
63
68
  style={[
64
69
  styles.selectedItems,
65
70
  { backgroundColor: primaryColor },
66
71
  multipleSelectedItemStyle,
67
72
  ]}
68
- >
69
- {item}
70
- </Text>
73
+ label={label}
74
+ />
71
75
  ))
72
76
  ) : (
73
- <Text style={[styles.blackText, selectedItemStyle]}>
74
- {getSelectedItemsLabel()}
75
- </Text>
77
+ <DropdownContent
78
+ onPress={() => {
79
+ handleToggleModal();
80
+ setIndexOfSelectedItem(getSelectedItemsLabel()); // immediately scrolls to list item with the specified label when modal
81
+ }}
82
+ style={[styles.blackText, selectedItemStyle]}
83
+ label={getSelectedItemsLabel()}
84
+ />
76
85
  )}
77
86
  {!selectedItem && selectedItems?.length === 0 && (
78
- <Text style={[styles.blackText, placeholderStyle]}>
79
- {placeholder ?? 'Select an option'}
80
- </Text>
87
+ <DropdownContent
88
+ onPress={() => handleToggleModal()}
89
+ style={[styles.blackText, placeholderStyle]}
90
+ label={placeholder ?? 'Select an option'}
91
+ />
81
92
  )}
82
93
  </View>
83
94
  </ScrollView>
@@ -90,6 +101,14 @@ const DropdownSelectedItemsView = ({
90
101
  );
91
102
  };
92
103
 
104
+ const DropdownContent = ({ onPress, style, label, ...rest }: any) => {
105
+ return (
106
+ <TouchableOpacity onPress={() => onPress()} {...rest}>
107
+ <Text style={style}>{label}</Text>
108
+ </TouchableOpacity>
109
+ );
110
+ };
111
+
93
112
  const styles = StyleSheet.create({
94
113
  iconStyle: { position: 'absolute', right: 25, top: 25 },
95
114
  selectedItemsContainer: {
package/src/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useState, useEffect } from 'react';
1
+ import React, { useState, useEffect, useCallback } from 'react';
2
2
  import { TouchableOpacity, StyleSheet, View } from 'react-native';
3
3
  import Input from './components/Input';
4
4
  import CheckBox from './components/CheckBox';
@@ -52,6 +52,7 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
52
52
  checkboxStyle, // kept for backwards compatibility
53
53
  checkboxLabelStyle, // kept for backwards compatibility
54
54
  checkboxComponentStyles,
55
+ checkboxComponent,
55
56
  listHeaderComponent,
56
57
  listFooterComponent,
57
58
  listComponentStyles,
@@ -62,9 +63,13 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
62
63
  const [newOptions, setNewOptions] = useState<TFlatList | TSectionList>([]);
63
64
  const [open, setOpen] = useState<boolean>(false);
64
65
  const [selectAll, setSelectAll] = useState<boolean>(false);
65
- const [selectedItem, setSelectedItem] = useState<any>(''); //for single selection
66
- const [selectedItems, setSelectedItems] = useState<any[]>([]); //for multiple selection
66
+ const [selectedItem, setSelectedItem] = useState<any>(''); // for single selection
67
+ const [selectedItems, setSelectedItems] = useState<any[]>([]); // for multiple selection
67
68
  const [searchValue, setSearchValue] = useState<string>('');
69
+ const [listIndex, setListIndex] = useState<{
70
+ sectionIndex?: number;
71
+ itemIndex: number;
72
+ }>({ itemIndex: 0 }); // for scrollToIndex in Sectionlist and Flatlist
68
73
 
69
74
  useEffect(() => {
70
75
  if (options) {
@@ -92,11 +97,15 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
92
97
  const ListTypeComponent = isSectionList
93
98
  ? DropdownSectionList
94
99
  : DropdownFlatList;
95
- let modifiedSectionData = extractPropertyFromArray(newOptions, 'data').flat();
96
- let modifiedOptions = isSectionList ? modifiedSectionData : newOptions;
100
+ const modifiedSectionData = extractPropertyFromArray(
101
+ newOptions,
102
+ 'data'
103
+ ).flat();
104
+ const modifiedOptions = isSectionList ? modifiedSectionData : newOptions;
97
105
 
98
106
  const optLabel = optionLabel || DEFAULT_OPTION_LABEL;
99
107
  const optValue = optionValue || DEFAULT_OPTION_VALUE;
108
+ const optionsCopy = JSON.parse(JSON.stringify(options)); //copy of the original options array
100
109
 
101
110
  /*===========================================
102
111
  * Selection handlers
@@ -121,19 +130,7 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
121
130
  } else {
122
131
  selectedValues.push(value);
123
132
  }
124
-
125
- setSelectedItems(selectedValues);
126
133
  onValueChange(selectedValues); //send value to parent
127
-
128
- //select all checkbox should not be checked if the list contains disabled values
129
- if (
130
- modifiedOptions.filter((item: TFlatListItem) => !item.disabled)
131
- .length === selectedValues.length
132
- ) {
133
- setSelectAll(true);
134
- } else {
135
- setSelectAll(false);
136
- }
137
134
  return selectedValues;
138
135
  });
139
136
  };
@@ -159,6 +156,32 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
159
156
  });
160
157
  };
161
158
 
159
+ /*===========================================
160
+ * Handle side effects
161
+ *==========================================*/
162
+ const checkSelectAll = useCallback(
163
+ (selectedValues: any[]) => {
164
+ //if the list contains disabled values, those values will not be selected
165
+ if (
166
+ modifiedOptions.filter((item: TFlatListItem) => !item.disabled)
167
+ .length === selectedValues.length
168
+ ) {
169
+ setSelectAll(true);
170
+ } else {
171
+ setSelectAll(false);
172
+ }
173
+ },
174
+ [modifiedOptions]
175
+ );
176
+
177
+ // anytime the selected items change, check if it is time to set `selectAll` to true
178
+ useEffect(() => {
179
+ if (isMultiple) {
180
+ checkSelectAll(selectedItems);
181
+ }
182
+ return () => {};
183
+ }, [checkSelectAll, isMultiple, selectedItems]);
184
+
162
185
  /*===========================================
163
186
  * Get label handler
164
187
  *==========================================*/
@@ -192,7 +215,6 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
192
215
  const regexFilter = new RegExp(searchText, 'i');
193
216
 
194
217
  //Because Search mutates the initial state, we have to search with a copy of the original array
195
- const optionsCopy = JSON.parse(JSON.stringify(options));
196
218
  const searchResults = isSectionList
197
219
  ? searchSectionList(optionsCopy as TSectionList, regexFilter)
198
220
  : searchFlatList(optionsCopy as TFlatList, regexFilter);
@@ -241,6 +263,7 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
241
263
  setOpen(!open);
242
264
  setSearchValue('');
243
265
  setNewOptions(options);
266
+ setListIndex({ itemIndex: 0, sectionIndex: 0 });
244
267
  };
245
268
 
246
269
  useEffect(() => {
@@ -261,6 +284,25 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
261
284
  ? sectionListMaxLength > 1
262
285
  : newOptions.length > 1;
263
286
 
287
+ /*===========================================
288
+ * setIndexOfSelectedItem - For ScrollToIndex
289
+ *==========================================*/
290
+ const setIndexOfSelectedItem = (selectedLabel: string) => {
291
+ isSectionList
292
+ ? optionsCopy.map((item: TSectionListItem, sectionIndex: number) => {
293
+ item.data?.find((dataItem: TFlatListItem, itemIndex: number) => {
294
+ if (dataItem[optLabel] === selectedLabel) {
295
+ setListIndex({ sectionIndex, itemIndex });
296
+ }
297
+ });
298
+ })
299
+ : optionsCopy?.find((item: TFlatListItem, itemIndex: number) => {
300
+ if (item[optLabel] === selectedLabel) {
301
+ setListIndex({ itemIndex });
302
+ }
303
+ });
304
+ };
305
+
264
306
  return (
265
307
  <>
266
308
  <Dropdown
@@ -286,6 +328,7 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
286
328
  primaryColor={primary}
287
329
  disabled={disabled}
288
330
  placeholderStyle={placeholderStyle}
331
+ setIndexOfSelectedItem={setIndexOfSelectedItem}
289
332
  {...rest}
290
333
  />
291
334
  <CustomModal
@@ -320,6 +363,7 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
320
363
  checkboxStyle={checkboxStyle}
321
364
  checkboxLabelStyle={checkboxLabelStyle}
322
365
  checkboxComponentStyles={checkboxComponentStyles}
366
+ checkboxComponent={checkboxComponent}
323
367
  />
324
368
  </TouchableOpacity>
325
369
  </View>
@@ -342,6 +386,8 @@ export const DropdownSelect: React.FC<DropdownProps> = ({
342
386
  checkboxStyle={checkboxStyle}
343
387
  checkboxLabelStyle={checkboxLabelStyle}
344
388
  checkboxComponentStyles={checkboxComponentStyles}
389
+ checkboxComponent={checkboxComponent}
390
+ listIndex={listIndex}
345
391
  />
346
392
  </CustomModal>
347
393
  </>
@@ -47,6 +47,7 @@ export type DropdownProps = {
47
47
  checkboxStyle?: ViewStyle;
48
48
  checkboxLabelStyle?: TextStyle;
49
49
  };
50
+ checkboxComponent?: React.ReactNode;
50
51
  placeholderStyle?: TextStyle;
51
52
  listHeaderComponent?: React.ReactNode;
52
53
  listFooterComponent?: React.ReactNode;