|
| 1 | +import { withMethodExposing } from "../generators/withMethodExposing"; |
| 2 | +import { simpleMultiComp } from "../generators"; |
| 3 | +import { withExposingConfigs } from "../generators/withExposing"; |
| 4 | +import { EvalParamType, ParamsConfig } from "../controls/actionSelector/executeCompTypes"; |
| 5 | +import { JSONObject } from "../../util/jsonTypes"; |
| 6 | +import { trans } from "i18n"; |
| 7 | +import { notificationInstance } from "lowcoder-design"; |
| 8 | +import type { ArgsProps, NotificationPlacement } from 'antd/es/notification/interface'; |
| 9 | + |
| 10 | +const params: ParamsConfig = [ |
| 11 | + { name: "text", type: "string" }, |
| 12 | + { name: "options", type: "JSON" }, |
| 13 | +]; |
| 14 | + |
| 15 | +const showNotification = ( |
| 16 | + params: EvalParamType[], |
| 17 | + level: "open" | "info" | "success" | "warning" | "error" |
| 18 | +) => { |
| 19 | + const text = params?.[0] as string; |
| 20 | + const options = params?.[1] as JSONObject; |
| 21 | + |
| 22 | + const { message , duration, id, placement, dismissible } = options; |
| 23 | + |
| 24 | + const closeIcon: boolean | undefined = dismissible === true ? undefined : (dismissible === false ? false : undefined); |
| 25 | + |
| 26 | + const durationNumberOrNull: number | null = typeof duration === 'number' ? duration : null; |
| 27 | + |
| 28 | + const notificationArgs: ArgsProps = { |
| 29 | + message: text, |
| 30 | + description: message as React.ReactNode, |
| 31 | + duration: durationNumberOrNull ?? 3, |
| 32 | + key: id as React.Key, |
| 33 | + placement: placement as NotificationPlacement ?? "bottomRight", |
| 34 | + closeIcon: closeIcon as boolean, |
| 35 | + }; |
| 36 | + |
| 37 | + // Use notificationArgs to trigger the notification |
| 38 | + |
| 39 | + text && notificationInstance[level](notificationArgs); |
| 40 | +}; |
| 41 | + |
| 42 | +const destroy = ( |
| 43 | + params: EvalParamType[] |
| 44 | +) => { |
| 45 | + // Extract the id from the params |
| 46 | + const id = params[0] as React.Key; |
| 47 | + |
| 48 | + // Call notificationInstance.destroy with the provided id |
| 49 | + notificationInstance.destroy(id); |
| 50 | +}; |
| 51 | + |
| 52 | +//what we would like to expose: title, text, duration, id, btn-obj, onClose, placement |
| 53 | + |
| 54 | +const ToastCompBase = simpleMultiComp({}); |
| 55 | + |
| 56 | +export let ToastComp = withExposingConfigs(ToastCompBase, []); |
| 57 | + |
| 58 | +ToastComp = withMethodExposing(ToastComp, [ |
| 59 | + { |
| 60 | + method: { name: "destroy", description: trans("toastComp.destroy"), params: params }, |
| 61 | + execute: (comp, params) => destroy(params), |
| 62 | + }, |
| 63 | + { |
| 64 | + method: { name: "open", description: trans("toastComp.info"), params: params }, |
| 65 | + execute: (comp, params) => { |
| 66 | + showNotification(params, "open"); |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + method: { name: "info", description: trans("toastComp.info"), params: params }, |
| 71 | + execute: (comp, params) => { |
| 72 | + showNotification(params, "info"); |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + method: { name: "success", description: trans("toastComp.success"), params: params }, |
| 77 | + execute: (comp, params) => { |
| 78 | + showNotification(params, "success"); |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + method: { name: "warn", description: trans("toastComp.warn"), params: params }, |
| 83 | + execute: (comp, params) => { |
| 84 | + showNotification(params, "warning"); |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + method: { name: "error", description: trans("toastComp.error"), params: params }, |
| 89 | + execute: (comp, params) => { |
| 90 | + showNotification(params, "error"); |
| 91 | + }, |
| 92 | + }, |
| 93 | +]); |
| 94 | + |
| 95 | + |
0 commit comments