UNPKG

tdesign-react

Version:
148 lines (144 loc) 6.22 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js'; import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js'; import { _ as _objectWithoutProperties } from '../_chunks/dep-6b660ef0.js'; import React, { useState, useEffect } from 'react'; import classNames from 'classnames'; import '../_chunks/dep-f53c91cd.js'; import { Loading } from '../loading/index.js'; import useConfig from '../hooks/useConfig.js'; import useCommonClassName from '../hooks/useCommonClassName.js'; import { switchDefaultProps } from './defaultProps.js'; import parseTNode from '../_util/parseTNode.js'; import useDefaultProps from '../hooks/useDefaultProps.js'; import { l as log } from '../_chunks/dep-b908e1fe.js'; import '../_chunks/dep-eca3a3de.js'; import '../_chunks/dep-026a4c6b.js'; import '../loading/Loading.js'; import '../_chunks/dep-3a09424a.js'; import 'lodash-es'; import '../common/Portal.js'; import 'react-dom'; import '../hooks/useLayoutEffect.js'; import '../loading/gradient.js'; import '../_chunks/dep-c48e2ca1.js'; import '../_chunks/dep-1630b9b4.js'; import '../_chunks/dep-87d110df.js'; import '../hooks/useDomRefCallback.js'; import '../loading/defaultProps.js'; import '../config-provider/ConfigContext.js'; import '../locale/zh_CN.js'; import '../_chunks/dep-e29214cb.js'; import 'dayjs'; import '../_chunks/dep-3c9ab31a.js'; import '../loading/plugin.js'; import '../_util/react-render.js'; import '../_chunks/dep-a74cc5e4.js'; import '../common/PluginContainer.js'; import '../config-provider/index.js'; import '../config-provider/ConfigProvider.js'; import '../config-provider/type.js'; import '../loading/style/index.js'; import '../loading/type.js'; var _excluded = ["className", "value", "defaultValue", "disabled", "loading", "size", "label", "customValue", "onChange", "beforeChange"]; 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; } var Switch = /*#__PURE__*/React.forwardRef(function (originalProps, ref) { var _useConfig = useConfig(), classPrefix = _useConfig.classPrefix; var props = useDefaultProps(originalProps, switchDefaultProps); var className = props.className, value = props.value, defaultValue = props.defaultValue, disabled = props.disabled, loading = props.loading, size = props.size, label = props.label, customValue = props.customValue, onChange = props.onChange, beforeChange = props.beforeChange, restProps = _objectWithoutProperties(props, _excluded); var _ref = customValue || [], _ref2 = _slicedToArray(_ref, 2), _ref2$ = _ref2[0], activeValue = _ref2$ === void 0 ? true : _ref2$, _ref2$2 = _ref2[1], inactiveValue = _ref2$2 === void 0 ? false : _ref2$2; var isControlled = typeof value !== "undefined"; var initChecked = defaultValue === activeValue || value === activeValue; var _useState = useState(initChecked), _useState2 = _slicedToArray(_useState, 2), innerChecked = _useState2[0], setInnerChecked = _useState2[1]; var contentNode = React.useMemo(function () { if (Array.isArray(label)) { var _label = _slicedToArray(label, 2), _label$ = _label[0], activeContent = _label$ === void 0 ? "" : _label$, _label$2 = _label[1], inactiveContent = _label$2 === void 0 ? "" : _label$2; var content = innerChecked ? activeContent : inactiveContent; return parseTNode(content, { value: value }); } return parseTNode(label, { value: value }); }, [label, innerChecked, value]); var handleChange = function handleChange(e) { !isControlled && setInnerChecked(!innerChecked); var changedValue = !innerChecked ? activeValue : inactiveValue; onChange === null || onChange === void 0 || onChange(changedValue, { e: e }); }; var onInternalClick = function onInternalClick(e) { if (disabled) { return; } if (!beforeChange) { handleChange(e); return; } Promise.resolve(beforeChange()).then(function (v) { if (v) { handleChange(e); } })["catch"](function (e2) { log.error("Switch", "some error occurred: ".concat(e2)); }); }; useEffect(function () { if (Array.isArray(customValue) && !customValue.includes(value)) { log.error("Switch", "value is not in customValue: ".concat(JSON.stringify(customValue))); } isControlled && setInnerChecked(value === activeValue); }, [value, customValue, activeValue, isControlled]); var _useCommonClassName = useCommonClassName(), SIZE = _useCommonClassName.SIZE, STATUS = _useCommonClassName.STATUS; var switchClassName = classNames("".concat(classPrefix, "-switch"), className, _defineProperty(_defineProperty(_defineProperty({}, STATUS.checked, innerChecked), STATUS.disabled, disabled), STATUS.loading, loading), SIZE[size]); return /* @__PURE__ */React.createElement("button", _objectSpread(_objectSpread({}, restProps), {}, { type: "button", role: "switch", disabled: disabled || loading, className: switchClassName, ref: ref, onClick: onInternalClick }), /* @__PURE__ */React.createElement("span", { className: "".concat(classPrefix, "-switch__handle") }, loading && /* @__PURE__ */React.createElement(Loading, { loading: true, size: "small" })), /* @__PURE__ */React.createElement("div", { className: "".concat(classPrefix, "-switch__content") }, contentNode)); }); Switch.displayName = "Switch"; export { Switch as default }; //# sourceMappingURL=Switch.js.map