@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
131 lines (120 loc) • 4.87 kB
JavaScript
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, { useEffect, useState, memo } from 'react';
import { View, Text, ScrollView, TouchableOpacity } from 'react-native';
import Button from '../button';
import { varCreator as varCreatorButton } from '../button/style';
import CheckboxIcon from '../checkbox/checkbox-icon';
import { varCreator as varCreatorCheckbox } from '../checkbox/style';
import Empty from '../empty';
import { useSafeHeight } from '../hooks';
import SuccessOutline from '../icon/success';
import Locale from '../locale';
import Popup from '../popup/popup';
import PopupHeader from '../popup/popup-header';
import Theme from '../theme';
import { varCreator, styleCreator } from './style';
/**
* Selector 弹出层式 Select
* @description 类似 Web 端的 Select 组件,可以多选、单选。
*/
const Selector = _ref => {
let {
title,
options,
value,
multiple = false,
onChange,
onChangeImmediate,
safeAreaInsetTop,
confirmButtonText,
// popup 组件相关属性
visible,
closeOnPressOverlay = true,
onClose,
...restProps
} = _ref;
const safeHeight = useSafeHeight({
top: safeAreaInsetTop
});
const locale = Locale.useLocale().Selector;
const TOKENS = Theme.useThemeTokens();
const CV = Theme.createVar(TOKENS, varCreator);
const CV_BUTTON = Theme.createVar(TOKENS, varCreatorButton);
const CV_CHECKBOX = Theme.createVar(TOKENS, varCreatorCheckbox);
const STYLES = Theme.createStyle(CV, styleCreator);
const [selectedKeys, setSelectedKeys] = useState(multiple ? value ? [].concat(value) : [] : value ? [value] : []); // 同步已选的数据
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 === null || onChange === void 0 ? void 0 : onChange(newValue, options.filter(opt => opt.value === newValue));
}
};
/**
* 点击确定按钮
*/
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",
safeAreaInsetBottom: true,
round: true
}), /*#__PURE__*/React.createElement(View, {
style: {
maxHeight: safeHeight
}
}, /*#__PURE__*/React.createElement(PopupHeader, {
title: title || locale.title,
onClose: onClose
}), /*#__PURE__*/React.createElement(ScrollView, {
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: CV_BUTTON.button_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: CV.selector_icon_selected_color,
size: CV_CHECKBOX.checkbox_icon_size
}) : null));
})), multiple ? /*#__PURE__*/React.createElement(View, {
style: STYLES.btn
}, /*#__PURE__*/React.createElement(Button, {
type: "primary",
onPress: onPressOk,
text: confirmButtonText || locale.confirmButtonText
})) : null));
};
export default /*#__PURE__*/memo(Selector);
//# sourceMappingURL=selector.js.map