@flatbiz/antd
Version:
118 lines (114 loc) • 4.73 kB
JavaScript
/*! @flatjs/forge MIT @flatbiz/antd */
import { _ as _objectWithoutProperties, a as _slicedToArray, b as _objectSpread2 } from './_rollupPluginBabelHelpers-BspM60Sw.js';
import { useState } from 'react';
import { useMemoizedFn } from 'ahooks';
import { message, Tooltip, Switch } from 'antd';
import { isUndefinedOrNull } from '@flatbiz/utils';
import { jsx } from 'react/jsx-runtime';
var _excluded = ["checkedValue", "unCheckedValue", "onChange", "value", "tooltipProps", "tooltipTitle", "serviceConfig"];
/**
* 解决Switch只能接收boolean的限制,与Form.Item结合使用最佳
* ```
* 1. value 为状态值,不局限于boolean,可以为 [string | number | boolean]
* 2. checkedValue 选中值
* 3. unCheckedValue 未选中值
* 4. 与 Form.Item 结合使用,不再需要配置 valuePropName
* <Form.Item name="fieldName">
* <SwitchWrapper checkedValue={2} unCheckedValue={1} />
* </Form.Item>
* 5. 可设置tooltip效果数据显示
* 6. 可设置 serviceConfig 配置服务调用交互
* ```
*/
var SwitchWrapper = function SwitchWrapper(props) {
var checkedValue = props.checkedValue,
unCheckedValue = props.unCheckedValue,
onChange = props.onChange,
value = props.value,
tooltipProps = props.tooltipProps,
tooltipTitle = props.tooltipTitle,
serviceConfig = props.serviceConfig,
otherProps = _objectWithoutProperties(props, _excluded);
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
loading = _useState2[0],
setLoading = _useState2[1];
var onChangeHandle = useMemoizedFn(function (checked) {
return new Promise(function ($return, $error) {
var changeValue, _serviceConfig$messag, _serviceConfig$messag2;
changeValue = checked ? checkedValue : unCheckedValue;
if (serviceConfig !== null && serviceConfig !== void 0 && serviceConfig.onRequest) {
var $Try_1_Finally = function ($Try_1_Exit) {
return function ($Try_1_Value) {
try {
setLoading(false);
return $Try_1_Exit && $Try_1_Exit.call(this, $Try_1_Value);
} catch ($boundEx) {
return $error($boundEx);
}
}.bind(this);
}.bind(this);
var $Try_1_Post = function () {
try {
return $If_2.call(this);
} catch ($boundEx) {
return $error($boundEx);
}
}.bind(this);
var $Try_1_Catch = function (error) {
try {
void message.error((error === null || error === void 0 ? void 0 : error.message) || (serviceConfig === null || serviceConfig === void 0 || (_serviceConfig$messag2 = serviceConfig.message) === null || _serviceConfig$messag2 === void 0 ? void 0 : _serviceConfig$messag2.defaultError) || '状态修改失败!');
return $Try_1_Finally($Try_1_Post)();
} catch ($boundEx) {
return $Try_1_Finally($error)($boundEx);
}
};
try {
setLoading(true);
return Promise.resolve(serviceConfig.onRequest(changeValue)).then(function ($await_3) {
try {
onChange === null || onChange === void 0 || onChange(changeValue);
void message.success((serviceConfig === null || serviceConfig === void 0 || (_serviceConfig$messag = serviceConfig.message) === null || _serviceConfig$messag === void 0 ? void 0 : _serviceConfig$messag.success) || '状态修改成功!');
return $Try_1_Finally($Try_1_Post)();
} catch ($boundEx) {
return $Try_1_Catch($boundEx);
}
}, $Try_1_Catch);
} catch (error) {
$Try_1_Catch(error);
}
} else {
onChange === null || onChange === void 0 || onChange(changeValue);
return $If_2.call(this);
}
function $If_2() {
return $return();
}
});
});
var tipTitle;
if (typeof tooltipTitle === 'function') {
tipTitle = tooltipTitle(value);
} else {
tipTitle = tooltipTitle;
}
if (tipTitle) {
return /*#__PURE__*/jsx(Tooltip, _objectSpread2(_objectSpread2({
placement: "top"
}, tooltipProps), {}, {
title: tipTitle,
children: /*#__PURE__*/jsx(Switch, _objectSpread2(_objectSpread2({}, otherProps), {}, {
checked: !isUndefinedOrNull(value) && checkedValue === value,
onChange: onChangeHandle
}))
}));
}
return /*#__PURE__*/jsx(Switch, _objectSpread2(_objectSpread2({
loading: loading
}, otherProps), {}, {
checked: !isUndefinedOrNull(value) && checkedValue === value,
onChange: onChangeHandle
}));
};
export { SwitchWrapper as S };
//# sourceMappingURL=switch-wrapper-4CaXzwYq.js.map