Skip to content

Commit

Permalink
refactor: move all watchers execution to first render (#1020)
Browse files Browse the repository at this point in the history
* 🚧 feat: wip

* ♻ refactor: refactor the radio group

* 🐛 fix(TextField): CounterValue should not compute the NULL

* ♻ refactor: move all watchers to the first render

* 🚧 feat: wip

* ⚰ refactor: remove the debug code

* use Fixed instead of App on mobile

* ⚰ refactor: remove unused usings
  • Loading branch information
capdiem authored Feb 21, 2023
1 parent cdbad68 commit 07baa36
Show file tree
Hide file tree
Showing 28 changed files with 187 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ the [MList](/blazor/components/lists) component using the **Nav** property.

<app-alert type="error" content="If you are using **MNavigationDrawer** with **App** property enabled, you don't need to use `Absolute` prop as in examples."></app-alert>

<app-alert type="info" content="The `ExpandOnHover` prop does not alter the content area of **MMain**. To have content area respond to `ExpandOnHover`, bind `OnMiniVariantUpdate` to a data prop."></app-alert>
<app-alert type="info" content="The `ExpandOnHover` prop does not alter the content area of **MMain**. To have content area respond to `ExpandOnHover`, please set `@bind-MiniVariant`."></app-alert>

## Examples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ related:

<app-alert type="error" content="如果使用启用了 **App** 属性的 **MNavigationDrawer**,则不需要像示例中那样使用 `Absolute` 属性。"></app-alert>

<app-alert type="info" content="`ExtensionOnHover` 参数不会改变**MMain**的内容区域。 要使内容区域响应`ExtensionOnHover`,请绑定`OnMiniVariantUpdate` 到数据"></app-alert>
<app-alert type="info" content="`ExpandOnHover` 参数不会改变**MMain**的内容区域,即不会改变 **MMain** 的左内边距。 要使内容区域响应`ExpandOnHover`,请设置 `@bind-MinVariant`"></app-alert>

## 示例

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Class="pt-2"
Width="240"
Right
App
Fixed
Floating
Temporary>
<MList Nav @key="Value">
Expand Down
4 changes: 1 addition & 3 deletions docs/Masa.Docs.Shared/Shared/NavigationDrawer.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@using System.Text;
@inject AppService AppService
@inject DocService DocService
@inject DocService DocService
@inject MasaBlazor MasaBlazor

<MNavigationDrawer Value="Value"
Expand Down
6 changes: 3 additions & 3 deletions src/Masa.Blazor/Components/Autocomplete/MAutocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ protected override bool MenuCanShow

protected override bool EnableSpaceKeDownPreventDefault => false;

protected override void OnWatcherInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnWatcherInitialized();
base.RegisterWatchers(watcher);

Watcher
watcher
.Watch<string>(nameof(InternalSearch), (val) => _ = SetValueByJsInterop(val))
.Watch<List<TItem>>(nameof(FilteredItems), OnFilteredItemsChanged);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Masa.Blazor/Components/Carousel/MCarousel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ protected override void SetComponentClass()
.Apply<BProgressLinear, MProgressLinear>();
}

protected override void OnWatcherInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnWatcherInitialized();
base.RegisterWatchers(watcher);

