@niku/react-native-dropdown-select
Version:
A Dropdown select component for React Native
130 lines (114 loc) • 5.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _constants = require("../constants");
var _utils = require("../utils");
var _styles = require("./styles");
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const DropdownModalContent = ({
visible,
onDismiss,
onDismissed,
dropdownContainerStyle,
renderItem,
options,
loading,
loadingComponent,
buttonLayout,
position
}) => {
/**
* Animation
*/
const animationValue = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current;
const show = (0, _react.useCallback)(() => {
_reactNative.InteractionManager.runAfterInteractions(() => {
_reactNative.Animated.timing(animationValue, {
toValue: 1,
duration: 250,
useNativeDriver: false
}).start();
});
}, [animationValue]);
const hide = (0, _react.useCallback)(() => {
_reactNative.InteractionManager.runAfterInteractions(() => {
_reactNative.Animated.timing(animationValue, {
toValue: 0,
duration: 250,
useNativeDriver: false
}).start(result => {
if (result.finished) {
onDismissed && onDismissed();
}
});
});
}, [animationValue, onDismissed]);
(0, _react.useEffect)(() => {
visible ? show() : hide();
}, [visible]);
/**
* Dropdown Position
*/
const frame = (0, _reactNativeSafeAreaContext.useSafeAreaFrame)();
const insets = (0, _reactNativeSafeAreaContext.useSafeAreaInsets)();
const [dropdownContainerHeight, setDropdownContainerHeight] = (0, _react.useState)(0);
const measureDropdownContainer = (0, _react.useCallback)(e => {
setDropdownContainerHeight(e.nativeEvent.layout.height);
}, []);
const dropdownContainerPositionStyles = (0, _react.useMemo)(() => {
const top = buttonLayout.y + buttonLayout.height;
const left = buttonLayout.x;
const width = buttonLayout.width > _constants.DROPDOWN_MIN_WIDTH ? buttonLayout.width : _constants.DROPDOWN_MIN_WIDTH;
let positionStyles = {
top: top - (_reactNative.Platform.OS === 'ios' || frame.y > 0 ? 0 : insets.top),
left: left,
width
};
if (position === 'top') {
positionStyles.top = buttonLayout.y - dropdownContainerHeight + (_reactNative.Platform.OS === 'ios' || frame.y > 0 ? 0 : insets.top);
}
return positionStyles;
}, [buttonLayout, dropdownContainerHeight, position, frame, insets]);
/**
* Animation translate
*/
const dropdownContainerTranslateY = animationValue.interpolate({
inputRange: [0, 1],
outputRange: [position === 'bottom' ? -dropdownContainerHeight / 2 : dropdownContainerHeight / 2, 0],
easing: _reactNative.Easing.linear,
extrapolate: 'clamp'
});
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.TouchableWithoutFeedback, {
style: _styles.styles.backdrop,
onPress: onDismiss
}, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
style: _styles.styles.backdrop
})), /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, {
onLayout: measureDropdownContainer,
style: [_styles.styles.dropdownContainer, dropdownContainerPositionStyles, position === 'top' ? _styles.styles.dropdownContainerPositionTop : {}, {
transform: [{
translateY: dropdownContainerTranslateY
}, {
scaleY: animationValue
}],
opacity: animationValue
}, dropdownContainerStyle]
}, /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
renderItem: renderItem,
data: options,
keyExtractor: (item, index) => {
var _item$value;
return (0, _utils.isDropdownOptionItem)(item) ? (_item$value = item.value) === null || _item$value === void 0 ? void 0 : _item$value.toString() : (typeof item.label === 'string' ? item.label : index) + '-' + item.options.length;
},
ListFooterComponent: () => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, loading && loadingComponent)
})));
};
var _default = DropdownModalContent;
exports.default = _default;
//# sourceMappingURL=index.js.map