Skip to content

Commit

Permalink
fix: api生成以及整合hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomaolala committed Feb 7, 2025
1 parent 3445882 commit e48af9c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 52 deletions.
24 changes: 23 additions & 1 deletion src/hooks/useLengthLimit.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { computed, ComputedRef } from 'vue';
import { computed, ComputedRef, onMounted, watch } from 'vue';
import isNumber from 'lodash/isNumber';
import isObject from 'lodash/isObject';
import log from '../_common/js/log';
import { getCharacterLength, getUnicodeLength, limitUnicodeMaxLength } from '../_common/js/utils/helper';
import { TdInputProps } from '@/components';

export interface UseLengthLimitParams {
value: string;
maxlength: number;
maxcharacter: number;
allowInputOverMax: boolean;
status?: TdInputProps['status'];
onValidate?: TdInputProps['onValidate'];
}

export default function useLengthLimit(params: ComputedRef<UseLengthLimitParams>) {
Expand Down Expand Up @@ -44,6 +47,25 @@ export default function useLengthLimit(params: ComputedRef<UseLengthLimitParams>
return '';
});

const innerStatus = computed(() => {
if (limitNumber.value) {
const [current, total] = limitNumber.value.split('/');
return Number(current) > Number(total) ? 'error' : '';
}
return '';
});

const onValidateChange = () => {
params.value.onValidate?.({
error: innerStatus.value ? 'exceed-maximum' : undefined,
});
};

watch(innerStatus, onValidateChange);

onMounted(() => {
innerStatus.value && onValidateChange();
});
return {
limitNumber,
getValueByLimitNumber,
Expand Down
2 changes: 1 addition & 1 deletion src/input/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ autofocus | Boolean | false | 自动聚焦 | N
borderless | Boolean | false | 是否开启无边框模式 | N
clearTrigger | String | always | 清空图标触发方式,仅在输入框有值时有效。可选项:always / focus | N
clearable | Boolean | false | 是否可清空 | N
cursorColor | String | #0052d9 | 光标颜色,默认颜色值 #0052d9 | N
cursorColor | String | #0052d9 | 光标颜色 | N
disabled | Boolean | undefined | 是否禁用输入框 | N
enterkeyhint | String | - | 用于控制回车键样式,此 API 仅在部分浏览器支持,HTML5 原生属性,[点击查看详情](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/enterkeyhint)。可选项:enter/done/go/next/previous/search/send | N
format | Function | - | 指定输入框展示值的格式。TS 类型:`InputFormatType` `type InputFormatType = (value: InputValue) => string`[详细类型定义](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/input/type.ts) | N
Expand Down
2 changes: 1 addition & 1 deletion src/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { FormItemInjectionKey } from '../form/const';
import { useFormDisabled } from '../form/hooks';
import { usePrefixClass } from '../hooks/useClass';
import { useTNodeJSX } from '../hooks/tnode';
import useLengthLimit from './useLengthLimit';
import useLengthLimit from '@/hooks/useLengthLimit';

const { prefix } = config;

Expand Down
10 changes: 3 additions & 7 deletions src/input/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* */

import { NOOP } from '@/shared';
import { TdInputProps } from './type';
import { PropType } from 'vue';

Expand Down Expand Up @@ -40,7 +39,7 @@ export default {
},
/** 是否可清空 */
clearable: Boolean,
/** 光标颜色,默认颜色值 #0052d9 */
/** 光标颜色 */
cursorColor: {
type: String,
default: '#0052d9',
Expand Down Expand Up @@ -155,9 +154,6 @@ export default {
onClear: Function as PropType<TdInputProps['onClear']>,
/** 获得焦点时触发 */
onFocus: Function as PropType<TdInputProps['onFocus']>,
/** 字数超出限制时触发 */
onValidate: {
type: Function as PropType<TdInputProps['onValidate']>,
default: NOOP,
},
/** 【暂不支持】字数超出限制时触发 */
onValidate: Function as PropType<TdInputProps['onValidate']>,
};
2 changes: 1 addition & 1 deletion src/input/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface TdInputProps {
*/
clearable?: boolean;
/**
* 光标颜色,默认颜色值 #0052d9
* 光标颜色
* @default #0052d9
*/
cursorColor?: string;
Expand Down
41 changes: 0 additions & 41 deletions src/input/useLengthLimit.tsx

This file was deleted.

0 comments on commit e48af9c

Please sign in to comment.