@alifd/meet-react
Version:
Fusion Mobile React UI System Component
105 lines • 6.22 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { createElement, forwardRef, useContext } from "react";
import classNames from 'classnames';
import View from '../view';
import { ItemContext } from '../form';
import Tag from '../tag';
import { useValue } from '../utils/hooks';
var Selectable = Tag.Selectable;
var TagSelect = function TagSelect(props, ref) {
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 = useContext(ItemContext),
isInsideForm = _useContext.isInsideForm,
contextSize = _useContext.size;
var size = sizeProp || (isInsideForm ? contextSize : 'medium');
var _useValue = useValue(props, [], {
fitValue: function fitValue(v) {
return Array.isArray(v) ? v : [v];
},
compare: function compare(a, b) {
return a.join(',') === b.join(',');
}
}),
_useValue2 = _slicedToArray(_useValue, 3),
value = _useValue2[0],
setValue = _useValue2[1],
isControlled = _useValue2[2];
var handleClick = function handleClick(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 = _toConsumableArray(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 = "".concat(prefix, "tag-select");
return /*#__PURE__*/React.createElement(View, {
className: classNames(clsPrefix, "".concat(clsPrefix, "--").concat(size), className, _defineProperty({}, "".concat(clsPrefix, "--inline"), inlineMode)),
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__*/React.createElement(Selectable, {
key: v,
size: size,
type: tagType,
disabled: itemDisabled,
onChange: handleClick.bind(null, v),
checked: isPreview ? false : value.includes(v)
}, label);
}));
};
export default /*#__PURE__*/forwardRef(TagSelect);