UNPKG

@aliretail/react-materials-components

Version:
124 lines (109 loc) 3.66 kB
import _Button from "@alifd/next/es/button"; import _Icon from "@alifd/next/es/icon"; import _Dialog from "@alifd/next/es/dialog"; import _Message from "@alifd/next/es/message"; import React, { useState, useEffect, useCallback } from 'react'; import CoreInput from "./CoreInput"; var FormulaInput = function FormulaInput(props) { var noWrapper = props.noWrapper, onChange = props.onChange, configList = props.configList, value = props.value, dialogTitle = props.dialogTitle, isPreview = props.isPreview, disabled = props.disabled, onOpenDialog = props.onOpenDialog, onCloseDialog = props.onCloseDialog; var _useState = useState(false), showInput = _useState[0], setShowInput = _useState[1]; var _useState2 = useState(value), dialogInputValue = _useState2[0], setDialogInputValue = _useState2[1]; var _useState3 = useState(value), stateValue = _useState3[0], setStateValue = _useState3[1]; var handleChange = useCallback(function (newValue) { onChange(newValue); setStateValue(newValue); }, [onChange]); var handleButtonClick = useCallback(function () { var invoke = function invoke() { setDialogInputValue(stateValue); setShowInput(!showInput); }; if (typeof onOpenDialog === 'function') { onOpenDialog().then(invoke)["catch"](function () {}); } else { invoke(); } }, [showInput, stateValue, onOpenDialog]); var closeDialog = useCallback(function () { setShowInput(false); }, []); useEffect(function () { setStateValue(value); }, [value]); if (isPreview) { return /*#__PURE__*/React.createElement("span", null, value); } if (noWrapper) { return /*#__PURE__*/React.createElement(CoreInput, { value: value, onChange: onChange, configList: configList }); } var inputDialog = null; if (showInput) { var handleOk = function handleOk() { if (dialogInputValue) { var invoke = function invoke() { handleChange(dialogInputValue); closeDialog(); }; if (typeof onCloseDialog === 'function') { onCloseDialog(dialogInputValue).then(invoke)["catch"](function (errorMessage) { if (errorMessage === void 0) { errorMessage = ''; } _Message.warning(errorMessage || '请正确输入'); }); } else { invoke(); } } }; inputDialog = /*#__PURE__*/React.createElement(_Dialog, { visible: true, title: dialogTitle, onOk: handleOk, onClose: closeDialog, onCancel: closeDialog, shouldUpdatePosition: true }, /*#__PURE__*/React.createElement("div", { className: "aliretail-formula-input-dialog" }, /*#__PURE__*/React.createElement(CoreInput, { value: dialogInputValue, onChange: setDialogInputValue, configList: configList }))); } return /*#__PURE__*/React.createElement("div", { className: "aliretail-formula-input-container" }, /*#__PURE__*/React.createElement("span", { className: "aliretail-formula-input-value" }, stateValue), /*#__PURE__*/React.createElement(_Button, { text: true, type: "primary", onClick: handleButtonClick, className: "aliretail-formula-input-button", disabled: disabled }, /*#__PURE__*/React.createElement("span", null, stateValue ? '编辑' : '添加'), /*#__PURE__*/React.createElement(_Icon, { type: stateValue ? 'edit-square' : 'add' })), inputDialog); }; FormulaInput.defaultProps = { onChange: function onChange() {} }; export default FormulaInput;