Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 0fb50e8

Browse files
authored
Merge pull request #342 from wglios/dev
add WhitelistingTextInputFormatter
2 parents 6f8b50a + 532638d commit 0fb50e8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Runtime/service/text_formatter.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, Tex
8181
}
8282
}
8383

84+
public class WhitelistingTextInputFormatter : TextInputFormatter {
85+
public WhitelistingTextInputFormatter(Regex whitelistedPattern) {
86+
D.assert(whitelistedPattern != null);
87+
this.whitelistedPattern = whitelistedPattern;
88+
}
89+
90+
readonly Regex whitelistedPattern;
91+
92+
public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
93+
return Util._selectionAwareTextManipulation(
94+
value: newValue,
95+
substringManipulation: substring => {
96+
string groups = "";
97+
foreach (Match match in this.whitelistedPattern.Matches(input: substring)) {
98+
groups += match.Groups[0].Value;
99+
}
100+
101+
return groups;
102+
}
103+
);
104+
}
105+
106+
public static readonly WhitelistingTextInputFormatter digitsOnly
107+
= new WhitelistingTextInputFormatter(new Regex(@"\d+"));
108+
}
109+
84110
static class Util {
85111
internal static TextEditingValue _selectionAwareTextManipulation(TextEditingValue value,
86112
Func<string, string> substringManipulation) {

0 commit comments

Comments
 (0)