|
12 | 12 | ref="refInput" |
13 | 13 | class="vf-input_clean input-text" |
14 | 14 | type="text" |
15 | | - :value="isFocused ? modelValue : pretty ? pretty(modelValue) : modelValue" |
| 15 | + :value="isFocused ? modelValue : executePretty(modelValue)" |
16 | 16 | :disabled="disabled" |
17 | 17 | :autofocus="autofocus" |
18 | 18 | :placeholder="placeholder" |
|
27 | 27 | <script setup lang="ts"> |
28 | 28 | import InputWrap from "../input-wrap.vue"; |
29 | 29 | import {ref, watch} from "vue"; |
30 | | -import warn from "../../../debug/warn"; |
| 30 | +import useModify from "../../../local-hooks/use-modify"; |
| 31 | +import {StringModify} from "../../../types"; |
| 32 | +import onlyNumber from "../../../local-hooks/only-number"; |
31 | 33 |
|
32 | | -type ModifyFunction = (a: string) => string |
33 | | -
|
34 | | -const props = withDefaults(defineProps<{ |
| 34 | +const props = defineProps<{ |
35 | 35 | label?: string, |
36 | 36 | errors: string[], |
37 | 37 | modelValue: any, |
38 | 38 | disabled: boolean, |
39 | 39 | autofocus: boolean, |
40 | | - pretty?: (a: string) => string, |
41 | | - modify?: ModifyFunction | ModifyFunction[] |
| 40 | + pretty?: StringModify | StringModify[] | Function, |
| 41 | + modify?: StringModify | StringModify[]| Function |
42 | 42 | placeholder?: string, |
43 | 43 | maxLength?: string | number, |
44 | 44 | maxlength?: string | number, |
45 | 45 | prefix?: string, |
46 | 46 | name?: string |
47 | | - numeric?: boolean |
48 | | -}>(), { |
49 | | - pretty: (a: string) => a, |
50 | | -}) |
| 47 | + numeric?: boolean, |
| 48 | +}>() |
51 | 49 |
|
52 | 50 | const isFocused = ref(false); |
53 | 51 | const refInput = ref<HTMLInputElement>(props.modelValue); |
54 | 52 | const emit = defineEmits<{ |
55 | 53 | (e: 'update:modelValue', value: any): void |
56 | 54 | }>() |
57 | 55 |
|
58 | | -function onlyNumber(a: string) { |
59 | | - return a.replace(/[^\d,.+-]/,'') |
60 | | -} |
61 | | -
|
62 | | -/** |
63 | | - * @description Function for wrapping all modify callbacks. |
64 | | - * */ |
65 | | -function executeModify(v: string): string { |
66 | | - const arrayModifyCallback: ModifyFunction[] = []; |
67 | | -
|
68 | | - arrayModifyCallback.push( |
69 | | - ...(!props.modify ? [] : Array.isArray(props.modify) ? props.modify : [props.modify]) |
70 | | - ); |
71 | | -
|
72 | | - if (props.numeric) arrayModifyCallback.unshift(onlyNumber) |
73 | | -
|
74 | | - try { |
75 | | - arrayModifyCallback.forEach(modify => { |
76 | | - v = modify(v) |
77 | | - }) |
78 | | - } catch (e) { |
79 | | - warn(`input-text${props.name ? ` (${props.name})` : ''}`, `Modify handler throw the error`, e) |
80 | | - } |
81 | | -
|
82 | | - return v; |
83 | | -} |
| 56 | +const executePretty = useModify(() => props.pretty); |
| 57 | +const executeModify = useModify( |
| 58 | + () => [ |
| 59 | + props.numeric ? onlyNumber : undefined, |
| 60 | + ...(Array.isArray(props.modify) ? props.modify : [props.modify]) |
| 61 | + ] |
| 62 | +) |
84 | 63 |
|
85 | 64 | function onInput(v: string) { |
86 | 65 | if ( |
@@ -115,7 +94,7 @@ watch(() => props.maxLength, () => onInput(props.modelValue)); |
115 | 94 | border: var(--vf-input-border-error); |
116 | 95 | } |
117 | 96 | .input-text-prefix { |
118 | | - color: #646363; |
| 97 | + color: var(--vf-input-black-light); |
119 | 98 | line-height: var(--vf-input-height); |
120 | 99 | font-size: var(--vf-input-font-size); |
121 | 100 | padding: 0 0 0 4px; |
|
0 commit comments