Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ For more examples visit our [wiki page](https://github.com/azeezat/react-native-
| listControls | `Object` | `{ selectAllText: 'Choose all', unselectAllText: 'Remove all', selectAllCallback: () => {}, unselectAllCallback: () => {}, hideSelectAll: boolean, emptyListMessage: 'No record found', keyboardShouldPersistTaps: "always" }` |
| searchControls | `Object` | `{ textInputStyle: ViewStyle \| TextStyle, textInputContainerStyle: ViewStyle, textInputProps: TextInputProps, searchCallback:(value)=>{}}` |
| modalControls | `Object` | `{ modalBackgroundStyle: ViewStyle, modalOptionsContainerStyle: ViewStyle, modalProps: ModalProps}` |
| minSelectableItems | `number` | 3 |
| maxSelectableItems | `number` | 5 |
| ref | `useRef<DropdownSelectHandle \| null>(null)` | Use this to open or close the modal as needed e.g dropdownRef.current?.open() or dropdownRef.current?.close() |

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
],
"coverageThreshold": {
"global": {
"branches": 94,
"branches": 93,
"functions": 94,
"lines": 94,
"statements": 94
Expand Down
25 changes: 19 additions & 6 deletions src/hooks/use-selection-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TSelectedItem } from '../types/index.types';
interface UseSelectionHandlerProps {
initialSelectedValue: TSelectedItem | TSelectedItem[]; // Can be a single value or an array
isMultiple: boolean;
minSelectableItems?: number;
maxSelectableItems?: number;
onValueChange: (selectedItems: TSelectedItem | TSelectedItem[]) => void;
closeModal: () => void;
Expand All @@ -13,6 +14,7 @@ interface UseSelectionHandlerProps {
export const useSelectionHandler = ({
initialSelectedValue,
isMultiple,
minSelectableItems = 0,
maxSelectableItems,
onValueChange,
closeModal,
Expand All @@ -29,8 +31,11 @@ export const useSelectionHandler = ({
const handleSingleSelection = useCallback(
(value: TSelectedItem) => {
if (selectedItem === value) {
setSelectedItem('');
onValueChange(''); // Send null to parent when deselected
// Deselect item if minSelectableItems is not reached
if (minSelectableItems === 0) {
setSelectedItem('');
onValueChange(''); // Send null to parent when deselected
}
} else {
setSelectedItem(value);
onValueChange(value); // Send selected value to parent
Expand All @@ -40,7 +45,13 @@ export const useSelectionHandler = ({
}
}
},
[selectedItem, onValueChange, autoCloseOnSelect, closeModal]
[
selectedItem,
minSelectableItems,
onValueChange,
autoCloseOnSelect,
closeModal,
]
);

const handleMultipleSelections = useCallback(
Expand All @@ -49,8 +60,10 @@ export const useSelectionHandler = ({
let selectedValues = [...prevVal];

if (selectedValues.includes(value)) {
// Remove item
selectedValues = selectedValues.filter((item) => item !== value);
// Only remove item if it doesn't drop below the minimum required
if (selectedValues.length > minSelectableItems) {
selectedValues = selectedValues.filter((item) => item !== value);
}
} else {
// Add item
if (
Expand All @@ -66,7 +79,7 @@ export const useSelectionHandler = ({
return selectedValues;
});
},
[maxSelectableItems, onValueChange]
[minSelectableItems, maxSelectableItems, onValueChange]
);

// Return the relevant state and handlers
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const DropdownSelect = forwardRef<DropdownSelectHandle, DropdownProps>(
modalControls,
checkboxControls,
autoCloseOnSelect = true,
minSelectableItems,
maxSelectableItems,
...rest
},
Expand Down Expand Up @@ -138,6 +139,7 @@ export const DropdownSelect = forwardRef<DropdownSelectHandle, DropdownProps>(
} = useSelectionHandler({
initialSelectedValue: selectedValue,
isMultiple,
minSelectableItems,
maxSelectableItems,
onValueChange,
closeModal: () => closeModal(),
Expand Down
1 change: 1 addition & 0 deletions src/types/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type CommonDropdownProps = {
onValueChange: (selectedItems: TSelectedItem | TSelectedItem[]) => void;
selectedValue: TSelectedItem | TSelectedItem[];
autoCloseOnSelect?: boolean;
minSelectableItems?: number;
maxSelectableItems?: number;
};

Expand Down
Loading