Skip to content

Commit

Permalink
refactor(snackbar-service): remove Toasts and add EnqueuedSnackbars (#…
Browse files Browse the repository at this point in the history
…1039)

* 🎬 docs: fix exmaple

* 🚧 feat: wip

* 🆕 feat(Alert): add Title parameter

* 🚧 feat: wip

* 🆕 feat: add DefaultsProvider

* 🚸 feat: enhance the stability of docs

* 📝 docs: Add docs about defaults-providers

* ♻ refactor: Update namespace

* 🎬 docs: update example

* 📝 docs(PopupService): update docs

* 💄 style: Improve the ui of docs

* 🆕 feat: update error-handler

* ⚰ refactor: remove todo

* 🐛 fix: build error
  • Loading branch information
capdiem authored Feb 28, 2023
1 parent 7e0ac4b commit 34c9190
Show file tree
Hide file tree
Showing 85 changed files with 1,257 additions and 1,545 deletions.
6 changes: 2 additions & 4 deletions docs/Masa.Blazor.Docs/Components/Examples/Example.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
else
{
<MSheet Class="mb-9 m-example"
<MSheet Class="m-example"
Outlined
Rounded="true">
<div style="min-height: 52px">
Expand Down Expand Up @@ -59,16 +59,14 @@

<AppDivider />

<MWindow Value="_selected" Class="grey lighten-5">
<MWindow Value="_selected" Class="grey lighten-5 rounded-b">
@foreach (var item in _sections)
{
<MWindowItem>
<Markup Code="@item.Code" Language="@item.Language" Rounded="false" />
</MWindowItem>
}
</MWindow>

<AppDivider />
</ShowTransitionElement>
</ExpandTransition>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div>
<MCard Class="ma-10">
<MCardTitle>Title</MCardTitle>
<MCardSubtitle>Subtitle</MCardSubtitle>
</MCard>

<MDefaultsProvider Defaults="@_defaults">
<MCard Class="ma-10">
<MCardTitle>Title</MCardTitle>
<MCardSubtitle>Subtitle</MCardSubtitle>
</MCard>
</MDefaultsProvider>
</div>

@code {

private readonly IDictionary<string, IDictionary<string, object>> _defaults = new Dictionary<string, IDictionary<string, object>>()
{
{
nameof(MCard), new Dictionary<string, object>()
{
{ nameof(MCard.Elevation), (StringNumber)10 }
}
}
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="text-center">
<PEnqueuedSnackbars @ref="_enqueuedSnackbars" MaxCount="1" />
<MButton OnClick="TurnToText" Color="@_color">Turn to red</MButton>
</div>

@code {

private PEnqueuedSnackbars? _enqueuedSnackbars;

private string? _color;

private void TurnToText()
{
_color = "red";

_enqueuedSnackbars?.EnqueueSnackbar(new SnackbarOptions()
{
Content = "Turn to red successfully!",
Closeable = true,
ActionText = "Undo",
OnAction = async () =>
{
await Task.Delay(1000);
_color = null;
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="text-center">
<PEnqueuedSnackbars @ref="_enqueuedSnackbars"></PEnqueuedSnackbars>

<MButton Color="info" Outlined OnClick="() => Enqueue(AlertTypes.Info)">Info</MButton>
<MButton Color="warning" Outlined OnClick="() => Enqueue(AlertTypes.Warning)">Warning</MButton>
<MButton Color="success" Outlined OnClick="() => Enqueue(AlertTypes.Success)">Success</MButton>
<MButton Color="error" Outlined OnClick="() => Enqueue(AlertTypes.Error)">Error</MButton>
</div>

@code {

private PEnqueuedSnackbars? _enqueuedSnackbars;

private void Enqueue(AlertTypes type)
{
_enqueuedSnackbars?.EnqueueSnackbar(new SnackbarOptions()
{
Content = $"{type} snackbar!",
Type = type,
Closeable = true
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@namespace Masa.Blazor.Docs.Examples.components.enqueued_snackbars
@inherits Masa.Blazor.Presets.PEnqueuedSnackbars

<div class="text-center">
<PEnqueuedSnackbars Position="@Position"
Timeout="@Timeout"
MaxCount="@MaxCount"
Closeable="Closeable"
@ref="_enqueuedSnackbars" />
<MButton OnClick="Enqueue">Enqueue a snackbar</MButton>
</div>

@code {

private PEnqueuedSnackbars? _enqueuedSnackbars;

private void Enqueue()
{
if (_enqueuedSnackbars is null) return;

var content = DateTime.UtcNow;

var snackbar = new SnackbarOptions()
{
Title = "Enqueued snackbars",
Content = $"created at {content} UTC"
};

_enqueuedSnackbars.EnqueueSnackbar(snackbar);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Masa.Blazor.Presets;

namespace Masa.Blazor.Docs.Examples.components.enqueued_snackbars;

public class Usage : Masa.Blazor.Docs.Components.Usage
{
public Usage() : base(typeof(AdvanceUsage))
{
}

protected override string ComponentName => nameof(PEnqueuedSnackbars);

protected override ParameterList<SelectParameter> GenSelectParameters() => new()
{
{
nameof(PEnqueuedSnackbars.Position), new SelectParameter(new List<string>()
{
nameof(SnackPosition.TopLeft),
nameof(SnackPosition.TopRight),
nameof(SnackPosition.TopCenter),
nameof(SnackPosition.BottomLeft),
nameof(SnackPosition.BottomRight),
nameof(SnackPosition.BottomCenter),
}, nameof(SnackPosition.BottomCenter))
}
};

protected override ParameterList<CheckboxParameter> GenCheckboxParameters() => new()
{
{ nameof(PEnqueuedSnackbars.Closeable), new CheckboxParameter(true) }
};

protected override ParameterList<SliderParameter> GenSliderParameters() => new()
{
{ nameof(PEnqueuedSnackbars.MaxCount), new SliderParameter(5, 1, 10, 1) },
{ nameof(PEnqueuedSnackbars.Timeout), new SliderParameter(5000, 0, 9000, 1000) },
};

protected override object? CastValue(ParameterItem<object?> parameter)
{
return parameter.Key switch
{
nameof(PEnqueuedSnackbars.Position) => GetPosition(parameter.Value?.ToString()),
nameof(PEnqueuedSnackbars.MaxCount) => int.Parse(parameter.Value!.ToString()!),
nameof(PEnqueuedSnackbars.Timeout) => int.Parse(parameter.Value!.ToString()!),
_ => parameter.Value
};
}

protected override string FormatValue(string key, object value)
{
if (key == nameof(PEnqueuedSnackbars.Position))
{
return value.ToString() switch
{
nameof(SnackPosition.TopLeft) => "@SnackPosition.TopLeft",
nameof(SnackPosition.TopRight) => "@SnackPosition.TopRight",
nameof(SnackPosition.TopCenter) => "@SnackPosition.TopCenter",
nameof(SnackPosition.BottomLeft) => "@SnackPosition.BottomLeft",
nameof(SnackPosition.BottomRight) => "@SnackPosition.BottomRight",
nameof(SnackPosition.BottomCenter) => "@SnackPosition.BottomCenter",
_ => base.FormatValue(key, value)
};
}

return base.FormatValue(key, value);
}

private static SnackPosition GetPosition(string? name)
{
return name switch
{
nameof(SnackPosition.TopLeft) => SnackPosition.TopLeft,
nameof(SnackPosition.TopRight) => SnackPosition.TopRight,
nameof(SnackPosition.TopCenter) => SnackPosition.TopCenter,
nameof(SnackPosition.BottomLeft) => SnackPosition.BottomLeft,
nameof(SnackPosition.BottomRight) => SnackPosition.BottomRight,
_ => SnackPosition.BottomCenter
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<MErrorHandler PopupType="@_errorPopupType" @ref="_errorHandler">
<MRadioGroup @bind-Value="_errorPopupType" HideDetails="true" Row>
<MRadio Value="ErrorPopupType.Toast" Label="Toast"></MRadio>
<MRadio Value="ErrorPopupType.Snackbar" Label="Snackbar"></MRadio>
<MRadio Value="ErrorPopupType.None" Label="None"></MRadio>
</MRadioGroup>
Expand All @@ -18,7 +17,7 @@
@code
{
private bool _isErrorContent;
private ErrorPopupType _errorPopupType = ErrorPopupType.Toast;
private ErrorPopupType _errorPopupType = ErrorPopupType.Snackbar;
private MErrorHandler? _errorHandler;

private void OnClick()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
@inject IPopupService PopupService

<MAlert Type="AlertTypes.Info">
<div>
在 <code>MApp</code> 组件通过 <code>ConfirmParameters</code> 设置Confirm的默认样式和行为。
</div>
<div>
例如:<code>ConfirmParameters="@@("param => { param.OkText = "Confirm"; })"</code>
</div>
</MAlert>

<MDivider Class="my-4" />

<MButton OnClick="ShowBasic">Basic Confirm</MButton>

<MDivider Class="my-4" />

<MRadioGroup @bind-Value="alertType">
<MRadioGroup @bind-Value="alertType" Row>
<MRadio Value="AlertTypes.Success" Label="@nameof(AlertTypes.Success)"></MRadio>
<MRadio Value="AlertTypes.Warning" Label="@nameof(AlertTypes.Warning)"></MRadio>
<MRadio Value="AlertTypes.Error" Label="@nameof(AlertTypes.Error)"></MRadio>
Expand Down Expand Up @@ -71,13 +60,13 @@
throw new Exception("something wrong!");
}

await PopupService.AlertAsync("Handle onOk successfully!", AlertTypes.Success);
await PopupService.EnqueueSnackbarAsync("Handle onOk successfully!", AlertTypes.Success);
}
catch (Exception e)
{
args.Cancel();

await PopupService.AlertAsync(e);
await PopupService.EnqueueSnackbarAsync(e, true);
}
});
}
Expand All @@ -99,12 +88,12 @@
{
if (confirmed)
{
await PopupService.AlertAsync("Ok was clicked!", AlertTypes.Info);
await PopupService.EnqueueSnackbarAsync("Ok was clicked!", AlertTypes.Info);
}
else
{
await PopupService.AlertAsync("Cancel was clicked!", AlertTypes.Warning);
await PopupService.EnqueueSnackbarAsync("Cancel was clicked!", AlertTypes.Warning);
}
}

}
}
Loading

0 comments on commit 34c9190

Please sign in to comment.