Skip to content

Commit

Permalink
feat: add props mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hoaphantn7604 committed Mar 25, 2023
1 parent 2ceec7f commit 556b6a5
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ yarn add react-native-element-dropdown
### Dropdown Props
| Props | Params | isRequire | Description |
| ------------------ | ----------------------------------------------- | --------- | ------------------------------------------------------------------- |
| mode | 'default' or 'modal' | No | Mode 'modal' is show the dropdown in the middle of the screen. |
| data | Array | Yes | Data is a plain array |
| labelField | String | Yes | Extract the label from the data item |
| valueField | String | Yes | Extract the primary key from the data item |
Expand Down
15 changes: 9 additions & 6 deletions src/components/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const DropdownComponent: <T>(
onConfirmSelectItem,
accessibilityLabel,
itemAccessibilityLabelField,
mode = 'default',
} = props;

const ref = useRef<View>(null);
Expand All @@ -107,11 +108,10 @@ const DropdownComponent: <T>(
}, []);
const styleHorizontal: ViewStyle = useMemo(() => {
return {
marginBottom: 20,
width: W / 2,
width: orientation === 'LANDSCAPE' ? W / 2 : '100%',
alignSelf: 'center',
};
}, [W]);
}, [W, orientation]);

useImperativeHandle(currentRef, () => {
return { open: eventOpen, close: eventClose };
Expand Down Expand Up @@ -161,7 +161,9 @@ const DropdownComponent: <T>(
const _measure = useCallback(() => {
if (ref && ref?.current) {
ref.current.measureInWindow((pageX, pageY, width, height) => {
const isFull = orientation === 'LANDSCAPE' && !isTablet;
const isFull = isTablet
? false
: mode === 'modal' || orientation === 'LANDSCAPE';
const top = isFull ? 20 : height + pageY + 2;
const bottom = H - top + height;
const left = I18nManager.isRTL ? W - width - pageX : pageX;
Expand All @@ -176,7 +178,7 @@ const DropdownComponent: <T>(
});
});
}
}, [H, W, orientation]);
}, [H, W, orientation, mode]);

const onKeyboardDidShow = useCallback(
(e: KeyboardEvent) => {
Expand Down Expand Up @@ -607,13 +609,14 @@ const DropdownComponent: <T>(
justifyContent: 'flex-end',
paddingBottom: extendHeight,
},
isFull && styles.fullScreen,
])}
>
<View
style={StyleSheet.flatten([
styles.container,
containerStyle,
isFull ? styleHorizontal : styleVertical,
containerStyle,
])}
>
{_renderList(isTopPosition)}
Expand Down
1 change: 1 addition & 0 deletions src/components/Dropdown/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface DropdownProps<T> {
accessibilityLabel?: string;
itemAccessibilityLabelField?: string;
inverted?: boolean;
mode?: 'default' | 'modal';
onChange: (item: T) => void;
renderLeftIcon?: (visible?: boolean) => JSX.Element | null | undefined;
renderRightIcon?: (visible?: boolean) => JSX.Element | null | undefined;
Expand Down
4 changes: 4 additions & 0 deletions src/components/Dropdown/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ export const styles = StyleSheet.create({
margin: 6,
height: 45,
},
fullScreen: {
alignItems: 'center',
justifyContent: 'center',
},
});
18 changes: 13 additions & 5 deletions src/components/MultiSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-shadow */
import _ from 'lodash';
import React, {
JSXElementConstructor,
Expand Down Expand Up @@ -91,6 +92,7 @@ const MultiSelectComponent: <T>(
accessibilityLabel,
itemAccessibilityLabelField,
visibleSelectedItem = true,
mode = 'default',
} = props;

const ref = useRef<View>(null);
Expand All @@ -110,8 +112,11 @@ const MultiSelectComponent: <T>(
};
}, []);
const styleHorizontal: ViewStyle = useMemo(() => {
return { marginBottom: 20, width: W / 2, alignSelf: 'center' };
}, [W]);
return {
width: orientation === 'LANDSCAPE' ? W / 2 : '100%',
alignSelf: 'center',
};
}, [W, orientation]);

useImperativeHandle(currentRef, () => {
return { open: eventOpen, close: eventClose };
Expand Down Expand Up @@ -164,7 +169,9 @@ const MultiSelectComponent: <T>(
const _measure = useCallback(() => {
if (ref && ref?.current) {
ref.current.measureInWindow((pageX, pageY, width, height) => {
const isFull = orientation === 'LANDSCAPE' && !isTablet;
const isFull = isTablet
? false
: mode === 'modal' || orientation === 'LANDSCAPE';
const top = isFull ? 20 : height + pageY + 2;
const bottom = H - top + height;
const left = I18nManager.isRTL ? W - width - pageX : pageX;
Expand All @@ -179,7 +186,7 @@ const MultiSelectComponent: <T>(
});
});
}
}, [H, W, orientation]);
}, [H, W, orientation, mode]);

const onKeyboardDidShow = useCallback(
(e: KeyboardEvent) => {
Expand Down Expand Up @@ -597,13 +604,14 @@ const MultiSelectComponent: <T>(
justifyContent: 'flex-end',
paddingBottom: extendHeight,
},
isFull && styles.fullScreen,
])}
>
<View
style={StyleSheet.flatten([
styles.container,
containerStyle,
isFull ? styleHorizontal : styleVertical,
containerStyle,
])}
>
{_renderList(isTopPosition)}
Expand Down
1 change: 1 addition & 0 deletions src/components/MultiSelect/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface MultiSelectProps<T> {
accessibilityLabel?: string;
itemAccessibilityLabelField?: string;
inverted?: boolean;
mode?: 'default' | 'modal';
onChange: (value: string[]) => void;
renderLeftIcon?: (visible?: boolean) => JSX.Element | null | undefined;
renderRightIcon?: (visible?: boolean) => JSX.Element | null | undefined;
Expand Down
4 changes: 4 additions & 0 deletions src/components/MultiSelect/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ export const styles = StyleSheet.create({
color: 'gray',
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
},
fullScreen: {
alignItems: 'center',
justifyContent: 'center',
},
});
1 change: 1 addition & 0 deletions src/components/TextInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-shadow */
import React, { useEffect, useState } from 'react';
import {
Image,
Expand Down
1 change: 1 addition & 0 deletions src/useDeviceOrientation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-shadow */
import { useEffect, useState } from 'react';
import { Dimensions, ScaledSize } from 'react-native';

Expand Down

0 comments on commit 556b6a5

Please sign in to comment.