UNPKG

@yuntijs/ui

Version:

☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps

142 lines 6.39 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["defaultValue", "value", "onChange", "multiple", "disabled", "options", "classNames", "styles", "size", "checkIcon", "optionRender"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { Icon } from '@lobehub/ui'; import { Avatar, Flex, Typography } from 'antd'; import { Check } from 'lucide-react'; import { useMergedState } from 'rc-util'; import React, { useCallback, useMemo } from 'react'; import { getNumberBySize, useStyles } from "./style"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var Text = Typography.Text, Paragraph = Typography.Paragraph; export var SelectCard = /*#__PURE__*/React.forwardRef(function (props, ref) { var defaultValue = props.defaultValue, value = props.value, onChange = props.onChange, _props$multiple = props.multiple, multiple = _props$multiple === void 0 ? false : _props$multiple, _props$disabled = props.disabled, disabled = _props$disabled === void 0 ? false : _props$disabled, _props$options = props.options, options = _props$options === void 0 ? [] : _props$options, classNames = props.classNames, stylesFromProps = props.styles, _props$size = props.size, size = _props$size === void 0 ? 'middle' : _props$size, checkIcon = props.checkIcon, optionRender = props.optionRender, otherProps = _objectWithoutProperties(props, _excluded); var isImg = useMemo(function () { return options.some(function (o) { return !!o.img || !!o.icon; }); }, [options]); var _useStyles = useStyles({ disabled: disabled, size: size }), cx = _useStyles.cx, styles = _useStyles.styles; var _useMemo = useMemo(function () { return getNumberBySize(size); }, [size]), imgHeight = _useMemo.imgHeight; var formatValue = useCallback(function (v) { if (!multiple) { return v; } if (Array.isArray(v)) { return v; } return v === undefined ? [] : [v]; }, [multiple]); var _useMergedState = useMergedState(formatValue(defaultValue), { defaultValue: formatValue(defaultValue), value: value, onChange: onChange }), _useMergedState2 = _slicedToArray(_useMergedState, 2), currentValue = _useMergedState2[0], setCurrentValue = _useMergedState2[1]; var onSelect = useCallback(function (v) { if (disabled) { return; } if (!multiple) { setCurrentValue(v); return; } if (currentValue.includes(v)) { setCurrentValue(currentValue.filter(function (cv) { return cv !== v; })); return; } setCurrentValue([].concat(_toConsumableArray(currentValue), [v])); }, [currentValue, disabled, multiple, setCurrentValue]); var isSelected = useCallback(function (v) { return multiple ? currentValue.includes(v) : currentValue === v; }, [currentValue, multiple]); var checkIconContent = useMemo(function () { if (checkIcon === null) { return null; } return checkIcon ? /*#__PURE__*/_jsx("div", { className: styles.check, children: checkIcon }) : /*#__PURE__*/_jsx(Icon, { className: styles.check, icon: Check }); }, [checkIcon, styles.check]); var renderOption = useCallback(function (o, index) { var selected = isSelected(o.value); var Option = /*#__PURE__*/_jsxs(Flex, { align: isImg ? 'center' : 'flex-start', className: cx(styles.option, selected && styles.optionSelected, classNames === null || classNames === void 0 ? void 0 : classNames.card), gap: "small", onClick: function onClick() { return onSelect(o.value); }, style: _objectSpread(_objectSpread({}, stylesFromProps === null || stylesFromProps === void 0 ? void 0 : stylesFromProps.card), o.style), vertical: true, children: [isImg && /*#__PURE__*/_jsx(Avatar, { className: classNames === null || classNames === void 0 ? void 0 : classNames.icon, icon: o.icon, shape: "square", size: imgHeight, src: o.img, style: _objectSpread(_objectSpread({}, stylesFromProps === null || stylesFromProps === void 0 ? void 0 : stylesFromProps.icon), o.iconStyle) }), o.label && selected && /*#__PURE__*/_jsx(Text, { ellipsis: true, strong: true, children: o.label }), o.label && !selected && /*#__PURE__*/_jsx(Text, { ellipsis: true, children: o.label }), o.description && /*#__PURE__*/_jsx(Paragraph, { ellipsis: { rows: 2 }, type: "secondary", children: o.description }), selected && checkIconContent] }, o.value); return optionRender ? optionRender(Option, o, index) : Option; }, [classNames === null || classNames === void 0 ? void 0 : classNames.card, classNames === null || classNames === void 0 ? void 0 : classNames.icon, cx, imgHeight, isImg, isSelected, onSelect, checkIconContent, optionRender, styles.option, styles.optionSelected, stylesFromProps === null || stylesFromProps === void 0 ? void 0 : stylesFromProps.card, stylesFromProps === null || stylesFromProps === void 0 ? void 0 : stylesFromProps.icon]); return /*#__PURE__*/_jsx(Flex, _objectSpread(_objectSpread({ gap: "large", ref: ref, wrap: true }, otherProps), {}, { children: options.map(function (o, index) { return renderOption(o, index); }) })); });