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, { useMemo } from 'react';
import classNames from 'classnames';
import "./Tabs.less";

/**
 * Tabs 胶囊风格选项卡组件
 *
 * @description 提供类似 Segmented 控件的胶囊风格选项卡，支持受控与非受控模式。
 * 用于表单中替代 Radio.Group，展示更为直观的标签页切换交互。
 *
 * @example
 * ```tsx
 * <Tabs
 *   value={current}
 *   options={[
 *     { label: '校区', value: 'campus' },
 *     { label: '场地', value: 'venue' },
 *   ]}
 *   onChange={(val) => setCurrent(val)}
 * />
 * ```
 */
var Tabs = function Tabs(_ref) {
  var value = _ref.value,
    defaultValue = _ref.defaultValue,
    _ref$options = _ref.options,
    options = _ref$options === void 0 ? [] : _ref$options,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    onChange = _ref.onChange,
    className = _ref.className,
    style = _ref.style;
  /** 内部选中值，支持受控与非受控 */
  var _React$useState = React.useState(defaultValue),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    internalValue = _React$useState2[0],
    setInternalValue = _React$useState2[1];

  /** 最终使用的选中值：受控优先 */
  var selectedValue = value !== undefined ? value : internalValue;

  /**
   * 处理选项点击
   */
  var handleSelect = function handleSelect(option) {
    if (disabled || option.disabled) return;
    var newValue = option.value;
    if (value === undefined) {
      setInternalValue(newValue);
    }
    onChange === null || onChange === void 0 || onChange(newValue);
  };
  var tabItems = useMemo(function () {
    return options.map(function (option) {
      var isActive = selectedValue != null && selectedValue !== undefined && String(selectedValue) === String(option.value);
      var isDisabled = disabled || option.disabled;
      return /*#__PURE__*/React.createElement("div", {
        key: option.value,
        className: classNames('pisell-form-tabs-item', {
          'pisell-form-tabs-item--active': isActive,
          'pisell-form-tabs-item--disabled': isDisabled
        }),
        onClick: function onClick() {
          return handleSelect(option);
        },
        role: "tab",
        "aria-selected": isActive,
        "aria-disabled": isDisabled,
        tabIndex: isDisabled ? -1 : 0,
        onKeyDown: function onKeyDown(e) {
          if (e.key === 'Enter' || e.key === ' ') {
            e.preventDefault();
            handleSelect(option);
          }
        }
      }, /*#__PURE__*/React.createElement("span", {
        className: "pisell-form-tabs-item-label"
      }, option.label));
    });
  }, [options, selectedValue, disabled]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-form-tabs', className),
    style: style,
    role: "tablist"
  }, tabItems);
};
export default Tabs;