UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

98 lines (97 loc) 4.16 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _view = _interopRequireDefault(require("../view")); var _form = require("../form"); var _tag = _interopRequireDefault(require("../tag")); var _hooks = require("../utils/hooks"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } var Selectable = _tag.default.Selectable; var TagSelect = function (props, ref) { var _classNames; var _props$prefix = props.prefix, prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix, isPreview = props.isPreview, className = props.className, _props$dataSource = props.dataSource, dataSource = _props$dataSource === void 0 ? [] : _props$dataSource, _props$mode = props.mode, mode = _props$mode === void 0 ? 'single' : _props$mode, onChange = props.onChange, sizeProp = props.size, disabled = props.disabled, _props$inlineMode = props.inlineMode, inlineMode = _props$inlineMode === void 0 ? false : _props$inlineMode; var _useContext = (0, _react.useContext)(_form.ItemContext), isInsideForm = _useContext.isInsideForm, contextSize = _useContext.size; var size = sizeProp || (isInsideForm ? contextSize : 'medium'); var _useValue = (0, _hooks.useValue)(props, [], { fitValue: function fitValue(v) { return Array.isArray(v) ? v : [v]; }, compare: function compare(a, b) { return a.join(',') === b.join(','); } }), value = _useValue[0], setValue = _useValue[1], isControlled = _useValue[2]; var handleClick = function (v) { if (isPreview) { return; } if (mode === 'single') { var hasChecked = value.includes(v); if (isControlled) { if (onChange) { onChange(hasChecked ? undefined : v); } } else { setValue(hasChecked ? [] : [v]); } } else { var newValue = [].concat(value); if (newValue.includes(v)) { newValue.splice(newValue.indexOf(v), 1); } else { newValue.push(v); } if (isControlled) { if (onChange) { onChange(newValue); } } else { setValue(newValue); } } }; var tagType; if (isPreview) { tagType = 'primary'; } else { tagType = mode === 'single' ? 'primary' : 'normal'; } var clsPrefix = prefix + "tag-select"; return /*#__PURE__*/(0, _react.createElement)(_view.default, { className: (0, _classnames.default)(clsPrefix, clsPrefix + "--" + size, className, (_classNames = {}, _classNames[clsPrefix + "--inline"] = inlineMode, _classNames)), ref: ref }, dataSource.map(function (it) { var v = typeof it === 'object' ? it.value : it; var label = typeof it === 'object' ? it.label : it; var itemDisabled = disabled || (typeof it === 'object' ? it.disabled : false); return /*#__PURE__*/(0, _react.createElement)(Selectable, { key: v, size: size, type: tagType, disabled: itemDisabled, onChange: handleClick.bind(null, v), checked: isPreview ? false : value.includes(v) }, label); })); }; var _default = exports.default = /*#__PURE__*/(0, _react.forwardRef)(TagSelect);