UNPKG

@gizwits/vantui

Version:

机智云组件库

106 lines 5.03 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import { useEffect, useState, useCallback } from 'react'; import { View } from '@tarojs/components'; import classnames from 'classnames'; import { everyItemEqual } from './utils'; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var preCls = "van-sku"; export default 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 = useState(), _useState2 = _slicedToArray(_useState, 2), currentGoods = _useState2[0], setCurrentGoods = _useState2[1]; 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 = useCallback(function (attrId, attrs) { var skuIds = _toConsumableArray((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 && 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 = useCallback(function (canBuyGoodsItem, canBuy) { if (canBuy) { setCurrentGoods(canBuyGoodsItem); if (onChange) onChange(canBuyGoodsItem); } else { if (clickAttrDisable) clickAttrDisable(canBuyGoodsItem); } }, [clickAttrDisable, onChange]); return /*#__PURE__*/_jsx(View, { className: "".concat(preCls, " ").concat(className), children: sku.map(function (item, index) { return /*#__PURE__*/_jsxs(View, { className: "".concat(preCls, "-item"), children: [/*#__PURE__*/_jsx(View, { className: "".concat(preCls, "-name"), children: item.name }), /*#__PURE__*/_jsx(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__*/_jsx(View, { onClick: function onClick() { return attrClick(canBuyGoodsItem, canBuy); }, className: classnames((_classnames = {}, _defineProperty(_classnames, "".concat(preCls, "-attr"), true), _defineProperty(_classnames, "".concat(preCls, "-attr-active ").concat(activeClassName), currentGoods === null || currentGoods === void 0 ? void 0 : currentGoods.skuIds.includes(it.id)), _defineProperty(_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)); }) }); }