From 36ab66c58d2c618ac6e9a1e1f5ceb61d7518f699 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Thu, 13 Apr 2023 16:34:27 +0400 Subject: [PATCH] refactor: Input formatter types are reduced to basic types (deny-allow) and updated example jsons --- .../assets/json/text_field_example.json | 8 +- .../mirai_input_formatter.g.dart | 3 - .../mirai_text_field_parser.dart | 9 +- .../mirai_text_form_field_parser.dart | 9 +- packages/mirai/lib/src/parsers/parsers.dart | 1 + .../mirai/lib/src/utils/input_formatters.dart | 92 +------------------ 6 files changed, 20 insertions(+), 102 deletions(-) diff --git a/examples/mirai_gallery/assets/json/text_field_example.json b/examples/mirai_gallery/assets/json/text_field_example.json index 5d4b1594..4d850a62 100644 --- a/examples/mirai_gallery/assets/json/text_field_example.json +++ b/examples/mirai_gallery/assets/json/text_field_example.json @@ -83,7 +83,8 @@ }, "inputFormatters": [ { - "type": "digitsOnly" + "type": "deny", + "rule": "[a-zA-Z]" } ], "readOnly": false, @@ -144,9 +145,8 @@ }, "inputFormatters": [ { - "type": "masked", - "rule": "XXXX-XXXX-XXXX-XXXX", - "value": "-" + "type": "deny", + "rule": "[a-zA-Z]" } ], "readOnly": false, diff --git a/packages/mirai/lib/src/parsers/mirai_input_formatters/mirai_input_formatter.g.dart b/packages/mirai/lib/src/parsers/mirai_input_formatters/mirai_input_formatter.g.dart index 239a3187..ca77c1e1 100644 --- a/packages/mirai/lib/src/parsers/mirai_input_formatters/mirai_input_formatter.g.dart +++ b/packages/mirai/lib/src/parsers/mirai_input_formatters/mirai_input_formatter.g.dart @@ -23,9 +23,6 @@ Map _$$_MiraiInputFormatterToJson( }; const _$InputFormatterTypeEnumMap = { - InputFormatterType.digitsOnly: 'digitsOnly', - InputFormatterType.singleLineFormatter: 'singleLineFormatter', InputFormatterType.allow: 'allow', InputFormatterType.deny: 'deny', - InputFormatterType.masked: 'masked', }; diff --git a/packages/mirai/lib/src/parsers/mirai_text_field/mirai_text_field_parser.dart b/packages/mirai/lib/src/parsers/mirai_text_field/mirai_text_field_parser.dart index aaeb053c..3b066fd8 100644 --- a/packages/mirai/lib/src/parsers/mirai_text_field/mirai_text_field_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_text_field/mirai_text_field_parser.dart @@ -1,10 +1,10 @@ import 'package:flutter/material.dart'; import 'package:mirai/src/framework/framework.dart'; import 'package:mirai/src/parsers/mirai_input_decoration/mirai_input_decoration.dart'; +import 'package:mirai/src/parsers/mirai_input_formatters/mirai_input_formatter.dart'; import 'package:mirai/src/parsers/mirai_text_field/mirai_text_field.dart'; import 'package:mirai/src/parsers/mirai_text_style/mirai_text_style.dart'; import 'package:mirai/src/utils/color_utils.dart'; -import 'package:mirai/src/utils/input_formatters.dart'; import 'package:mirai/src/utils/widget_type.dart'; class MiraiTextFieldParser extends MiraiParser { @@ -52,9 +52,10 @@ class MiraiTextFieldParser extends MiraiParser { cursorColor: model.cursorColor?.toColor, style: model.style?.parse, decoration: model.decoration?.parse(context), - inputFormatters: model.inputFormatters.map((formatter) { - return InputFormatters.format(formatter); - }).toList(), + inputFormatters: model.inputFormatters + .map((MiraiInputFormatter formatter) => + formatter.type.format(formatter.rule ?? "")) + .toList(), ); } } diff --git a/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart b/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart index ba114058..1f772649 100644 --- a/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart @@ -2,10 +2,10 @@ import 'package:flutter/material.dart'; import 'package:mirai/src/framework/framework.dart'; import 'package:mirai/src/parsers/mirai_edge_insets/mirai_edge_insets.dart'; import 'package:mirai/src/parsers/mirai_input_decoration/mirai_input_decoration.dart'; +import 'package:mirai/src/parsers/mirai_input_formatters/mirai_input_formatter.dart'; import 'package:mirai/src/parsers/mirai_text_form_field/mirai_text_form_field.dart'; import 'package:mirai/src/parsers/mirai_text_style/mirai_text_style.dart'; import 'package:mirai/src/utils/color_utils.dart'; -import 'package:mirai/src/utils/input_formatters.dart'; import 'package:mirai/src/utils/widget_type.dart'; class MiraiTextFormFieldParser extends MiraiParser { @@ -60,9 +60,10 @@ class MiraiTextFormFieldParser extends MiraiParser { cursorColor: model.cursorColor?.toColor, style: model.style?.parse, decoration: model.decoration.parse(context), - inputFormatters: model.inputFormatters.map((formatter) { - return InputFormatters.format(formatter); - }).toList(), + inputFormatters: model.inputFormatters + .map((MiraiInputFormatter formatter) => + formatter.type.format(formatter.rule ?? "")) + .toList(), ); } } diff --git a/packages/mirai/lib/src/parsers/parsers.dart b/packages/mirai/lib/src/parsers/parsers.dart index 5eb0fac0..0eb95827 100644 --- a/packages/mirai/lib/src/parsers/parsers.dart +++ b/packages/mirai/lib/src/parsers/parsers.dart @@ -16,6 +16,7 @@ export 'package:mirai/src/parsers/mirai_icon/mirai_icon.dart'; export 'package:mirai/src/parsers/mirai_icon_button/mirai_icon_button.dart'; export 'package:mirai/src/parsers/mirai_image/mirai_image.dart'; export 'package:mirai/src/parsers/mirai_input_decoration/mirai_input_decoration.dart'; +export 'package:mirai/src/parsers/mirai_input_formatters/mirai_input_formatter.dart'; export 'package:mirai/src/parsers/mirai_list_tile/mirai_list_tile.dart'; export 'package:mirai/src/parsers/mirai_list_view/mirai_list_view.dart'; export 'package:mirai/src/parsers/mirai_navigation_bar_item/mirai_bottom_navigation_bar_item.dart'; diff --git a/packages/mirai/lib/src/utils/input_formatters.dart b/packages/mirai/lib/src/utils/input_formatters.dart index efa63244..288a14ba 100644 --- a/packages/mirai/lib/src/utils/input_formatters.dart +++ b/packages/mirai/lib/src/utils/input_formatters.dart @@ -1,102 +1,20 @@ import 'package:flutter/services.dart'; -import 'package:mirai/src/parsers/mirai_input_formatters/mirai_input_formatter.dart'; enum InputFormatterType { - digitsOnly, - singleLineFormatter, allow, - deny, - masked, -} + deny; -class InputFormatters { - static TextInputFormatter format(MiraiInputFormatter inputFormatter) { + TextInputFormatter format(String rule) { try { - switch (inputFormatter.type) { - case InputFormatterType.digitsOnly: - return FilteringTextInputFormatter.digitsOnly; - - case InputFormatterType.singleLineFormatter: - return FilteringTextInputFormatter.singleLineFormatter; - + switch (this) { case InputFormatterType.allow: - return FilteringTextInputFormatter.allow( - RegExp(inputFormatter.rule ?? "")); + return FilteringTextInputFormatter.allow(RegExp(rule)); case InputFormatterType.deny: - return FilteringTextInputFormatter.allow( - RegExp(inputFormatter.rule ?? "")); - - case InputFormatterType.masked: - return MaskedTextInputFormatter( - mask: inputFormatter.rule ?? "", - separator: inputFormatter.value ?? "-", - ); + return FilteringTextInputFormatter.deny(RegExp(rule)); } } catch (_) { return FilteringTextInputFormatter.allow(RegExp('')); } } } - -class CardNumberFormatter extends TextInputFormatter { - @override - TextEditingValue formatEditUpdate( - TextEditingValue oldValue, - TextEditingValue newValue, - ) { - var inputText = newValue.text; - - if (newValue.selection.baseOffset == 0) { - return newValue; - } - - var bufferString = StringBuffer(); - for (int i = 0; i < inputText.length; i++) { - bufferString.write(inputText[i]); - var nonZeroIndexValue = i + 1; - if (nonZeroIndexValue % 4 == 0 && nonZeroIndexValue != inputText.length) { - bufferString.write(' '); - } - } - - var string = bufferString.toString(); - return newValue.copyWith( - text: string, - selection: TextSelection.collapsed( - offset: string.length, - ), - ); - } -} - -class MaskedTextInputFormatter extends TextInputFormatter { - final String mask; - final String separator; - - MaskedTextInputFormatter({ - required this.mask, - required this.separator, - }); - - @override - TextEditingValue formatEditUpdate( - TextEditingValue oldValue, TextEditingValue newValue) { - if (newValue.text.isNotEmpty) { - if (newValue.text.length > oldValue.text.length) { - if (newValue.text.length > mask.length) return oldValue; - if (newValue.text.length < mask.length && - mask[newValue.text.length - 1] == separator) { - return TextEditingValue( - text: - '${oldValue.text}$separator${newValue.text.substring(newValue.text.length - 1)}', - selection: TextSelection.collapsed( - offset: newValue.selection.end + 1, - ), - ); - } - } - } - return newValue; - } -}