@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
210 lines (209 loc) • 8.55 kB
JavaScript
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import React, { useMemo, useState } from "react";
import classNames from "classnames";
import { ArrowDown } from "@nutui/icons-react";
import Popup from "../popup/index";
import { useConfig } from "../configprovider";
import { ComponentDefaults } from "../../utils/typings";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
visible: false,
rightActions: '',
type: 'default',
custom: [],
random: false,
onClose: function() {}
});
export var NumberKeyboard = function(props) {
var locale = useConfig().locale;
var _ref = _object_spread({}, defaultProps, props), title = _ref.title, rightActions = _ref.rightActions, confirmText = _ref.confirmText, visible = _ref.visible, type = _ref.type, custom = _ref.custom, random = _ref.random, style = _ref.style, className = _ref.className, onChange = _ref.onChange, onDelete = _ref.onDelete, onClose = _ref.onClose, onConfirm = _ref.onConfirm, rest = _object_without_properties(_ref, [
"title",
"rightActions",
"confirmText",
"visible",
"type",
"custom",
"random",
"style",
"className",
"onChange",
"onDelete",
"onClose",
"onConfirm"
]);
var classPrefix = 'nut-numberkeyboard';
var getBasicKeys = function() {
var keys = new Array(9).fill(0).map(function(_, index) {
return {
id: String(index + 1),
type: 'num'
};
});
return random ? keys.sort(function() {
return Math.random() > 0.5 ? 1 : -1;
}) : keys;
};
var getCustomKeys = function() {
var customKeys = [
{
id: 'close',
type: 'close'
},
{
id: '0',
type: 'num'
},
{
id: 'delete',
type: 'delete'
}
];
if (!custom) return customKeys;
if (custom.length > 0) {
customKeys[0] = {
id: custom[0],
type: 'custom'
};
}
if (custom.length > 1) {
customKeys[2] = {
id: custom[1],
type: 'custom'
};
}
return customKeys;
};
var keysList = useMemo(function() {
return _to_consumable_array(getBasicKeys()).concat(_to_consumable_array(getCustomKeys()));
}, [
type,
random,
custom
]);
var DeleteIcon = function() {
return /*#__PURE__*/ React.createElement("svg", {
viewBox: "0 0 1024 1024",
width: "28",
height: "28"
}, /*#__PURE__*/ React.createElement("path", {
d: "M875.594 186.122H390.803a51.162 51.162 0 0 0-36.18 14.986L79.91 475.821a51.166 51.166 0 0 0 0 72.358l274.714 274.712a51.164 51.164 0 0 0 36.179 14.986h484.791c46.033 0 83.484-37.45 83.484-83.483V269.606c.001-46.033-37.45-83.484-83.483-83.484zm32.32 568.274c0 17.85-14.473 32.318-32.32 32.318H390.803L116.089 512l274.714-274.714h484.791c17.849 0 32.32 14.47 32.32 32.32v484.789z",
fill: "currentColor"
}), /*#__PURE__*/ React.createElement("path", {
d: "M753.945 360.214l-121.43 121.429-121.43-121.429s-16.062-8.224-30.356 6.072c-14.295 14.295-6.073 30.357-6.073 30.357l121.43 121.428L486.8 627.357s-8.222 16.062 6.072 30.357c14.297 14.296 30.358 6.072 30.358 6.072l109.286-109.285 109.286 109.285s16.062 8.224 30.357-6.072c14.295-14.295 6.07-30.357 6.07-30.357L668.944 518.072l121.431-121.43s8.22-16.061-6.074-30.356c-14.294-14.296-30.356-6.072-30.356-6.072z",
fill: "currentColor"
}));
};
var NumberKeyboardKey = function(props) {
var item = props.item;
var _useState = _sliced_to_array(useState(false), 2), active = _useState[0], setActive = _useState[1];
var onTouchStart = function() {
setActive(true);
};
var onTouchEnd = function(item) {
setActive(false);
switch(item.type){
case 'num':
case 'custom':
onChange === null || onChange === void 0 ? void 0 : onChange(item.id);
break;
case 'close':
onClose === null || onClose === void 0 ? void 0 : onClose();
break;
case 'delete':
onDelete === null || onDelete === void 0 ? void 0 : onDelete();
break;
case 'confirm':
onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm();
break;
default:
break;
}
};
var renderContent = function(item) {
switch(item.type){
case 'num':
case 'custom':
return /*#__PURE__*/ React.createElement("div", null, item.id);
case 'delete':
return /*#__PURE__*/ React.createElement(DeleteIcon, null);
case 'close':
return /*#__PURE__*/ React.createElement(ArrowDown, {
width: 18,
height: 18
});
case 'confirm':
return /*#__PURE__*/ React.createElement(React.Fragment, null, confirmText || locale.done);
default:
return null;
}
};
return /*#__PURE__*/ React.createElement("div", {
key: item.id,
className: "".concat(classPrefix, "-body-wrapper")
}, /*#__PURE__*/ React.createElement("div", {
className: classNames({
key: true,
active: active,
close: item.type === 'close',
delete: item.type === 'delete',
confirm: item.type === 'confirm'
}),
onTouchStart: function() {
return onTouchStart();
},
onTouchEnd: function() {
return onTouchEnd(item);
},
onTouchCancel: function() {
return onTouchEnd(item);
}
}, renderContent(item)));
};
return /*#__PURE__*/ React.createElement(Popup, _object_spread_props(_object_spread({}, rest), {
visible: visible,
position: "bottom",
onOverlayClick: onClose,
onCloseIconClick: onClose,
zIndex: 9999,
overlayStyle: {
backgroundColor: 'rgba(0, 0, 0, 0)'
}
}), /*#__PURE__*/ React.createElement("div", {
className: classNames(classPrefix, className),
style: style
}, title && /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-header")
}, /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-header-title")
}, title), type === 'default' && /*#__PURE__*/ React.createElement("span", {
className: "".concat(classPrefix, "-header-close"),
onClick: onConfirm
}, rightActions || locale.done)), /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-body")
}, /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-body-keys")
}, keysList === null || keysList === void 0 ? void 0 : keysList.map(function(item) {
return /*#__PURE__*/ React.createElement(NumberKeyboardKey, {
key: item.id,
item: item
});
})), type === 'rightColumn' && /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-sidebar")
}, /*#__PURE__*/ React.createElement(NumberKeyboardKey, {
key: "delete",
item: {
id: 'delete',
type: 'delete'
}
}), /*#__PURE__*/ React.createElement(NumberKeyboardKey, {
key: "confirm",
item: {
id: 'confirm',
type: 'confirm'
}
})))));
};
NumberKeyboard.displayName = 'NutNumberKeyboard';