import {iInputCompositionData, showInputError} from "../input.utils"; import {computed, watch} from "@peryl/react-compose"; /** * 渲染最大文本长度提示suffix * @author 韦胜健 * @date 2022.5.22 10:03 */ export function useInputMaxLength({ hooks, props, model }: iInputCompositionData) { watch(() => props.maxLength, () => { showInputError('You should modify the key props of the Input to reinitialize it. Changing props [maxLength] are not supported'); }); watch(() => props.noMaxLengthSuffix, () => { showInputError('You should modify the key props of the Input to reinitialize it. Changing props [noMaxLengthSuffix] are not supported'); }); if (!props.maxLength || !!props.noMaxLengthSuffix) { return; } const currentLength = computed(() => { if (model.value == null) {return 0;} if (typeof model.value === "number") {return String(model.value).length;} return model.value.length; }); hooks.onRenderSuffix.use(prev => <> {currentLength.value}/{props.maxLength} {prev} ); }