Skip to content

Commit

Permalink
fix: defaultOption prop (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
irekrog committed Apr 7, 2023
1 parent 9365ac3 commit 1e80ec2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-parrots-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mobile-reality/react-native-select-pro': patch
---

`defaultOption` prop fix
8 changes: 8 additions & 0 deletions apps/expo/src/examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export const Basic = () => {
return (
<SafeAreaViewWrapper>
<Select options={DATA} />
<Select options={DATA} defaultOption={DATA[2]} />
<Select
options={DATA}
defaultOption={{
value: '84bc47cd-c8ab-4673-b428-3d96876f0a3f',
label: 'THIRD LABEL',
}}
/>
</SafeAreaViewWrapper>
);
};
5 changes: 3 additions & 2 deletions packages/react-native-select-pro/src/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ export const reducer = <T>(state: State<T>, action: ActionType<T>): State<T> =>
const setDefaultOption = <T>(options: OptionsType<T>, defaultOption: OptionType<T> | undefined) => {
if (isValidDefaultOption(defaultOption) && options.length > 0) {
const isSectionedOptions = isSectionOptionsType(options);

const foundIndex = isSectionedOptions
? getReducedSectionData(options).indexOf(defaultOption)
: options.indexOf(defaultOption);
? getReducedSectionData(options).findIndex((item) => item.value === defaultOption.value)
: options.findIndex((item) => item.value === defaultOption.value);

if (foundIndex !== -1) {
return {
Expand Down

0 comments on commit 1e80ec2

Please sign in to comment.