-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🆕 add the new component MToggle (#2066)
* 🆕 : add the new component MToggle * refactor with Toggle component * add docs for toggle * (ApiGenerator): type parameter cannot resolve
- Loading branch information
Showing
14 changed files
with
222 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<div class="text-center"> | ||
<MMenu @bind-Value="_on" OffsetY> | ||
<ActivatorContent Context="activator"> | ||
<MToggle Value="_on" DataOn="@("mdi-toggle-switch")" DataOff="@("mdi-toggle-switch-off")"> | ||
<MButton IconName="@context.Data" @attributes="@activator.Attrs"/> | ||
</MToggle> | ||
</ActivatorContent> | ||
<ChildContent> | ||
<MList> | ||
@foreach (var item in items) | ||
{ | ||
<MListItem Title="@item"/> | ||
} | ||
</MList> | ||
</ChildContent> | ||
</MMenu> | ||
</div> | ||
|
||
@code { | ||
|
||
private bool _on; | ||
|
||
string[] items = | ||
[ | ||
"Click Me", | ||
"Click Me", | ||
"Click Me", | ||
"Click Me 2" | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div class="text-center"> | ||
<MToggle @bind-Value="_on" DataOn="_dataOn" DataOff="_dataOff"> | ||
<MButton OnClick="@context.Toggle" LeftIconName="@context.Data?.Icon"> | ||
@context.Data?.Label | ||
</MButton> | ||
</MToggle> | ||
</div> | ||
|
||
@code { | ||
|
||
private bool _on; | ||
|
||
private record Model(string Label, string Icon); | ||
|
||
private Model _dataOn = new Model("On", "mdi-check"); | ||
private Model _dataOff = new Model("Off", "mdi-close"); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="text-center"> | ||
<MToggle @bind-Value="_on" DataOn="@("mdi-check")" DataOff="@("mdi-close")"> | ||
<MButton OnClick="@context.Toggle" IconName="@context.Data"/> | ||
</MToggle> | ||
</div> | ||
|
||
@code { | ||
|
||
private bool _on; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
docs/Masa.Blazor.Docs/wwwroot/data/apis/toggles/MToggle-en-US.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"props": { | ||
"dataOn": "The data when `Value` is `true`", | ||
"dataOff": "The data when `Value` is `false`" | ||
}, | ||
"events": { | ||
}, | ||
"contents": { | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
docs/Masa.Blazor.Docs/wwwroot/data/apis/toggles/MToggle-zh-CN.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"props": { | ||
"dataOn": "当 `Value` 为`true`时的值", | ||
"dataOff": "当 `Value` 为`false`时的值" | ||
}, | ||
"events": { | ||
}, | ||
"contents": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: Toggle | ||
desc: "The Toggle component is a wrapper that allows you to switch between two states." | ||
related: | ||
- /blazor/components/buttons | ||
- /blazor/components/icons | ||
- /blazor/components/switches | ||
--- | ||
|
||
## Usage | ||
|
||
<masa-example file="Examples.labs.toggles.Usage"></masa-example> | ||
|
||
## Examples | ||
|
||
### Props | ||
|
||
#### TData | ||
|
||
<masa-example file="Examples.labs.toggles.TData"></masa-example> | ||
|
||
### Misc | ||
|
||
#### Menu | ||
|
||
<masa-example file="Examples.labs.toggles.Menu"></masa-example> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: Toggle(切换) | ||
desc: "切换组件是一个包装器,允许您在两种状态之间切换。" | ||
related: | ||
- /blazor/components/buttons | ||
- /blazor/components/icons | ||
- /blazor/components/switches | ||
--- | ||
|
||
## 使用 {#usage} | ||
|
||
<masa-example file="Examples.labs.toggles.Usage"></masa-example> | ||
|
||
## 示例 {#examples} | ||
|
||
### 属性 {#props} | ||
|
||
#### TData | ||
|
||
<masa-example file="Examples.labs.toggles.TData"></masa-example> | ||
|
||
### 其他 {#misc} | ||
|
||
#### 菜单 {#menu} | ||
|
||
<masa-example file="Examples.labs.toggles.Menu"></masa-example> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace Masa.Blazor; | ||
|
||
public class MToggle<TData> : ComponentBase | ||
{ | ||
[Parameter] public bool Value { get; set; } | ||
|
||
[Parameter] public EventCallback<bool> ValueChanged { get; set; } | ||
|
||
[Parameter] public TData? DataOn { get; set; } | ||
|
||
[Parameter] public TData? DataOff { get; set; } | ||
|
||
[Parameter] public RenderFragment<ToggleContext<TData>>? ChildContent { get; set; } | ||
|
||
private async Task OnToggle() | ||
{ | ||
Value = !Value; | ||
await ValueChanged.InvokeAsync(Value); | ||
} | ||
|
||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
builder.AddContent( | ||
0, | ||
ChildContent?.Invoke(new ToggleContext<TData>(OnToggle, Value ? DataOn : DataOff, Value)) | ||
); | ||
} | ||
} | ||
|
||
public record ToggleContext<TData>(Func<Task> Toggle, TData? Data, bool On); |