|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/wavetermdev/waveterm/tsunami/app" |
| 5 | + "github.com/wavetermdev/waveterm/tsunami/vdom" |
| 6 | +) |
| 7 | + |
| 8 | +const AppTitle = "Modal Test (Tsunami Demo)" |
| 9 | +const AppShortDesc = "Test alert and confirm modals in Tsunami" |
| 10 | + |
| 11 | +var App = app.DefineComponent("App", func(_ struct{}) any { |
| 12 | + // State to track modal results |
| 13 | + alertResult := app.UseLocal("") |
| 14 | + confirmResult := app.UseLocal("") |
| 15 | + |
| 16 | + // Hook for alert modal |
| 17 | + alertOpen, triggerAlert := app.UseAlertModal() |
| 18 | + |
| 19 | + // Hook for confirm modal |
| 20 | + confirmOpen, triggerConfirm := app.UseConfirmModal() |
| 21 | + |
| 22 | + // Event handlers for alert |
| 23 | + handleShowAlert := func() { |
| 24 | + triggerAlert(app.ModalConfig{ |
| 25 | + Icon: "⚠️", |
| 26 | + Title: "Alert Message", |
| 27 | + Text: "This is an alert modal. Click OK to dismiss.", |
| 28 | + OnClose: func() { |
| 29 | + alertResult.Set("Alert dismissed") |
| 30 | + }, |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + handleShowAlertSimple := func() { |
| 35 | + triggerAlert(app.ModalConfig{ |
| 36 | + Title: "Simple Alert", |
| 37 | + Text: "This alert has no icon and custom OK text.", |
| 38 | + OkText: "Got it!", |
| 39 | + OnClose: func() { |
| 40 | + alertResult.Set("Simple alert dismissed") |
| 41 | + }, |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + // Event handlers for confirm |
| 46 | + handleShowConfirm := func() { |
| 47 | + triggerConfirm(app.ModalConfig{ |
| 48 | + Icon: "❓", |
| 49 | + Title: "Confirm Action", |
| 50 | + Text: "Do you want to proceed with this action?", |
| 51 | + OnResult: func(confirmed bool) { |
| 52 | + if confirmed { |
| 53 | + confirmResult.Set("User confirmed the action") |
| 54 | + } else { |
| 55 | + confirmResult.Set("User cancelled the action") |
| 56 | + } |
| 57 | + }, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + handleShowConfirmCustom := func() { |
| 62 | + triggerConfirm(app.ModalConfig{ |
| 63 | + Icon: "🗑️", |
| 64 | + Title: "Delete Item", |
| 65 | + Text: "Are you sure you want to delete this item? This action cannot be undone.", |
| 66 | + OkText: "Delete", |
| 67 | + CancelText: "Keep", |
| 68 | + OnResult: func(confirmed bool) { |
| 69 | + if confirmed { |
| 70 | + confirmResult.Set("Item deleted") |
| 71 | + } else { |
| 72 | + confirmResult.Set("Item kept") |
| 73 | + } |
| 74 | + }, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + // Read state values |
| 79 | + currentAlertResult := alertResult.Get() |
| 80 | + currentConfirmResult := confirmResult.Get() |
| 81 | + |
| 82 | + return vdom.H("div", map[string]any{ |
| 83 | + "className": "max-w-4xl mx-auto p-8", |
| 84 | + }, |
| 85 | + vdom.H("h1", map[string]any{ |
| 86 | + "className": "text-3xl font-bold mb-6 text-white", |
| 87 | + }, "Tsunami Modal Test"), |
| 88 | + |
| 89 | + // Alert Modal Section |
| 90 | + vdom.H("div", map[string]any{ |
| 91 | + "className": "mb-8 p-6 bg-gray-800 rounded-lg border border-gray-700", |
| 92 | + }, |
| 93 | + vdom.H("h2", map[string]any{ |
| 94 | + "className": "text-2xl font-semibold mb-4 text-white", |
| 95 | + }, "Alert Modals"), |
| 96 | + vdom.H("div", map[string]any{ |
| 97 | + "className": "flex gap-4 mb-4", |
| 98 | + }, |
| 99 | + vdom.H("button", map[string]any{ |
| 100 | + "className": "px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed", |
| 101 | + "onClick": handleShowAlert, |
| 102 | + "disabled": alertOpen, |
| 103 | + }, "Show Alert with Icon"), |
| 104 | + vdom.H("button", map[string]any{ |
| 105 | + "className": "px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed", |
| 106 | + "onClick": handleShowAlertSimple, |
| 107 | + "disabled": alertOpen, |
| 108 | + }, "Show Simple Alert"), |
| 109 | + ), |
| 110 | + vdom.If(currentAlertResult != "", vdom.H("div", map[string]any{ |
| 111 | + "className": "mt-4 p-3 bg-gray-700 rounded text-gray-200", |
| 112 | + }, "Result: ", currentAlertResult)), |
| 113 | + ), |
| 114 | + |
| 115 | + // Confirm Modal Section |
| 116 | + vdom.H("div", map[string]any{ |
| 117 | + "className": "mb-8 p-6 bg-gray-800 rounded-lg border border-gray-700", |
| 118 | + }, |
| 119 | + vdom.H("h2", map[string]any{ |
| 120 | + "className": "text-2xl font-semibold mb-4 text-white", |
| 121 | + }, "Confirm Modals"), |
| 122 | + vdom.H("div", map[string]any{ |
| 123 | + "className": "flex gap-4 mb-4", |
| 124 | + }, |
| 125 | + vdom.H("button", map[string]any{ |
| 126 | + "className": "px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed", |
| 127 | + "onClick": handleShowConfirm, |
| 128 | + "disabled": confirmOpen, |
| 129 | + }, "Show Confirm Modal"), |
| 130 | + vdom.H("button", map[string]any{ |
| 131 | + "className": "px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700 disabled:opacity-50 disabled:cursor-not-allowed", |
| 132 | + "onClick": handleShowConfirmCustom, |
| 133 | + "disabled": confirmOpen, |
| 134 | + }, "Show Delete Confirm"), |
| 135 | + ), |
| 136 | + vdom.If(currentConfirmResult != "", vdom.H("div", map[string]any{ |
| 137 | + "className": "mt-4 p-3 bg-gray-700 rounded text-gray-200", |
| 138 | + }, "Result: ", currentConfirmResult)), |
| 139 | + ), |
| 140 | + |
| 141 | + // Status info |
| 142 | + vdom.H("div", map[string]any{ |
| 143 | + "className": "p-6 bg-gray-800 rounded-lg border border-gray-700", |
| 144 | + }, |
| 145 | + vdom.H("h2", map[string]any{ |
| 146 | + "className": "text-2xl font-semibold mb-4 text-white", |
| 147 | + }, "Modal Status"), |
| 148 | + vdom.H("div", map[string]any{ |
| 149 | + "className": "text-gray-300", |
| 150 | + }, |
| 151 | + vdom.H("div", nil, "Alert Modal Open: ", vdom.IfElse(alertOpen, "Yes", "No")), |
| 152 | + vdom.H("div", nil, "Confirm Modal Open: ", vdom.IfElse(confirmOpen, "Yes", "No")), |
| 153 | + ), |
| 154 | + ), |
| 155 | + ) |
| 156 | +}) |
0 commit comments