react-native dropdown/picker/selector component, support Android, IOS, Web.
A react-native dropdown/picker/selector component for Android & iOS & Web.
react-native-modal-dropdown hereinafter referred to as RNMD
I think that in most front-end development processes, developers often need to use basic components to encapsulate components that conform to UI style and business logic. RNMD is such a very basic component, which is only used for positioning and popping up DropDown, so you may need to use it before Encapsulate first.
I think most Frontend Development process developers often need to use base component encapsulate component that conform to UI style and business logic. RNMD is a very basic component that is only used to locate and popup dorpdown, so before using it that you may need to encapsulate it.
If you are looking for an out-of-the-box component, I have encapsulated some cases, you can directly copy the source code into your project and preview it online:
If you are looking for an out of the box component, I have encapsulated a few cases, you can copy the source code into your project directly, online preview.
https://expo.io/@912305043/monchilin-react-native-dropdown-example
(Source Code)( https://github.com/MonchiLin/react-native-dropdown/blob/master/example/src/App.tsx )
You can here find more examples.
You can be here Find more instances.
npm i @monchilin/react-native-dropdown -save
// yarn add @monchilin/react-native-dropdown
Import module
import ModalDropdown from '@monchilin/react-native-dropdown';
<ModalDropdown options={['option 1', 'option 2']}/>
<ModalDropdown options={['option 1', 'option 2']}>
I'm Label
</ModalDropdown>
Prop | Type | Optional | Default | Description |
---|---|---|---|---|
disabled |
bool | Yes | false | disable / enable the component. |
defaultIndex |
number | Yes | -1 | Init selected index. -1 : None is selected. This only change the highlight of the dropdown row, you have to give a defaultValue to change the init text. |
index |
number | Yes | -1 | Init selected index. -1 : None is selected. |
defaultLabel |
string | Yes | Please select… | Init label text. Invalid in custom label. |
dataSource |
array | No | dataSource for renderItem. | |
animated |
bool | Yes | true | Disable / enable animation. |
transitionShow |
string | Yee | flipUp | Expand animation [‘flipUp’, ‘scaleIn’, ‘fadeIn’, ‘slideUp’] |
transitionHide |
string | Yee | flipDown | Expand animation [‘flipDown’, ‘scaleOut’, ‘fadeOut’, ‘slideDown’] |
loading |
bool | Yes | false | Enable loading Indicator |
scrollEnabled |
bool | Yes | true | When false, the content does not scroll. The default value is true |
keyExtractor |
func | Yes | (_, index) => index.toString() | Used to extract a unique key for a given item at the specified index. Key is used for caching and as the react key to track item re-ordering. The default extractor checks item.key , then falls back to using the index, like React does. |
adjustFrame |
func | Yes | This is a callback after the frame of the dropdown have been calculated and before showing. You will receive a style object as argument with some of the props like width height top left and right . Change them to appropriate values that accord with your requirement and make the new style as the return value of this function. |
|
renderItem |
func | Yes | Customize render dataSource item: function(option,index,isActive) Will render a default row if null /undefined . |
|
renderSeparator |
func | Yes | Customize render dropdown list separators. Will render a default thin gray line if null /undefined . |
|
showSeparator |
bool | yes | true | Show split line or not |
renderLabel |
func | Yes | Use this to extract and return text from dataSource object. This text will show on label after dataSource selected. Invalid in wrapper mode. | |
onDropdownWillShow |
func | Yes | Trigger when dropdown will show by touching the button. Return false can cancel the event. |
|
onDropdownWillHide |
func | Yes | Trigger when dropdown will hide by touching the button. Return false can cancel the event. |
|
onSelect |
func | Yes | Trigger when item touched with selected index and value . Return false can cancel the event. |
见 自定义任何样式与属性
章节
Prop | Type | Optional |
---|---|---|
rootContainerStyle |
StyleProp<ViewStyle> |
Yes |
rootContainerProps |
Omit<ViewProps, 'style'> |
Yes |
labelContainerDisabledStyle |
StyleProp<ViewStyle> |
Yes |
labelContainerStyle |
StyleProp<ViewStyle> |
Yes |
labelContainerProps |
Omit<TouchableOpacityProps, 'ref'> |
Yes |
labelStyle |
StyleProp<TextStyle> |
Yes |
labelDisabledStyle |
StyleProp<TextStyle> |
Yes |
labelProps |
Omit<TextProps, 'style'> |
Yes |
modalProps |
Omit<ModalProps, 'visible'> |
Yes |
dropdownStyle |
StyleProp<ViewStyle> |
Yes |
dropdownProps |
Omit<FlatListProps<ItemT>, 'data'> |
Yes |
itemTouchableProps |
Omit<TouchableOpacityProps,'onPress'> |
Yes |
itemLabelStyle |
StyleProp<TextStyle> |
Yes |
itemLabelProps |
Omit<TextProps, 'style'> |
Yes |
itemHighlightStyle |
StyleProp<ViewStyle> |
Yes |
itemLabelHighlightStyle |
StyleProp<TextStyle> |
Yes |
Method | Description |
---|---|
show() |
Show the dropdown. Won’t trigger onDropdownWillShow . |
hide() |
Hide the dropdown. Won’t trigger onDropdownWillHide . |
select(index) |
Select the specified dataSource of the index . Select -1 will reset it to display defaultValue . Won’t trigger onSelect . |
As mentioned at the beginning, RNMD is a very basic component, which provides the ability to customize any styles and attributes, but in RN we cannot see the DOM structure as intuitively as in the browser, so someStyle, someProp
when we see the api Feeling very confused, for this, I made two pictures to show the structure.
RN’s Animated modules provide useNativeDriver
options to improve the animation performance , but it can only be with opacity
and transform
used in conjunction, slideUp with slideDown is using height
to achieve, more obvious in development mode, so if there are requirements for fluency recommend the use of other animation effects .
Author: MonchiLin
Source Code: https://github.com/MonchiLin/react-native-dropdown
#react-native #react #mobile-apps