Skip to content

Commit

Permalink
FC binding state
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Feb 4, 2024
1 parent a32916e commit a85105e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
20 changes: 16 additions & 4 deletions ReactWithDotNet/ElementSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,18 @@ propertyValue is Expression<Func<bool>>||
transformFunction = transformValueInClientAttribute?.TransformFunction
};

var (success, handlerComponentUniqueIdentifier) = GetHandlerComponentUniqueIdentifierFromBindingExpression(context,propertyValueAsLambdaExpression);
var (success, handlerComponentUniqueIdentifier, handlerIsReactComponent) = GetHandlerComponentUniqueIdentifierFromBindingExpression(context,propertyValueAsLambdaExpression);
if (!success)
{
throw HandlerMethodShouldBelongToReactComponent(propertyInfo, propertyValueAsLambdaExpression.ToString());
}

if (handlerIsReactComponent is false)
{
bindInfo.sourceIsState = true;
bindInfo.sourcePath = new[]{nameof(FunctionalComponent.State.Scope)}.Concat(bindInfo.sourcePath).ToList();
}

bindInfo.HandlerComponentUniqueIdentifier = handlerComponentUniqueIdentifier;

var debounceTimeout = instance.GetType().GetProperty(propertyInfo.Name + "DebounceTimeout")?.GetValue(instance) as int?;
Expand Down Expand Up @@ -625,11 +631,17 @@ static async Task<object> GetPropertyValueOfHtmlElement(ElementSerializerContext
transformFunction = propertyDefinition.transformValueInClient
};

var (success, handlerComponentUniqueIdentifier) = GetHandlerComponentUniqueIdentifierFromBindingExpression(context,propertyValueAsLambdaExpression);
var (success, handlerComponentUniqueIdentifier, handlerIsReactComponent) = GetHandlerComponentUniqueIdentifierFromBindingExpression(context,propertyValueAsLambdaExpression);
if (!success)
{
throw HandlerMethodShouldBelongToReactComponent(propertyDefinition.name, propertyValueAsLambdaExpression.ToString());
}

if (handlerIsReactComponent is false)
{
bindInfo.sourceIsState = true;
bindInfo.sourcePath = new[]{nameof(FunctionalComponent.State.Scope)}.Concat(bindInfo.sourcePath).ToList();
}

bindInfo.HandlerComponentUniqueIdentifier = handlerComponentUniqueIdentifier;

Expand Down Expand Up @@ -659,7 +671,7 @@ static async Task<object> GetPropertyValueOfHtmlElement(ElementSerializerContext
return propertyValue;
}

static (bool success, int value) GetHandlerComponentUniqueIdentifierFromBindingExpression(ElementSerializerContext context, LambdaExpression lambdaExpression)
static (bool success, int value, bool handlerIsReactComponent) GetHandlerComponentUniqueIdentifierFromBindingExpression(ElementSerializerContext context, LambdaExpression lambdaExpression)
{
var (success, targetValue) = GetTargetValueFromExpression(lambdaExpression);
if (success)
Expand All @@ -668,7 +680,7 @@ static async Task<object> GetPropertyValueOfHtmlElement(ElementSerializerContext

if (handlerComponentUniqueIdentifier.HasValue)
{
return (true, handlerComponentUniqueIdentifier.Value);
return (true, handlerComponentUniqueIdentifier.Value, targetValue is ReactComponentBase);
}
}

Expand Down
31 changes: 31 additions & 0 deletions ReactWithDotNet/FC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ protected override Element render()
{
return new FlexColumn(WidthMaximized, HeightMaximized)
{
BindingSample(7),
AAAA(6),
AAAA(7),
AAAA(8),
Expand All @@ -198,6 +200,35 @@ Task OnClickHandler(MouseEvent e)
return Task.CompletedTask;
}
}
static FC BindingSample(int start)
{
var count = start;
var text = "label: "+ count;
return cmp =>
{
return new FlexColumn
{
new input
{
valueBind = ()=>count,
},
new label{ text },
new button(OnClick(OnClickHandler)){ "Update Count"}
};
Task OnClickHandler(MouseEvent e)
{
text = "label: "+ count;
return Task.CompletedTask;
}
};
}
static FC AAAA(int start)
{
Expand Down

0 comments on commit a85105e

Please sign in to comment.