@lowdefy/blocks-antd
Version:
Lowdefy Ant Design Blocks
123 lines (119 loc) • 4.22 kB
JavaScript
/*
Copyright 2020-2026 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import React, { useState, useEffect } from 'react';
import { renderHtml, withBlockDefaults } from '@lowdefy/block-utils';
import { get } from '@lowdefy/helpers';
import { Modal } from 'antd';
import withTheme from '../withTheme.js';
const triggerSetOpen = ({ methods, setOpen, state })=>{
if (!state) {
methods.triggerEvent({
name: 'onClose'
});
}
if (state) {
methods.triggerEvent({
name: 'onOpen'
});
}
setOpen(state);
};
const ModalBlock = ({ blockId, classNames = {}, content, events, methods, properties, styles = {} })=>{
const [openState, setOpen] = useState(false);
useEffect(()=>{
methods.registerMethod('toggleOpen', ()=>triggerSetOpen({
state: !openState,
setOpen,
methods
}));
methods.registerMethod('setOpen', ({ open })=>triggerSetOpen({
state: !!open,
setOpen,
methods
}));
});
const extraProps = {};
if (content.footer) {
extraProps.footer = content.footer();
}
if (properties.footer === false) {
extraProps.footer = null;
}
return /*#__PURE__*/ React.createElement("div", {
id: blockId
}, /*#__PURE__*/ React.createElement(Modal, {
id: `${blockId}_modal`,
afterClose: ()=>methods.triggerEvent({
name: 'afterClose'
}),
cancelButtonProps: properties.cancelButtonProps,
cancelText: properties.cancelText ?? 'Cancel',
centered: !!properties.centered,
closable: properties.closable !== undefined ? properties.closable : true,
confirmLoading: get(events, 'onOk.loading'),
mask: properties.mask !== undefined ? properties.mask : true,
maskClosable: properties.maskClosable !== undefined ? properties.maskClosable : true,
okButtonProps: properties.okButtonProps,
okText: properties.okText ?? 'Ok',
okType: properties.okButtonType ?? 'primary',
title: renderHtml({
html: properties.title,
methods
}),
open: openState,
width: properties.width,
zIndex: properties.zIndex,
className: classNames.element,
classNames: {
header: classNames.header,
body: classNames.body,
footer: classNames.footer,
mask: classNames.mask,
content: classNames.content,
wrapper: classNames.wrapper
},
style: styles.element,
styles: {
body: styles.body,
mask: styles.mask,
wrapper: styles.wrapper
},
onOk: async ()=>{
const response = await methods.triggerEvent({
name: 'onOk'
});
if (response.success === false) return;
if (response.bounced !== true) {
triggerSetOpen({
state: false,
setOpen,
methods
});
}
},
onCancel: async ()=>{
const response = await methods.triggerEvent({
name: 'onCancel'
});
if (response.success === false) return;
if (response.bounced !== true) {
triggerSetOpen({
state: false,
setOpen,
methods
});
}
},
...extraProps
}, content.content && content.content()));
};
export default withTheme('Modal', withBlockDefaults(ModalBlock));