// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
namespace Microsoft.AspNetCore.Blazor.Components
{
///
/// Methods used internally by @bind syntax. Not intended to be used directly.
///
public static class BindMethods
{
///
/// Not intended to be used directly.
///
public static T GetValue(T value) => value;
///
/// Not intended to be used directly.
///
public static string GetValue(DateTime value, string format) =>
value == default ? null
: (format == null ? value.ToString() : value.ToString(format));
///
/// Not intended to be used directly.
///
public static UIEventHandler SetValueHandler(Action setter, string existingValue)
{
return _ => setter((string)((UIChangeEventArgs)_).Value);
}
///
/// Not intended to be used directly.
///
public static UIEventHandler SetValueHandler(Action setter, bool existingValue)
{
return _ => setter((bool)((UIChangeEventArgs)_).Value);
}
///
/// Not intended to be used directly.
///
public static UIEventHandler SetValueHandler(Action setter, int existingValue)
{
return _ => setter(int.Parse((string)((UIChangeEventArgs)_).Value));
}
///
/// Not intended to be used directly.
///
public static UIEventHandler SetValueHandler(Action setter, DateTime existingValue)
{
return _ => SetDateTimeValue(setter, (object)((UIChangeEventArgs)_).Value, null);
}
///
/// Not intended to be used directly.
///
public static UIEventHandler SetValueHandler(Action setter, DateTime existingValue, string format)
{
return _ => SetDateTimeValue(setter, (object)((UIChangeEventArgs)_).Value, format);
}
///
/// Not intended to be used directly.
///
public static UIEventHandler SetValueHandler(Action setter, T existingValue)
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException($"'bind' does not accept values of type {typeof(T).FullName}. To read and write this value type, wrap it in a property of type string with suitable getters and setters.");
}
return _ =>
{
var value = (string)((UIChangeEventArgs)_).Value;
var parsed = (T)Enum.Parse(typeof(T), value);
setter(parsed);
};
}
///
/// Not intended to be used directly.
///
public static Action