Watcher.Watch<StringNumber>(nameof(Value), RestartTimeout)
watcher.Watch<StringNumber>(nameof(Value), RestartTimeout)
.Watch<int>(nameof(Interval), RestartTimeout)
.Watch<StringNumber>(nameof(Height), (val, oldVal) =>
{
Expand Down
6 changes: 3 additions & 3 deletions src/Masa.Blazor/Components/DataIterator/MDataIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ public override Task SetParametersAsync(ParameterView parameters)
return base.SetParametersAsync(parameters);
}

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();
base.RegisterWatchers(watcher);

Watcher
watcher
.Watch<IEnumerable<TItem>>(nameof(Value), val =>
{
var keys = new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ public override DateOnly TableDate
}
}

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();

Watcher
base.RegisterWatchers(watcher);
watcher
.Watch<DateOnly>(nameof(TableDate), (newVal, oldVal) =>
{
IsReversing = newVal < oldVal;
Expand Down
24 changes: 11 additions & 13 deletions src/Masa.Blazor/Components/DatePicker/MDatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,22 @@ protected DateOnly LastValue
}
}

public override Task SetParametersAsync(ParameterView parameters)
protected override void OnInitialized()
{


return base.SetParametersAsync(parameters);
base.OnInitialized();

InternalActivePicker = ActivePicker ?? Type;

//Init TableDate
var multipleValue = WrapInArray(Value);
TableDate = multipleValue.Count > 0 ? multipleValue[multipleValue.Count - 1] : (ShowCurrent.IsT0 ? ShowCurrent.AsT0 : DateOnly.FromDateTime(DateTime.Now));
}

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();
base.RegisterWatchers(watcher);

Watcher
watcher
.Watch<DatePickerType?>(nameof(ActivePicker), val =>
{
InternalActivePicker = val.Value;
Expand All @@ -265,12 +269,6 @@ protected override void OnInitialized()
var multipleValue = WrapInArray(val);
TableDate = multipleValue.Count > 0 ? multipleValue[multipleValue.Count - 1] : (ShowCurrent.IsT0 ? ShowCurrent.AsT0 : DateOnly.FromDateTime(DateTime.Now));
});

InternalActivePicker = ActivePicker ?? Type;

//Init TableDate
var multipleValue = WrapInArray(Value);
TableDate = multipleValue.Count > 0 ? multipleValue[multipleValue.Count - 1] : (ShowCurrent.IsT0 ? ShowCurrent.AsT0 : DateOnly.FromDateTime(DateTime.Now));
}

private IList<DateOnly> WrapInArray(TValue value)
Expand Down
8 changes: 4 additions & 4 deletions src/Masa.Blazor/Components/DatePicker/MDatePickerHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ public static DateOnly MonthChange(DateOnly value, int sign)
return new DateOnly(date.Year, date.Month, 1);
}

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();

Watcher
base.RegisterWatchers(watcher);
watcher
.Watch<DateOnly>(nameof(Value), (newVal, oldVal) =>
{
IsReversing = newVal < oldVal;
Expand Down
6 changes: 3 additions & 3 deletions src/Masa.Blazor/Components/DatePicker/MDatePickerTitle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public async Task HandleOnTitleDateBtnClickAsync(MouseEventArgs arg)
}
}

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();
base.RegisterWatchers(watcher);

