UNPKG

@fruits-chain/react-native-xiaoshu

Version:
139 lines (127 loc) 5.57 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import React, { useRef, useEffect, useMemo, useState, memo } from 'react'; import { View, Text, ScrollView, TouchableOpacity, useWindowDimensions } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import Popup from '../popup/popup'; import PopupHeader from '../popup/header'; import Empty from '../empty'; import Button from '../button'; import CheckboxIcon from '../checkbox/icon'; import { SuccessOutLine } from '../icon'; import { useTheme, widthStyle } from '../theme'; import { getDefaultValue } from '../helpers'; import { createStyles } from './style'; /** * Selector 弹出层式 Select * @description 类似 Web 端的 Select 组件,可以多选、单选。 */ const Selector = _ref => { let { title, options, value, multiple = false, onChange, onChangeImmediate, safeAreaInsetTop, // popup 组件相关属性 visible, closeOnPressOverlay = true, onClose, ...restProps } = _ref; const THEME_VAR = useTheme(); const insets = useSafeAreaInsets(); const windowDimensions = useWindowDimensions(); const ScrollViewRef = useRef(null); const [selectedKeys, setSelectedKeys] = useState(multiple ? value ? [].concat(value) : [] : value ? [value] : []); const STYLES = widthStyle(THEME_VAR, createStyles); safeAreaInsetTop = getDefaultValue(safeAreaInsetTop, insets.top); /** select 动态高度 */ const selectHeight = useMemo(() => { /** 选项/内容高度 选项个数 + 标题高度 + 圆角边缘 + 多选按钮高度 + 底部安全距离 */ const pickHeight = options.length * THEME_VAR.select_popup_option_text_line_height + THEME_VAR.nav_bar_height + THEME_VAR.popup_round_border_radius + (multiple ? 60 : 0) + insets.bottom; const maxHeight = windowDimensions.height - safeAreaInsetTop; return pickHeight > maxHeight ? maxHeight : pickHeight < THEME_VAR.select_popup_min_height ? THEME_VAR.select_popup_min_height : pickHeight; }, [THEME_VAR.nav_bar_height, THEME_VAR.popup_round_border_radius, THEME_VAR.select_popup_min_height, THEME_VAR.select_popup_option_text_line_height, insets.bottom, multiple, options.length, safeAreaInsetTop, windowDimensions.height]); const selectStyle = useMemo(() => { return { height: selectHeight, paddingBottom: insets.bottom }; }, [selectHeight, insets.bottom]); // 同步已选的数据 useEffect(() => { setSelectedKeys(multiple ? value ? [].concat(value) : [] : value ? [value] : []); }, [value, multiple]); /** * 生成每次点击的回调 */ const genOnPressOption = key => () => { if (multiple) { // 多选维护内部变量 setSelectedKeys(v => { const other = v.filter(val => val !== key); if (other.length === v.length) { const newValues = v.concat(key); return onChangeImmediate ? onChangeImmediate(newValues) : newValues; } return onChangeImmediate ? onChangeImmediate(other) : other; }); } else { const newValue = onChangeImmediate ? onChangeImmediate(key) : key; // 单选直接出发回调 onChange(newValue, options.filter(opt => opt.value === newValue)[0]); } }; /** * 点击确定按钮 */ const onPressOk = () => { onChange === null || onChange === void 0 ? void 0 : onChange(selectedKeys, options.filter(opt => selectedKeys.indexOf(opt.value) > -1)); }; return /*#__PURE__*/React.createElement(Popup, _extends({}, restProps, { visible: visible, onClose: onClose, closeOnPressOverlay: closeOnPressOverlay, onPressOverlay: onClose, position: "bottom", round: true }), /*#__PURE__*/React.createElement(View, { style: selectStyle }, /*#__PURE__*/React.createElement(PopupHeader, { title: title, onClose: onClose }), /*#__PURE__*/React.createElement(View, { style: STYLES.body }, /*#__PURE__*/React.createElement(ScrollView, { ref: ScrollViewRef, bounces: false }, options.length === 0 ? /*#__PURE__*/React.createElement(Empty, null) : null, options === null || options === void 0 ? void 0 : options.map(item => { const isSelected = selectedKeys.findIndex(key => key === item.value) >= 0; return /*#__PURE__*/React.createElement(TouchableOpacity, { key: item.value, disabled: item.disabled, onPress: genOnPressOption(item.value), activeOpacity: THEME_VAR.active_opacity }, /*#__PURE__*/React.createElement(View, { style: STYLES.option_item }, /*#__PURE__*/React.createElement(Text, { style: STYLES.option_item_text, numberOfLines: 1 }, item.label), multiple ? /*#__PURE__*/React.createElement(CheckboxIcon, { active: isSelected, disabled: item.disabled, onPress: genOnPressOption(item.value) }) : isSelected ? /*#__PURE__*/React.createElement(SuccessOutLine, { color: THEME_VAR.primary, size: THEME_VAR.checkbox_icon_size }) : null)); })), multiple ? /*#__PURE__*/React.createElement(View, { style: STYLES.btn }, /*#__PURE__*/React.createElement(Button, { type: "primary", onPress: onPressOk, text: "\u786E\u5B9A" })) : null))); }; export default /*#__PURE__*/memo(Selector); //# sourceMappingURL=selector.js.map