|
| 1 | +@page "/modal" |
| 2 | +@using NetcodeHub.Packages.Components.Modal |
| 3 | +<h2 class="text-primary" style="font-family:'Comic Sans MS'">Confirmation Dialog Component</h2> |
| 4 | +<p>This Blazor component provides a customizable confirmation dialog. It allows users to confirm an action or dismiss the dialog with a "Close" button. The dialog can be styled with custom CSS and Bootstrap classes, and displays a specific instruction message.</p> |
| 5 | +<ul> |
| 6 | + <li><strong>CustomStyle</strong>: An optional parameter to apply custom CSS styles to the dialog.</li> |
| 7 | + <li><strong>BootstrapClass</strong>: An optional parameter to apply Bootstrap classes to the dialog. Default is <code>"border-danger"</code>.</li> |
| 8 | + <li><strong>Instruction</strong>: A string parameter that specifies the instruction message displayed in the dialog.</li> |
| 9 | + <li><strong>Action</strong>: An <code>EventCallback</code> that is invoked when the user confirms the action by clicking the "Confirm" button.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<h3>Methods</h3> |
| 13 | +<ul> |
| 14 | + <li><strong>Show()</strong>: Displays the dialog by invoking the JavaScript function <code>showDialog</code>.</li> |
| 15 | + <li><strong>Confirmed()</strong>: Invoked when the user clicks the "Confirm" button. It calls the <code>Action</code> callback and then closes the dialog by invoking the <code>Close()</code> method.</li> |
| 16 | + <li><strong>Close()</strong>: Closes the dialog by invoking the JavaScript function <code>closeDialog</code>.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<h3>JavaScript Integration</h3> |
| 20 | +<p>To fully implement this component, you need to define the JavaScript functions <code>show dialog</code> and <code>close dialog</code> to show and close the dialog, respectively. Here’s an example of what those functions might look like:</p> |
| 21 | +<pre><code><script> |
| 22 | +function showDialog() { |
| 23 | + document.getElementById('cD').showModal(); |
| 24 | +} |
| 25 | + |
| 26 | +function closeDialog() { |
| 27 | + document.getElementById('cD').close(); |
| 28 | +} |
| 29 | +</script></code></pre> |
| 30 | + |
| 31 | +<button class="btn btn-info mt-2" @onclick="Open">Show Confirmation</button> |
| 32 | + |
| 33 | +<NetcodeHubConfirmation @ref="confirmation" |
| 34 | + ConfirmationContent="@Instruction" |
| 35 | + ConfirmationHeader="Confirmation" |
| 36 | + Action="Handle" |
| 37 | + BootstrapClass="border-success" |
| 38 | + CustomStyle="width:300px;" /> |
| 39 | + |
| 40 | +<p class="m-2">This component can be used to prompt the user with a confirmation dialog before proceeding with an action, enhancing the user experience by ensuring that critical actions are intentional and confirmed.</p> |
| 41 | + |
| 42 | +<h2 style="font-family:'Comic Sans MS'" class="text-success mt-5">Modal Component Description</h2> |
| 43 | +<p>This Blazor component represents a customizable modal dialog. It allows users to define the content and buttons within the dialog, and control its visibility through parameters and events.</p> |
| 44 | +<h3>Component Description</h3> |
| 45 | +<ul> |
| 46 | + <li><strong>ContentTemplate</strong>: A <code>RenderFragment</code> parameter that allows the user to define the content of the dialog.</li> |
| 47 | + <li><strong>ButtonTemplate</strong>: A <code>RenderFragment</code> parameter that allows the user to define the buttons within the dialog.</li> |
| 48 | + <li><strong>Open</strong>: A boolean parameter that determines whether the dialog is open or closed.</li> |
| 49 | + <li><strong>BootstrapClass</strong>: An optional parameter to apply Bootstrap classes to the dialog for styling.</li> |
| 50 | + <li><strong>CustomStyle</strong>: An optional parameter to apply custom CSS styles to the dialog.</li> |
| 51 | + <li><strong>OpenChanged</strong>: An <code>EventCallback<bool></code> that is invoked when the <code>Open</code> parameter changes.</li> |
| 52 | + <li><strong>Actions</strong>: An <code>EventCallback<string></code> that is invoked with a string parameter when actions are performed within the dialog.</li> |
| 53 | +</ul> |
| 54 | + |
| 55 | +<h3>Lifecycle Methods</h3> |
| 56 | +<ul> |
| 57 | + <li><strong>OnAfterRenderAsync</strong>: This method is overridden to perform actions after the component has been rendered. It initializes the dialog events on the first render, and then shows or closes the dialog based on the <code>Open</code> parameter.</li> |
| 58 | +</ul> |
| 59 | + |
| 60 | +<h3>JavaScript Invokable Methods</h3> |
| 61 | +<ul> |
| 62 | + <li><strong>MA</strong>: A JavaScript-invokable method that handles actions performed within the dialog. If the dialog is open, it sets the <code>Open</code> parameter to <code>false</code> and invokes the <code>OpenChanged</code> callback. It also invokes the <code>Actions</code> callback with the provided return value.</li> |
| 63 | +</ul> |
| 64 | + |
| 65 | +<h3>JavaScript Integration</h3> |
| 66 | +<p>To fully implement this component, you need to define the JavaScript functions <code>bM</code>, <code>bOM</code>, and <code>bCM</code> to bind the modal, open the modal, and close the modal, respectively. Here’s an example of what those functions might look like:</p> |
| 67 | +<pre><code><script> |
| 68 | +function bM(element, dotNetHelper) { |
| 69 | + element.addEventListener('close', () => { |
| 70 | + dotNetHelper.invokeMethodAsync('MA', 'closed'); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +function bOM(element) { |
| 75 | + element.showModal(); |
| 76 | +} |
| 77 | + |
| 78 | +function bCM(element) { |
| 79 | + element.close(); |
| 80 | +} |
| 81 | +</script></code></pre> |
| 82 | + |
| 83 | +<button class="btn btn-success mt-3" @onclick="OpenModal">Open modal</button> |
| 84 | +<br /> |
| 85 | +<NetcodeHubModal @bind-Open="IsOpen" |
| 86 | + Actions="ButtonActions" |
| 87 | + BootstrapClass="border-success rounded p-2 ml-2 mr-2" |
| 88 | + CustomStyle="min-width:300px; max-width:500px;"> |
| 89 | + |
| 90 | + <ContentTemplate> |
| 91 | + <div class="card "> |
| 92 | + <div class="card-header">Modal Header</div> |
| 93 | + <div class="card-body"> |
| 94 | + <p class="text-muted"> |
| 95 | + The component was built using modal dialog. We are manipulating it using CSS and JS, |
| 96 | + This element provides what we want to display a modal and get a return value of type string. |
| 97 | + </p> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + </ContentTemplate> |
| 101 | + <ButtonTemplate> |
| 102 | + <div class="card border-0"> |
| 103 | + <div class="card-body"> |
| 104 | + <div class="d-flex justify-content-center align-content-center"> |
| 105 | + <button class="btn btn-danger" value="cancel">Cancel</button> |
| 106 | + <button class="btn btn-success" value="save" style="margin-left:10px">Save</button> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </ButtonTemplate> |
| 111 | +</NetcodeHubModal> |
| 112 | +<br /> |
| 113 | +<p>This component provides a flexible and customizable modal dialog for Blazor applications, allowing developers to define both the content and buttons within the dialog, and control its visibility and actions through parameters and events.</p> |
| 114 | + |
| 115 | +@code |
| 116 | +{ |
| 117 | + // NetcodeHub Confirmation Modal Component |
| 118 | + NetcodeHubConfirmation confirmation; |
| 119 | + public bool Confirm { get; set; } |
| 120 | + string Instruction = "Are you sure you wanna do this?"; |
| 121 | + async Task Handle() |
| 122 | + { |
| 123 | + Confirm = true; |
| 124 | + await Close(); |
| 125 | + } |
| 126 | + async Task Open() => await confirmation.OpenConfirmation(); |
| 127 | + async Task Close() => await confirmation!.CloseConfirmation(); |
| 128 | + |
| 129 | + |
| 130 | + // NetcodeHub Modal Component |
| 131 | + public bool IsOpen { get; set; } |
| 132 | + void OpenModal() => IsOpen = true; |
| 133 | + void ButtonActions(string action) |
| 134 | + { |
| 135 | + if (action == "save") |
| 136 | + { |
| 137 | + |
| 138 | + // do something |
| 139 | + OpenModal(); |
| 140 | + } |
| 141 | + |
| 142 | + else if (action == "cancel") |
| 143 | + { |
| 144 | + // default will close the modal |
| 145 | + // you can also do something |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
0 commit comments