UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

212 lines (211 loc) 10.6 kB
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-taro"; import { View } from "@tarojs/components"; import Popup from "../popup/index"; import { useConfig } from "../configprovider/index"; import { ComponentDefaults } from "../../utils/typings"; import { mergeProps } from "../../utils/merge-props"; 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 _mergeProps = mergeProps(defaultProps, props), title = _mergeProps.title, rightActions = _mergeProps.rightActions, confirmText = _mergeProps.confirmText, visible = _mergeProps.visible, type = _mergeProps.type, custom = _mergeProps.custom, random = _mergeProps.random, style = _mergeProps.style, className = _mergeProps.className, onChange = _mergeProps.onChange, onDelete = _mergeProps.onDelete, onClose = _mergeProps.onClose, onConfirm = _mergeProps.onConfirm, rest = _object_without_properties(_mergeProps, [ "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() { var style = { background: "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAnCAYAAABJ0cukAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyFpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDE0IDc5LjE1MTQ4MSwgMjAxMy8wMy8xMy0xMjowOToxNSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIChXaW5kb3dzKSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDozOUNBQTQ0MkY3RTMxMUVBQjIzNEJCQkYxNDZGQjg5OCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDozOUNBQTQ0M0Y3RTMxMUVBQjIzNEJCQkYxNDZGQjg5OCI+IDx4bXBNTTpEZXJpdmVkRnJvbS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MWYgnJD1qEhhphOWgS2E2yQZDJWa/lEgIvGIa0D2HKJZGA7yiBgaZqO9AermdFstGXQR8fn+BootW4wfPRBE8zXmXoia2SYLgpsJRVwqBBJ2N+pnueRVhPR8l8MGCnaWw8wDCDSWQoasGQCKJnpjGcwkkKqMBfo7ZyQklQCOxWVcRg4QDXmhkSJRjsGvRAD8HCTuNhNPpXKtLzHn9/n0orBqm052GvL2lcfepTMs8PwsJqyRJRbrlNFpBCUBcYWaKE2h4tampqUtV6pP60HNtapuUksTY2NidiLcWUQPnMOhlxvQoITGxxOPxDIVdR2dnb/V2dr7Umn50/Q31261JC4UhUQYSVUxdtPCCUAK10RuvZeQ0LRQ2tD7fddxdyuT3TijJehATF/yinqmJ2wB+jCGRA11TH9xiXAwEQpG4i0gcZHLPjHSqF0Vx76IgEIpEA9a7+3H5K2RK4jnuPvTToXgR0LW9Htpap71iAzPVNsUIMH0XMdXkfLrfD9BdanifksiKW/5w3Emj3mcHBgb6kpKTn6KYzfBCehzg10H6lHPzMdIWq9U8znEpsXE6N4rTO9TjSGgPanG/pfwjwABn1PZcM5XI0AAAAABJRU5ErkJggg0KLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTkwMjA5MzIyMjIxMzkzMzc2NzAwNjg0MC0tDQo=') no-repeat center", backgroundSize: '100% 100%', width: 28, height: 24 }; return /*#__PURE__*/ React.createElement(View, { style: style }); }; 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(React.Fragment, null, item.id); case 'delete': return /*#__PURE__*/ React.createElement(DeleteIcon, null); case 'close': return /*#__PURE__*/ React.createElement(ArrowDown, { size: 18 }); case 'confirm': return /*#__PURE__*/ React.createElement(React.Fragment, null, confirmText || locale.done); default: return null; } }; return /*#__PURE__*/ React.createElement(View, { key: item.id, className: "".concat(classPrefix, "-body-wrapper") }, /*#__PURE__*/ React.createElement(View, { 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, style: { minHeight: 0 }, overlayStyle: { backgroundColor: 'rgba(0, 0, 0, 0)' } }), /*#__PURE__*/ React.createElement(View, { className: classNames(classPrefix, className), style: style }, title && /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-header") }, /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-header-title") }, title), type === 'default' && /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-header-close"), onClick: onConfirm }, rightActions || locale.done)), /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-body") }, /*#__PURE__*/ React.createElement(View, { 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(View, { 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';