-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multiple default options (#263)
* feat: add support for multiple default options * docs: add usage example * docs: update prop list * docs: update v2 changelog * chore: add changeset
- Loading branch information
1 parent
7fa489d
commit 5eb49f4
Showing
12 changed files
with
187 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@mobile-reality/react-native-select-pro': minor | ||
--- | ||
|
||
Added support for multiple defaultOption objects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
apps/expo/src/examples/multiselect-with-default-options.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import { Select } from '@mobile-reality/react-native-select-pro'; | ||
|
||
import { SafeAreaViewWrapper } from '../components/safe-area-view-wrapper'; | ||
import { DATA } from '../constants/data'; | ||
|
||
export const MultiSelectWithDefaultOptions = () => { | ||
return ( | ||
<SafeAreaViewWrapper> | ||
<Select options={DATA} multiple={true} defaultOption={[DATA[0], DATA[1]]} /> | ||
</SafeAreaViewWrapper> | ||
); | ||
}; |
44 changes: 44 additions & 0 deletions
44
packages/react-native-select-pro/src/helpers/__tests__/get-default-selection-index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { OptionType } from '../../types'; | ||
import { getDefaultSelectionIndex } from '../index'; | ||
|
||
describe('getDefaultSelectionIndex', () => { | ||
const options: OptionType[] = [ | ||
{ label: 'Option 1', value: 'value1' }, | ||
{ label: 'Option 2', value: 'value2' }, | ||
{ label: 'Option 3', value: 'value3' }, | ||
]; | ||
|
||
it('should return the correct index for a single default option', () => { | ||
const defaultOption = { label: 'Option 2', value: 'value2' }; | ||
expect(getDefaultSelectionIndex(options, defaultOption)).toBe(1); | ||
}); | ||
|
||
it('should return -1 if the single default option is not found', () => { | ||
const defaultOption = { label: 'Option 4', value: 'value4' }; | ||
expect(getDefaultSelectionIndex(options, defaultOption)).toBe(-1); | ||
}); | ||
|
||
it('should return an array of indices for multiple default options', () => { | ||
const defaultOptions = [ | ||
{ label: 'Option 1', value: 'value1' }, | ||
{ label: 'Option 3', value: 'value3' }, | ||
]; | ||
expect(getDefaultSelectionIndex(options, defaultOptions)).toEqual([0, 2]); | ||
}); | ||
|
||
it('should return only found indices for multiple default options', () => { | ||
const defaultOptions = [ | ||
{ label: 'Option 1', value: 'value1' }, | ||
{ label: 'Option 4', value: 'value4' }, | ||
]; | ||
expect(getDefaultSelectionIndex(options, defaultOptions)).toEqual([0]); | ||
}); | ||
|
||
it('should return an empty array if no default options are found', () => { | ||
const defaultOptions = [ | ||
{ label: 'Option 4', value: 'value4' }, | ||
{ label: 'Option 5', value: 'value5' }, | ||
]; | ||
expect(getDefaultSelectionIndex(options, defaultOptions)).toEqual([]); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/react-native-select-pro/src/helpers/get-default-selection-index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { OptionType } from '../types'; | ||
|
||
export const getDefaultSelectionIndex = <T>( | ||
flatOptions: OptionType<T>[], | ||
defaultOption: OptionType<T> | OptionType<T>[], | ||
): number | number[] => { | ||
if (Array.isArray(defaultOption)) { | ||
const foundIndices = defaultOption | ||
.map((option) => flatOptions.findIndex((item) => item.value === option.value)) | ||
.filter((index) => index !== -1); | ||
|
||
if (foundIndices.length > 0) { | ||
return foundIndices; | ||
} | ||
} else { | ||
const foundIndex = flatOptions.findIndex((item) => item.value === defaultOption.value); | ||
|
||
if (foundIndex !== -1) { | ||
return foundIndex; | ||
} | ||
} | ||
|
||
return Array.isArray(defaultOption) ? [] : -1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.