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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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; }
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); }
import React from 'react';
import { InputNumber, Slider } from 'antd';
import { useNumberInputState } from '@pisell/utils';
/**
 * PisellPercent 编辑态视图组件
 * 
 * 负责渲染编辑态的输入框
 * 
 * 功能：
 * - 百分比输入（后缀显示 %）
 * - 可选：滑块调节
 * - 滑块和输入框双向同步
 * - 实时或失焦校验
 * 
 * @param props 组件 Props
 */
var EditView = function EditView(props) {
  var _props$showPercentSym = props.showPercentSymbol,
    showPercentSymbol = _props$showPercentSym === void 0 ? true : _props$showPercentSym,
    _props$showSlider = props.showSlider,
    showSlider = _props$showSlider === void 0 ? false : _props$showSlider,
    _props$precision = props.precision,
    precision = _props$precision === void 0 ? 1 : _props$precision,
    _props$range = props.range,
    range = _props$range === void 0 ? [0, 100] : _props$range,
    _props$min = props.min,
    min = _props$min === void 0 ? range[0] : _props$min,
    _props$max = props.max,
    max = _props$max === void 0 ? range[1] : _props$max,
    step = props.step,
    sliderProps = props.sliderProps,
    _props$placeholder = props.placeholder,
    placeholder = _props$placeholder === void 0 ? '请输入百分比' : _props$placeholder,
    disabled = props.disabled,
    autoFocus = props.autoFocus,
    size = props.size,
    keyboard = props.keyboard,
    controls = props.controls,
    className = props.className,
    style = props.style,
    ariaLabel = props['aria-label'],
    ariaDescribedBy = props['aria-describedby'],
    id = props.id,
    name = props.name,
    _onPressEnter = props.onPressEnter;

  // 使用状态管理 hook
  var _useNumberInputState = useNumberInputState(_objectSpread(_objectSpread({}, props), {}, {
      min: min,
      max: max,
      precision: precision
    })),
    value = _useNumberInputState.value,
    isValid = _useNumberInputState.isValid,
    errors = _useNumberInputState.errors,
    handleChange = _useNumberInputState.handleChange,
    handleBlur = _useNumberInputState.handleBlur,
    handleFocus = _useNumberInputState.handleFocus;

  // % 符号显示在 suffix（输入框内部）
  var suffixNode = showPercentSymbol ? '%' : undefined;
  var inputNumberElement = /*#__PURE__*/React.createElement(InputNumber, {
    value: value !== null && value !== void 0 ? value : undefined,
    onChange: handleChange,
    onBlur: handleBlur,
    onFocus: handleFocus,
    onPressEnter: function onPressEnter() {
      return _onPressEnter === null || _onPressEnter === void 0 ? void 0 : _onPressEnter(value !== null && value !== void 0 ? value : null);
    },
    min: min,
    max: max,
    step: step,
    precision: precision,
    suffix: suffixNode,
    placeholder: placeholder,
    disabled: disabled,
    autoFocus: autoFocus,
    size: size,
    keyboard: keyboard,
    controls: controls,
    status: isValid ? undefined : 'error',
    style: showSlider ? undefined : style,
    "aria-label": ariaLabel,
    "aria-describedby": ariaDescribedBy,
    id: id,
    name: name
  });

  // 如果需要显示滑块
  if (showSlider) {
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-percent pisell-percent-edit pisell-percent-with-slider ".concat(className || ''),
      style: style
    }, /*#__PURE__*/React.createElement(Slider, _extends({
      value: value !== null && value !== void 0 ? value : undefined,
      onChange: handleChange,
      min: min,
      max: max,
      step: step,
      disabled: disabled
    }, sliderProps)), inputNumberElement, !isValid && errors.length > 0 && /*#__PURE__*/React.createElement("div", {
      className: "pisell-percent-error"
    }, errors[0]));
  }

  // 仅输入框
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-percent pisell-percent-edit ".concat(className || '')
  }, inputNumberElement, !isValid && errors.length > 0 && /*#__PURE__*/React.createElement("div", {
    className: "pisell-percent-error"
  }, errors[0]));
};
export default EditView;