UNPKG

@ray-js/smart-ui

Version:

轻量、可靠的智能小程序 UI 组件库

83 lines (82 loc) 3.94 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.withReactProps = void 0; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = __importDefault(require("react")); var lodash_es_1 = require("lodash-es"); var styleObj2Str = function (style) { if (!(0, lodash_es_1.isObject)(style)) return style; return Object.entries(style).reduce(function (acc, _a) { var key = _a[0], value = _a[1]; return "".concat(acc).concat(key, ": ").concat(value, ";"); }, ''); }; var withReactProps = function (WrappedComponent) { return function (props) { var _a; // 自身是否是一个 Slot 组件 var isSelfSlot = typeof (props === null || props === void 0 ? void 0 : props.slot) === 'string'; var slotProps = isSelfSlot ? {} : (_a = props === null || props === void 0 ? void 0 : props.slot) !== null && _a !== void 0 ? _a : {}; var slotPropsNames = Object.keys(slotProps); var omitPropKeys = ['className', 'slot', 'children']; var stylePropsKeys = []; var vantProps = (0, lodash_es_1.omit)(props, omitPropKeys); vantProps = (0, lodash_es_1.mapKeys)(vantProps, function (__, key) { /** * React 中为 onClick,小程序中的 Event 标准写法为 bind:click,故这里会转一道 */ if (key.startsWith('on')) { var eventKey = "bind:".concat((0, lodash_es_1.kebabCase)(key.slice(2))); return eventKey; } if (key.endsWith('Style')) { var stylePropKey = (0, lodash_es_1.kebabCase)(key); stylePropsKeys.push(stylePropKey); return stylePropKey; } /** * React 为 camelCase,Smart 中的 props 统一为 kebab-case,故这里会转一道 */ return (0, lodash_es_1.kebabCase)(key); }); /** * React 中为 className,Smart 中使用 class,故这里会转一道 */ if (props.className) { vantProps.class = props.className; } if ((stylePropsKeys === null || stylePropsKeys === void 0 ? void 0 : stylePropsKeys.length) > 0) { stylePropsKeys.forEach(function (stylePropKey) { var stylePropValue = vantProps[stylePropKey]; vantProps[stylePropKey] = styleObj2Str(stylePropValue); }); } /** * 小程序中的 Slot 写法为子组件 + slot 属性,在 React 中无法直接使用,故这里会转一道, * 统一通过 slot props 来定义 */ if ((slotPropsNames === null || slotPropsNames === void 0 ? void 0 : slotPropsNames.length) > 0) { return ((0, jsx_runtime_1.jsxs)(WrappedComponent, __assign({}, vantProps, { children: [slotPropsNames.map(function (slotName) { var name = (0, lodash_es_1.kebabCase)(slotName); return react_1.default.cloneElement(slotProps === null || slotProps === void 0 ? void 0 : slotProps[slotName], { key: slotName, slot: name }); }), props.children] }))); } return ((0, jsx_runtime_1.jsx)(WrappedComponent, __assign({}, (isSelfSlot ? { slot: props.slot } : {}), vantProps, { children: props.children }))); }; }; exports.withReactProps = withReactProps;