|
| 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 | +importtype{ArgsProps,NotificationPlacement}from'antd/es/notification/interface'; |
| 9 | + |
| 10 | +constparams:ParamsConfig=[ |
| 11 | +{name:"text",type:"string"}, |
| 12 | +{name:"options",type:"JSON"}, |
| 13 | +]; |
| 14 | + |
| 15 | +constshowNotification=( |
| 16 | +params:EvalParamType[], |
| 17 | +level:"open"|"info"|"success"|"warning"|"error" |
| 18 | +)=>{ |
| 19 | +consttext=params?.[0]asstring; |
| 20 | +constoptions=params?.[1]asJSONObject; |
| 21 | + |
| 22 | +const{ message, duration, id, placement, dismissible}=options; |
| 23 | + |
| 24 | +constcloseIcon:boolean|undefined=dismissible===true ?undefined :(dismissible===false ?false :undefined); |
| 25 | + |
| 26 | +constdurationNumberOrNull:number|null=typeofduration==='number' ?duration :null; |
| 27 | + |
| 28 | +constnotificationArgs:ArgsProps={ |
| 29 | +message:text, |
| 30 | +description:messageasReact.ReactNode, |
| 31 | +duration:durationNumberOrNull??3, |
| 32 | +key:idasReact.Key, |
| 33 | +placement:placementasNotificationPlacement??"bottomRight", |
| 34 | +closeIcon:closeIconasboolean, |
| 35 | +}; |
| 36 | + |
| 37 | +// Use notificationArgs to trigger the notification |
| 38 | + |
| 39 | +text&¬ificationInstance[level](notificationArgs); |
| 40 | +}; |
| 41 | + |
| 42 | +constdestroy=( |
| 43 | +params:EvalParamType[] |
| 44 | +)=>{ |
| 45 | +// Extract the id from the params |
| 46 | +constid=params[0]asReact.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 | +constToastCompBase=simpleMultiComp({}); |
| 55 | + |
| 56 | +exportletToastComp=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 | + |