UNPKG

ze-react-component-library

Version:
165 lines (152 loc) 4.25 kB
import "antd/es/select/style"; import _Select from "antd/es/select"; var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } } return t; }; return __assign.apply(this, arguments); }; import React, { useState, useEffect, memo } from "react"; import useLocale from "../hooks/useLocale"; import ValueHandler from "./ValueHandler"; import "./ZEOperator.less"; var operatorMap = { "=": { label: "等于", types: ["Number", "String", "Boolean"] }, $ne: { label: "不等于", types: ["Number", "String", "Boolean"] }, $in: { label: "等于任一个", types: ["String"] }, $nin: { label: "不等于任一个", types: ["String"] }, $contains: { label: "包含", types: ["String"] }, $gt: { label: "大于", types: ["Number", "Date", "Timestamp"] }, $gte: { label: "大于等于", types: ["Number", "Date", "Timestamp"] }, $lt: { label: "小于", types: ["Number", "Date", "Timestamp"] }, $lte: { label: "小于等于", types: ["Number", "Date", "Timestamp"] }, between: { label: "区间", types: ["Number", "Date", "Timestamp"] }, "is null": { label: "为空", types: ["Number", "Date", "Timestamp", "String", "Boolean"] }, "is not null": { label: "不为空", types: ["Number", "Date", "Timestamp", "String", "Boolean"] } }; var FormOperator = /*#__PURE__*/memo(function (_a) { var type = _a.type, value = _a.value, initialValue = _a.initialValue, onChange = _a.onChange, operatorProps = _a.operatorProps, valueProps = _a.valueProps, disabled = _a.disabled, style = _a.style, _b = _a.className, className = _b === void 0 ? "" : _b; var _c = useState(), operator = _c[0], setOperator = _c[1]; var _d = useState(), innerValue = _d[0], setInnerValue = _d[1]; var t = useLocale().t; var operatorOptions = ((operatorProps === null || operatorProps === void 0 ? void 0 : operatorProps.options) || Object.keys(operatorMap)).filter(function (f) { var _a, _b; return (_b = (_a = operatorMap[f]) === null || _a === void 0 ? void 0 : _a.types) === null || _b === void 0 ? void 0 : _b.includes(type); }).map(function (k) { var _a; return { label: (_a = operatorMap[k]) === null || _a === void 0 ? void 0 : _a.label, value: k }; }); var actualValue = value || { operator: operator, value: innerValue }; var actualChange = function actualChange(v) { setOperator(v.operator); setInnerValue(v.value); onChange === null || onChange === void 0 ? void 0 : onChange(v); }; useEffect(function () { var _a; if (initialValue && !value) { actualChange(initialValue); } else if (!value) { actualChange({ operator: ((_a = operatorOptions[0]) === null || _a === void 0 ? void 0 : _a.value) || "=", value: undefined }); } }, []); return /*#__PURE__*/React.createElement("div", { className: ["form-operator", className].join(" "), style: style }, /*#__PURE__*/React.createElement(_Select, __assign({}, operatorProps, { disabled: disabled, value: actualValue.operator, onChange: function onChange(v) { actualChange({ value: undefined, operator: v }); }, dropdownMatchSelectWidth: false, options: operatorOptions.map(function (d) { return __assign(__assign({}, d), { label: t(d.label) }); }) })), actualValue.operator && /*#__PURE__*/React.createElement("div", { className: "form-operator-value-handler" }, /*#__PURE__*/React.createElement(ValueHandler, { type: type, operator: actualValue.operator, valueProps: __assign(__assign({}, valueProps), { disabled: disabled }), value: actualValue.value, onChange: function onChange(v) { actualChange({ value: v, operator: actualValue.operator }); } }))); }); export default FormOperator;