UNPKG

vantui-edit

Version:

一套适用于Taro3及React的vantui组件库

131 lines (110 loc) 5.57 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = Sku; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = require("react"); var _components = require("@tarojs/components"); var _classnames2 = _interopRequireDefault(require("classnames")); var _utils = require("./utils"); var _jsxRuntime = require("react/jsx-runtime"); var preCls = "van-sku"; function Sku(props) { var _props$sku = props.sku, sku = _props$sku === void 0 ? [] : _props$sku, _props$className = props.className, className = _props$className === void 0 ? '' : _props$className, _props$goodsList = props.goodsList, goodsList = _props$goodsList === void 0 ? [] : _props$goodsList, goodsId = props.goodsId, onChange = props.onChange, clickAttrDisable = props.clickAttrDisable, itemRender = props.itemRender, _props$disabledClassN = props.disabledClassName, disabledClassName = _props$disabledClassN === void 0 ? '' : _props$disabledClassN, _props$activeClassNam = props.activeClassName, activeClassName = _props$activeClassNam === void 0 ? '' : _props$activeClassNam, itemDisable = props.itemDisable; var _useState = (0, _react.useState)(), _useState2 = (0, _slicedToArray2.default)(_useState, 2), currentGoods = _useState2[0], setCurrentGoods = _useState2[1]; (0, _react.useEffect)(function () { if (!goodsId && goodsList) { for (var i = 0; i < goodsList.length; i++) { var goodsItem = goodsList[i]; if ((goodsItem === null || goodsItem === void 0 ? void 0 : goodsItem.disabled) !== true) { setCurrentGoods(goodsItem); if (onChange) onChange(goodsItem); break; } } } if (goodsId && goodsList) { for (var _i = 0; _i < goodsList.length; _i++) { var _goodsItem = goodsList[_i]; if ((_goodsItem === null || _goodsItem === void 0 ? void 0 : _goodsItem.id) === goodsId) { setCurrentGoods(_goodsItem); break; } } } }, // eslint-disable-next-line react-hooks/exhaustive-deps [goodsId, goodsList]); var currentAttrCanBuy = (0, _react.useCallback)(function (attrId, attrs) { var skuIds = (0, _toConsumableArray2.default)((currentGoods === null || currentGoods === void 0 ? void 0 : currentGoods.skuIds) || []); if (!skuIds.length) return undefined; var usedAttrId = attrs.filter(function (it) { return skuIds.includes(it.id); })[0].id; skuIds.splice(skuIds.indexOf(usedAttrId), 1); skuIds = skuIds.concat(attrId); var canBuyGoodsItem = undefined; for (var i = 0; i < goodsList.length; i++) { var _goodsList$i, _goodsList$i2; if ((_goodsList$i = goodsList[i]) !== null && _goodsList$i !== void 0 && _goodsList$i.skuIds && (0, _utils.everyItemEqual)(((_goodsList$i2 = goodsList[i]) === null || _goodsList$i2 === void 0 ? void 0 : _goodsList$i2.skuIds) || [], skuIds)) { canBuyGoodsItem = goodsList[i]; break; } } return canBuyGoodsItem; }, [currentGoods === null || currentGoods === void 0 ? void 0 : currentGoods.skuIds, goodsList]); var attrClick = (0, _react.useCallback)(function (canBuyGoodsItem, canBuy) { if (canBuy) { setCurrentGoods(canBuyGoodsItem); if (onChange) onChange(canBuyGoodsItem); } else { if (clickAttrDisable) clickAttrDisable(canBuyGoodsItem); } }, [clickAttrDisable, onChange]); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.View, { className: "".concat(preCls, " ").concat(className), children: sku.map(function (item, index) { return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.View, { className: "".concat(preCls, "-item"), children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.View, { className: "".concat(preCls, "-name"), children: item.name }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.View, { className: "".concat(preCls, "-attrs"), children: item.items.map(function (it, index) { var _classnames; var canBuyGoodsItem = currentAttrCanBuy(it.id, item.items); var canBuy = !canBuyGoodsItem || canBuyGoodsItem.disabled === true || itemDisable && itemDisable(canBuyGoodsItem) ? false : true; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.View, { onClick: function onClick() { return attrClick(canBuyGoodsItem, canBuy); }, className: (0, _classnames2.default)((_classnames = {}, (0, _defineProperty2.default)(_classnames, "".concat(preCls, "-attr"), true), (0, _defineProperty2.default)(_classnames, "".concat(preCls, "-attr-active ").concat(activeClassName), currentGoods === null || currentGoods === void 0 ? void 0 : currentGoods.skuIds.includes(it.id)), (0, _defineProperty2.default)(_classnames, "".concat(preCls, "-attr-disable ").concat(disabledClassName), !canBuy), _classnames)), children: itemRender ? itemRender(it) : it.name }, "attr#".concat(index, "@").concat(it.id)); }) })] }, "sku#".concat(index, "@").concat(item.id)); }) }); }