@flatbiz/antd
Version:
180 lines (176 loc) • 6.89 kB
JavaScript
/*! @flatjs/forge MIT @flatbiz/antd */
import { _ as _objectWithoutProperties, a as _slicedToArray, b as _objectSpread2 } from './_rollupPluginBabelHelpers-BYm17lo8.js';
import { hooks } from '@wove/react/hooks';
import { useMemo, useState } from 'react';
import { message, Popconfirm, Switch } from 'antd';
import { fbaHooks } from './fba-hooks/index.js';
import { jsx } from 'react/jsx-runtime';
var _excluded = ["popConfirmProps", "needConfirm", "checkedValue", "unCheckedValue", "onChange", "value", "serviceConfig", "noMessage"];
/**
*
* @description switch 切换时做二次pop提醒, 非可控组件【内部控制】
* @param props
* @param value
* @param checkedValue:选中状态值
* @param unCheckedValue:非选中状态值
* @param needConfirm:二次pop拦截时机 可不传
* close:switch 由开启转换成关闭时pop弹出
* open:switch 由开启转换成开启时pop弹出
* all:switch 一直pop
* none:switch 不弹出
* @param noMessage:不采用内置message提示
* @param onChange:switch 有效切换回掉
* @param popConfirmProps:popConfirm中参数 disabled title description
* @param serviceConfig onRequest
* @field onRequest 请求函数
* @field message message配置 success defaultError
* ```
* 1. 单独使用
* <SwitchConfirmWrapper
* value={value}
* checkedValue={'1'}
* unCheckedValue={'2'}
* serviceConfig={{
* onRequest: async () => {
* await sleep(2000);
* },
* }}
* needConfirm={'all'}
* popConfirmProps={{
* title: '确定要开启吗?',
* }}
* onChange={(value) => {
* setValue(value as string);
* }}
* />
*
* 2. 与Form.Item结合使用
* <Form.Item name="open">
* <SwitchConfirmWrapper
* checkedValue={'1'}
* unCheckedValue={'2'}
* serviceConfig={{
* onRequest: async () => {
* await sleep(2000);
* },
* }}
* needConfirm={'all'}
* popConfirmProps={{
* title: '确定要开启吗?',
* }}
* />
* </Form.Item>
* ```
*/
var SwitchConfirmWrapper = function SwitchConfirmWrapper(props) {
var popConfirmProps = props.popConfirmProps,
needConfirm = props.needConfirm,
checkedValue = props.checkedValue,
unCheckedValue = props.unCheckedValue,
onChange = props.onChange,
value = props.value,
serviceConfig = props.serviceConfig,
noMessage = props.noMessage,
otherProps = _objectWithoutProperties(props, _excluded);
var popConfirmPropsFt = useMemo(function () {
if (typeof popConfirmProps === 'function') {
return popConfirmProps(value === checkedValue);
}
return popConfirmProps;
}, [checkedValue, popConfirmProps, value]);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isCheked = _useState2[0],
setIsCheked = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
loading = _useState4[0],
setLoading = _useState4[1];
fbaHooks.useEffectCustom(function () {
setIsCheked(value === checkedValue);
}, [props.value]);
var showConfirm = useMemo(function () {
return needConfirm === 'close' && isCheked || needConfirm === 'open' && !isCheked || !needConfirm || needConfirm === 'all';
}, [isCheked, needConfirm]);
var onHandleChange = hooks.useCallbackRef(function () {
return new Promise(function ($return, $error) {
var changeValue, _serviceConfig$messag, _serviceConfig$messag2;
changeValue = !isCheked ? 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 {
if (!noMessage) {
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 {
setIsCheked(!isCheked);
onChange === null || onChange === void 0 || onChange(changeValue);
if (!noMessage) {
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 {
setIsCheked(!isCheked);
onChange === null || onChange === void 0 || onChange(changeValue);
return $If_2.call(this);
}
function $If_2() {
return $return();
}
});
});
if (showConfirm) {
return /*#__PURE__*/jsx(Popconfirm, {
disabled: popConfirmPropsFt === null || popConfirmPropsFt === void 0 ? void 0 : popConfirmPropsFt.disabled,
okButtonProps: {
loading: loading
},
title: popConfirmPropsFt === null || popConfirmPropsFt === void 0 ? void 0 : popConfirmPropsFt.title,
description: popConfirmPropsFt === null || popConfirmPropsFt === void 0 ? void 0 : popConfirmPropsFt.description,
onConfirm: onHandleChange,
children: /*#__PURE__*/jsx(Switch, _objectSpread2({
checked: isCheked
}, otherProps))
});
}
return /*#__PURE__*/jsx(Switch, _objectSpread2(_objectSpread2({
checked: isCheked,
loading: loading
}, otherProps), {}, {
onChange: onHandleChange
}));
};
export { SwitchConfirmWrapper as S };
//# sourceMappingURL=switch-confirm-wrapper-BW9G4u03.js.map