/// <summary> /// Show the modal. /// </summary> /// <param name="ModalTitle">Modal title.</param> /// <param name="Mode">To determine confirm button style and content.</param> /// <returns>Modal result, Confirm or cancel depend on user selection.</returns> public async Task<DialogResult> ShowModal(string ModalTitle, ModalConfirmButton Mode) { // Assign parameter to reflect on the component. // set animation class. animationName = ShowAnimation switch { ModalShowAnimation.BounceIn => "animate__bounceIn", ModalShowAnimation.BounceInDown => "animate__bounceInDown", _ => "animate__bounceIn" }; AnimateClassName = $"{animationName} animate__fast"; ConfirmType = Mode; this.ModalTitle = ModalTitle; Result = DialogResult.Undefiend; // set confirm button style and content. switch (Mode) { case ModalConfirmButton.Add: MainButtonClass = "btn-primary"; MainButtonText = "Add"; break; case ModalConfirmButton.Edit: MainButtonClass = "btn-primary"; MainButtonText = "Edit"; break; case ModalConfirmButton.Delete: MainButtonClass = "btn-danger"; MainButtonText = "Delete"; break; default: MainButtonClass = "btn-success"; MainButtonText = "Ok"; break; } // Show the modal. IsShown = true; // Force layout to refreshing component. StateHasChanged(); // Wait for user input. while (Result == DialogResult.Undefiend) { await Task.Delay(50); } return Result; }
/// <summary> /// Show Add/Update Modal and return the result. /// </summary> /// <param name="ModalTitle">Modal Title.</param> /// <param name="Mode">Add or Update modal.</param> /// <param name="ItemId">Item unique identifier to send to server when confirm, In update mode only.</param> /// <param name="Item">Item to update, In update mode only.</param> /// <returns> /// <para>Tuble of DialogResult, T.</para> /// <para>Fisrt is ModalResult: represent user selection, confirm or cancel.</para> /// <para>Second is NewItem: new item or updated item.</para> /// </returns> public async Task <(DialogResult ModalResult, T NewItem)> ShowModal(string ModalTitle, DialogMode Mode, string ItemId = null, T Item = default) { // Assign parameter to reflect on the component. DialogMode = Mode; this.Item = Item; ModalConfirmButton mode = ModalConfirmButton.Add; if (Mode == DialogMode.Edit) { // Update case: mode = ModalConfirmButton.Edit; // Create field for each property. for (int i = 0; i < Properties.Count; i++) { // Read property info. var prop = typeof(T).GetProperty(Properties[i].Key); // Read property value from item. var value = prop.GetValue(Item); // Assign value to defaultValue of the property to use in component. Properties[i].DefaultValue = (value is DateTime) ? ((DateTime)value).ToString("yyyy-MM-dd") : value.ToString(); } } else { // Add case: // Clear default values. for (int i = 0; i < Properties.Count; i++) { Properties[i].DefaultValue = ""; } } // Show modal and wait for the result. var ModalResult = await ModalRef.ShowModal(ModalTitle, mode); // Create new instance for result. T updatedItem = default; if (ModalResult == DialogResult.Ok) { // Modal confirmed: var param = new List <object>(); // Add ItemId to contractor parameters list in Update mode. if (Mode == DialogMode.Edit) { param.Add(ItemId); } // Add all other properties. for (int i = 0; i < Properties.Count; i++) { param.Add(Properties[i].DefaultValue); } // Create new instance of T object. updatedItem = (T)Activator.CreateInstance(typeof(T), param.ToArray()); } return(ModalResult, updatedItem); }