import React from 'react';
import {
Button, Col, Modal, Row, Space,
} from 'antd';
import IceForm from './IceForm';
class IceFormModal extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false,
viewOnly: false,
loading: false,
};
this.iceFormReference = React.createRef();
this.width = 800;
}
setViewOnly(value) {
this.setState({ viewOnly: value });
}
show(data) {
this.props.adapter.beforeRenderFieldHook = this.props.adapter.beforeRenderField ? this.props.adapter.beforeRenderField(data) : (fieldName, field) => field;
if (!data) {
this.setState({ visible: true });
if (this.iceFormReference.current) {
this.iceFormReference.current.resetFields();
}
} else {
this.setState({ visible: true });
if (this.iceFormReference.current && this.iceFormReference.current.isReady()) {
this.iceFormReference.current.updateFields(data);
} else {
this.waitForIt(
() => this.iceFormReference.current && this.iceFormReference.current.isReady(),
() => { this.iceFormReference.current.updateFields(data); },
1000,
);
}
}
}
waitForIt(condition, callback, time) {
setTimeout(() => {
if (condition()) {
callback();
} else {
this.waitForIt(condition, callback, time);
}
}, time);
}
hide() {
this.setState({ visible: false });
}
save(params) {
this.iceFormReference.current.save(params, () => { this.closeModal(); });
}
closeModal() {
this.hide();
this.iceFormReference.current.showError(false);
}
render() {
const {
fields, adapter, saveCallback, cancelCallback,
} = this.props;
const additionalProps = {};
additionalProps.footer = (
);
if (this.state.viewOnly) {
additionalProps.footer = null;
}
return (
{
if (cancelCallback) {
cancelCallback();
} else {
this.closeModal();
}
}}
{...additionalProps}
>
);
}
}
export default IceFormModal;