Watcher
watcher
.Watch<DateOnly>(nameof(Value), (val, prev) =>
{
IsReversing = val < prev;
Expand Down
7 changes: 4 additions & 3 deletions src/Masa.Blazor/Components/Editor/MEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ public override string Value
private IJSObjectReference QuillHelper { get; set; }
private DotNetObjectReference<MEditor> ObjRef { get; set; }

protected override async Task OnInitializedAsync()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
await base.OnInitializedAsync();
Watcher
base.RegisterWatchers(watcher);

watcher
.Watch<string>(nameof(Value), async val =>
{
if (_waitingUpdate && _editorRendered)
Expand Down
7 changes: 6 additions & 1 deletion src/Masa.Blazor/Components/Footer/MFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ protected override void OnInitialized()
base.OnInitialized();

MasaBlazor.Application.PropertyChanged += ApplicationPropertyChanged;
}

protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.RegisterWatchers(watcher);

Watcher.Watch<bool>(nameof(App), (_, prev) =>
watcher.Watch<bool>(nameof(App), (_, prev) =>
{
if (prev)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Masa.Blazor/Components/Gridstack/MGridstack.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public override async Task SetParametersAsync(ParameterView parameters)
ArgumentNullException.ThrowIfNull(ItemKey);
}

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();
base.RegisterWatchers(watcher);

Watcher.Watch<bool>(nameof(Readonly), (val) => { _ = SetStatic(val); });
watcher.Watch<bool>(nameof(Readonly), (val) => { _ = SetStatic(val); });
}

protected override void SetComponentClass()
Expand Down
6 changes: 3 additions & 3 deletions src/Masa.Blazor/Components/Input/MInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public virtual string ValidationState

public virtual bool IsLabelActive => IsDirty;

protected override void OnWatcherInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnWatcherInitialized();
Watcher.Watch<IEnumerable<Func<TValue, StringBoolean>>>(nameof(Rules), () =>
base.RegisterWatchers(watcher);
watcher.Watch<IEnumerable<Func<TValue, StringBoolean>>>(nameof(Rules), () =>
{
// waiting for InternalValue to be assigned
NextTick(InternalValidate);
Expand Down
20 changes: 10 additions & 10 deletions src/Masa.Blazor/Components/Map/BaiduMap/MBaiduMap.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
}
}

protected override void OnWatcherInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnWatcherInitialized();
base.RegisterWatchers(watcher);

Watcher.Watch<float>(nameof(Zoom), async (val) =>
watcher.Watch<float>(nameof(Zoom), async (val) =>
{
if (_zoomChangedInJs)
{
Expand All @@ -186,35 +186,35 @@ protected override void OnWatcherInitialized()
await _jsMap.TryInvokeVoidAsync("setZoom", val);
});

Watcher.Watch<float>(nameof(MaxZoom), async (val) =>
watcher.Watch<float>(nameof(MaxZoom), async (val) =>
{
await _jsMap.TryInvokeVoidAsync("setMaxZoom", val);

if (Zoom > val)
Zoom = val;
});

Watcher.Watch<float>(nameof(MinZoom), async (val) =>
watcher.Watch<float>(nameof(MinZoom), async (val) =>
{
await _jsMap.TryInvokeVoidAsync("setMinZoom", val);

if (Zoom < val)
Zoom = val;
});

Watcher.Watch<bool>(nameof(EnableScrollWheelZoom), async (val) =>
watcher.Watch<bool>(nameof(EnableScrollWheelZoom), async (val) =>
await _jsMap.TryInvokeVoidAsync(val ? "enableScrollWheelZoom" : "disableScrollWheelZoom"));

Watcher.Watch<BaiduMapType>(nameof(MapType), async (val) =>
watcher.Watch<BaiduMapType>(nameof(MapType), async (val) =>
await _jsMap.TryInvokeVoidAsync("setMapType", BaiduMapTypeName[val]));

Watcher.Watch<bool>(nameof(TrafficOn), async (val) =>
watcher.Watch<bool>(nameof(TrafficOn), async (val) =>
await _jsMap.TryInvokeVoidAsync(val ? "setTrafficOn" : "setTrafficOff"));

Watcher.Watch<bool>(nameof(Dark), async (val) =>
watcher.Watch<bool>(nameof(Dark), async (val) =>
await _jsMap.TryInvokeVoidAsync("setMapStyleV2", new { StyleId = val ? DarkThemeId : string.Empty }));

Watcher.Watch<GeoPoint>(nameof(Center), async (val) =>
watcher.Watch<GeoPoint>(nameof(Center), async (val) =>
{
if (_centerChangedInJs)
{
Expand Down
7 changes: 4 additions & 3 deletions src/Masa.Blazor/Components/Markdown/MMarkdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public override string Value

private IJSObjectReference VditorHelper { get; set; }

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();
Watcher
base.RegisterWatchers(watcher);

watcher
.Watch<string>(nameof(Value), async val =>
{
if (_wattingUpdate && _editorRendered)
Expand Down
6 changes: 3 additions & 3 deletions src/Masa.Blazor/Components/MarkdownIt/MMarkdownIt.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ public string[]? Quotes
private string? _prevSource;
private IJSObjectReference? _markdownIt;

protected override void OnInitialized()
protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.OnInitialized();
base.RegisterWatchers(watcher);

Watcher
watcher
.Watch<bool>(nameof(Html), GoCreateMarkdownItProxy)
.Watch<bool>(nameof(XHtmlOut), GoCreateMarkdownItProxy)
.Watch<bool>(nameof(Breaks), GoCreateMarkdownItProxy)
Expand Down
Loading

0 comments on commit 07baa36

Please sign in to comment.