-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from EasyAbp/more-form-item-types
Add `NumberBox`, `Toggle`, `TimePicker`, and `ColorPicker`.
- Loading branch information
Showing
32 changed files
with
1,000 additions
and
207 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
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
9 changes: 9 additions & 0 deletions
9
...n.Core/EasyAbp/DynamicForm/FormItemTypes/ColorPicker/ColorPickerFormItemConfigurations.cs
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,9 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace EasyAbp.DynamicForm.FormItemTypes.ColorPicker; | ||
|
||
public class ColorPickerFormItemConfigurations | ||
{ | ||
[CanBeNull] | ||
public string RegexPattern { get; set; } | ||
} |
87 changes: 87 additions & 0 deletions
87
....Domain.Core/EasyAbp/DynamicForm/FormItemTypes/ColorPicker/ColorPickerFormItemProvider.cs
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,87 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using EasyAbp.DynamicForm.Shared; | ||
using Volo.Abp; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace EasyAbp.DynamicForm.FormItemTypes.ColorPicker; | ||
|
||
public partial class ColorPickerFormItemProvider : FormItemProviderBase, IScopedDependency | ||
{ | ||
public static string Name { get; set; } = "ColorPicker"; | ||
public static string LocalizationItemKey { get; set; } = "FormItemType.ColorPicker"; | ||
|
||
public override Task ValidateTemplateAsync(IFormItemMetadata metadata) | ||
{ | ||
var configurations = GetConfigurations<ColorPickerFormItemConfigurations>(metadata); | ||
|
||
if (!configurations.RegexPattern.IsNullOrWhiteSpace()) | ||
{ | ||
try | ||
{ | ||
_ = Regex.Match(string.Empty, configurations.RegexPattern!); | ||
} | ||
catch | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.InvalidRegexPattern) | ||
.WithData("item", metadata.Name); | ||
} | ||
} | ||
|
||
if (metadata.AvailableValues.Any(x => !HexRegex().IsMatch(x))) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.ColorPickerInvalidHexValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task ValidateValueAsync(IFormItemMetadata metadata, string value) | ||
{ | ||
var isEmptyValue = value.IsNullOrWhiteSpace(); | ||
var configurations = GetConfigurations<ColorPickerFormItemConfigurations>(metadata); | ||
|
||
if (!metadata.Optional && isEmptyValue) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.FormItemValueIsRequired) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
if (isEmptyValue) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
if (!HexRegex().IsMatch(value)) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.ColorPickerInvalidHexValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
if (!configurations.RegexPattern.IsNullOrWhiteSpace() && !Regex.IsMatch(value!, configurations.RegexPattern!)) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.InvalidFormItemValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
if (metadata.AvailableValues.Any() && | ||
!metadata.AvailableValues.Contains(value, StringComparer.InvariantCultureIgnoreCase)) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.InvalidFormItemValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task<object> CreateConfigurationsObjectOrNullAsync() | ||
{ | ||
return Task.FromResult<object>(new ColorPickerFormItemConfigurations()); | ||
} | ||
|
||
[GeneratedRegex("^#(?:[0-9a-fA-F]{3,4}){1,2}$")] | ||
private static partial Regex HexRegex(); | ||
} |
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
8 changes: 3 additions & 5 deletions
8
...EasyAbp.DynamicForm.Domain.Core/EasyAbp/DynamicForm/FormItemTypes/FormItemProviderBase.cs
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
12 changes: 12 additions & 0 deletions
12
...omain.Core/EasyAbp/DynamicForm/FormItemTypes/NumberBox/NumberBoxFormItemConfigurations.cs
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,12 @@ | ||
namespace EasyAbp.DynamicForm.FormItemTypes.NumberBox; | ||
|
||
public class NumberBoxFormItemConfigurations | ||
{ | ||
public byte DecimalPlaces { get; set; } | ||
|
||
public long? MaxValue { get; set; } | ||
|
||
public long? MinValue { get; set; } | ||
|
||
public NumberUi NumberUi { get; set; } | ||
} |
84 changes: 84 additions & 0 deletions
84
...Form.Domain.Core/EasyAbp/DynamicForm/FormItemTypes/NumberBox/NumberBoxFormItemProvider.cs
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,84 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using EasyAbp.DynamicForm.Shared; | ||
using Volo.Abp; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace EasyAbp.DynamicForm.FormItemTypes.NumberBox; | ||
|
||
public class NumberBoxFormItemProvider : FormItemProviderBase, IScopedDependency | ||
{ | ||
public static string Name { get; set; } = "NumberBox"; | ||
public static string LocalizationItemKey { get; set; } = "FormItemType.NumberBox"; | ||
|
||
public override Task ValidateTemplateAsync(IFormItemMetadata metadata) | ||
{ | ||
var configurations = GetConfigurations<NumberBoxFormItemConfigurations>(metadata); | ||
|
||
if (configurations.MinValue.HasValue && configurations.MaxValue.HasValue && | ||
configurations.MinValue > configurations.MaxValue) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.NumberBoxInvalidMaxValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
if (metadata.AvailableValues.Any(x => !decimal.TryParse(x, out _))) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.InvalidFormItemValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task ValidateValueAsync(IFormItemMetadata metadata, string value) | ||
{ | ||
var isEmptyValue = value.IsNullOrWhiteSpace(); | ||
|
||
if (!metadata.Optional && isEmptyValue) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.FormItemValueIsRequired) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
if (isEmptyValue) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
if (!decimal.TryParse(value, out var numericValue)) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.NumberBoxInvalidNumericValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
var configurations = GetConfigurations<NumberBoxFormItemConfigurations>(metadata); | ||
|
||
int decimalPlaces = BitConverter.GetBytes(decimal.GetBits(numericValue)[3])[2]; | ||
|
||
if (configurations.DecimalPlaces < decimalPlaces || | ||
configurations.MinValue.HasValue && numericValue < configurations.MinValue || | ||
(configurations.MaxValue.HasValue && numericValue > configurations.MaxValue)) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.NumberBoxInvalidValue) | ||
.WithData("item", metadata.Name) | ||
.WithData("decimalPlaces", configurations.DecimalPlaces) | ||
.WithData("min", configurations.MinValue.HasValue ? configurations.MinValue.Value : "-∞") | ||
.WithData("max", configurations.MaxValue.HasValue ? configurations.MaxValue.Value : "∞"); | ||
} | ||
|
||
if (metadata.AvailableValues.Any() && !metadata.AvailableValues.Select(decimal.Parse).Contains(numericValue)) | ||
{ | ||
throw new BusinessException(DynamicFormCoreErrorCodes.InvalidFormItemValue) | ||
.WithData("item", metadata.Name); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task<object> CreateConfigurationsObjectOrNullAsync() | ||
{ | ||
return Task.FromResult<object>(new NumberBoxFormItemConfigurations()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/EasyAbp.DynamicForm.Domain.Core/EasyAbp/DynamicForm/FormItemTypes/NumberBox/NumberUi.cs
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,23 @@ | ||
namespace EasyAbp.DynamicForm.FormItemTypes.NumberBox; | ||
|
||
public enum NumberUi | ||
{ | ||
Default = 0, | ||
|
||
/// <summary> | ||
/// Normal numerical input box. | ||
/// </summary> | ||
NumberInput = 1, | ||
|
||
/// <summary> | ||
/// The <see cref="Slider"/> provides a ranged number selection. | ||
/// High-precision decimals or a wider range of numbers may not be well-supported. | ||
/// </summary> | ||
Slider = 2, | ||
|
||
/// <summary> | ||
/// The <see cref="Rating"/> provides a simple stars selection, usually integers like 1 to 5. | ||
/// High-precision decimals or a wider range of numbers may not be well-supported. | ||
/// </summary> | ||
Rating = 3 | ||
} |
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
Oops, something went wrong.