feat(input): component can be used outside of ion-item (#26283)

resolves #20153, resolves #19084, resolves #22736
This commit is contained in:
Liam DeBeasi
2022-11-15 11:46:44 -05:00
committed by GitHub
parent c50620af6c
commit 44472aeb9f
646 changed files with 3679 additions and 177 deletions

View File

@ -0,0 +1,34 @@
import { printIonError } from '@utils/logging';
export const getCounterText = (
value: string | number | null | undefined,
maxLength: number,
counterFormatter?: (inputLength: number, maxLength: number) => string
) => {
const valueLength = value == null ? 0 : value.toString().length;
const defaultCounterText = defaultCounterFormatter(valueLength, maxLength);
/**
* If developers did not pass a custom formatter,
* use the default one.
*/
if (counterFormatter === undefined) {
return defaultCounterText;
}
/**
* Otherwise, try to use the custom formatter
* and fallback to the default formatter if
* there was an error.
*/
try {
return counterFormatter(valueLength, maxLength);
} catch (e) {
printIonError('Exception in provided `counterFormatter`.', e);
return defaultCounterText;
}
};
const defaultCounterFormatter = (length: number, maxlength: number) => {
return `${length} / ${maxlength}`;
};