Skip to content

Commit 9e1afa1

Browse files
authored
fix(TextInputBase): correct counter limit handling (#428)
## Short description This pull request fixes the issue where the `counterLimit` was not correctly accounting for the spaces added by the `valueFormat` function in the `TextInputBase` component. This ensures that the actual text length (excluding spaces) is compared against the `counterLimit`. ## List of changes proposed in this pull request - Updated the `onChangeTextHandler` to correctly handle the `counterLimit` by calculating the actual text length without spaces - Adjusted the `maxLength` prop of the `TextInput` component to account for the spaces added by the `valueFormat` function - Ensured that the `counterLimit` is respected correctly for formatted text inputs such as `credit-card` and `iban` ## How to test - Set a `counterLimit` in `DSTextFields` - Insert some value in `credit-card` and `iban` `TextInput` - Ensure that now the `counterLimit` displaying the correct number ## Preview | Old | New | |--------|--------| | <video src="https://github.com/user-attachments/assets/6449ed43-75fb-4eec-b633-2a018c7e78e8" /> | <video src="https://github.com/user-attachments/assets/0de2d7f0-2b01-4751-b759-a0c41108198e"/> | ### Prevent counter updating on wrong value | Old | New | |--------|--------| | <video src="https://github.com/user-attachments/assets/bb0bab8f-bb4f-4433-aa10-f2ed1e87cfa1" /> | <video src="https://github.com/user-attachments/assets/7518a037-e9d4-4b48-aba8-2877b2d71e90"/> |
1 parent 432f4e5 commit 9e1afa1

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

src/components/textInput/TextInputBase.tsx

+38-8
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,27 @@ const styles = StyleSheet.create({
120120

121121
type InputTextHelperRow = Pick<
122122
InputTextProps,
123-
"value" | "counterLimit" | "bottomMessage" | "bottomMessageColor"
123+
| "value"
124+
| "counterLimit"
125+
| "bottomMessage"
126+
| "bottomMessageColor"
127+
| "inputType"
124128
>;
125129

126130
const HelperRow = ({
127131
value,
128132
counterLimit,
129133
bottomMessage,
130-
bottomMessageColor
134+
bottomMessageColor,
135+
inputType
131136
}: InputTextHelperRow) => {
132137
const theme = useIOTheme();
133-
const valueCount = useMemo(() => value.length, [value]);
138+
139+
const valueCount = useMemo(
140+
() =>
141+
inputType !== "default" ? value.replace(/\s/g, "").length : value.length,
142+
[inputType, value]
143+
);
134144

135145
const bottomMessageColorDefault: IOColors = theme["textBody-tertiary"];
136146
const bottomMessageColorValue =
@@ -315,12 +325,22 @@ export const TextInputBase = ({
315325

316326
const onChangeTextHandler = useCallback(
317327
(text: string) => {
318-
if (counterLimit && text.length > counterLimit) {
328+
const actualTextLength =
329+
inputType !== "default" ? text.replace(/\s/g, "").length : text.length;
330+
331+
if (counterLimit && actualTextLength > counterLimit) {
319332
return;
320333
}
321-
onChangeText(text);
334+
335+
if (inputType !== "default") {
336+
// necessary to omit whitespaces added by the valueFormat function
337+
const formattedText = text.replace(/\s/g, "");
338+
onChangeText(formattedText);
339+
} else {
340+
onChangeText(text);
341+
}
322342
},
323-
[counterLimit, onChangeText]
343+
[counterLimit, onChangeText, inputType]
324344
);
325345

326346
const onBlurHandler = useCallback(() => {
@@ -348,6 +368,15 @@ export const TextInputBase = ({
348368
[value, derivedInputProps]
349369
);
350370

371+
// Calculate the adjusted maxLength to account for spaces
372+
const adjustedMaxLength = useMemo(() => {
373+
if (counterLimit && derivedInputProps && derivedInputProps.valueFormat) {
374+
const spacesCount = Math.floor(counterLimit / 4);
375+
return counterLimit + spacesCount;
376+
}
377+
return counterLimit;
378+
}, [counterLimit, derivedInputProps]);
379+
351380
return (
352381
<>
353382
<Pressable
@@ -407,7 +436,7 @@ export const TextInputBase = ({
407436
accessibilityHint={accessibilityHint}
408437
selectionColor={IOColors[theme["interactiveElem-default"]]} // Caret on iOS
409438
cursorColor={IOColors[theme["interactiveElem-default"]]} // Caret Android
410-
maxLength={counterLimit}
439+
maxLength={adjustedMaxLength}
411440
onBlur={onBlurHandler}
412441
onFocus={onFocusHandler}
413442
blurOnSubmit={true}
@@ -499,10 +528,11 @@ export const TextInputBase = ({
499528

500529
{(bottomMessage || counterLimit) && (
501530
<HelperRow
502-
value={value}
531+
value={inputValue}
503532
bottomMessage={bottomMessage}
504533
bottomMessageColor={bottomMessageColor}
505534
counterLimit={counterLimit}
535+
inputType={inputType}
506536
/>
507537
)}
508538
</>

0 commit comments

Comments
 (0)