Commit 3c4de9b0be06350f0d9ad97bfb5f7f773c38be38

Authored by Vben
1 parent e49072c3

fix(input-count): make sure the reset function works close #381

CHANGELOG.zh_CN.md
@@ -4,6 +4,10 @@ @@ -4,6 +4,10 @@
4 4
5 - 重构路由多层模式,解决嵌套 keepalive 执行多次问题 5 - 重构路由多层模式,解决嵌套 keepalive 执行多次问题
6 6
  7 +### 🐛 Bug Fixes
  8 +
  9 +- 确保 CountDownInput 组件重置清空值
  10 +
7 ## 2.1.0 (2021-03-15) 11 ## 2.1.0 (2021-03-15)
8 12
9 ### ✨ Features 13 ### ✨ Features
src/components/CountDown/src/CountButton.vue
@@ -8,22 +8,21 @@ @@ -8,22 +8,21 @@
8 </Button> 8 </Button>
9 </template> 9 </template>
10 <script lang="ts"> 10 <script lang="ts">
11 - import { defineComponent, ref, PropType } from 'vue'; 11 + import { defineComponent, ref, PropType, watchEffect } from 'vue';
12 12
13 import { Button } from 'ant-design-vue'; 13 import { Button } from 'ant-design-vue';
14 14
15 import { useCountdown } from './useCountdown'; 15 import { useCountdown } from './useCountdown';
16 import { isFunction } from '/@/utils/is'; 16 import { isFunction } from '/@/utils/is';
17 import { useI18n } from '/@/hooks/web/useI18n'; 17 import { useI18n } from '/@/hooks/web/useI18n';
  18 + import { propTypes } from '/@/utils/propTypes';
18 19
19 export default defineComponent({ 20 export default defineComponent({
20 name: 'CountButton', 21 name: 'CountButton',
21 components: { Button }, 22 components: { Button },
22 props: { 23 props: {
23 - count: {  
24 - type: Number,  
25 - default: 60,  
26 - }, 24 + value: propTypes.string,
  25 + count: propTypes.number.def(60),
27 beforeStartFunc: { 26 beforeStartFunc: {
28 type: Function as PropType<() => boolean>, 27 type: Function as PropType<() => boolean>,
29 default: null, 28 default: null,
@@ -32,8 +31,12 @@ @@ -32,8 +31,12 @@
32 setup(props) { 31 setup(props) {
33 const loading = ref(false); 32 const loading = ref(false);
34 33
35 - const { currentCount, isStart, start } = useCountdown(props.count); 34 + const { currentCount, isStart, start, reset } = useCountdown(props.count);
36 const { t } = useI18n(); 35 const { t } = useI18n();
  36 +
  37 + watchEffect(() => {
  38 + props.value === undefined && reset();
  39 + });
37 /** 40 /**
38 * @description: Judge whether there is an external function before execution, and decide whether to start after execution 41 * @description: Judge whether there is an external function before execution, and decide whether to start after execution
39 */ 42 */
src/components/CountDown/src/CountdownInput.vue
1 <template> 1 <template>
2 - <AInput v-bind="$attrs" :class="prefixCls" :size="size"> 2 + <AInput v-bind="$attrs" :class="prefixCls" :size="size" :value="state">
3 <template #addonAfter> 3 <template #addonAfter>
4 - <CountButton :size="size" :count="count" :beforeStartFunc="sendCodeApi" /> 4 + <CountButton :size="size" :count="count" :value="state" :beforeStartFunc="sendCodeApi" />
5 </template> 5 </template>
6 </AInput> 6 </AInput>
7 </template> 7 </template>
@@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
18 18
19 export default defineComponent({ 19 export default defineComponent({
20 name: 'CountDownInput', 20 name: 'CountDownInput',
  21 + inheritAttrs: false,
21 components: { [Input.name]: Input, CountButton }, 22 components: { [Input.name]: Input, CountButton },
22 props: { 23 props: {
23 value: propTypes.string, 24 value: propTypes.string,
src/components/CountDown/src/useCountdown.ts
@@ -14,8 +14,8 @@ export function useCountdown(count: number) { @@ -14,8 +14,8 @@ export function useCountdown(count: number) {
14 14
15 function stop() { 15 function stop() {
16 isStart.value = false; 16 isStart.value = false;
17 - timerId = null;  
18 clear(); 17 clear();
  18 + timerId = null;
19 } 19 }
20 20
21 function start() { 21 function start() {
src/components/Form/src/componentMap.ts
@@ -23,6 +23,7 @@ import ApiSelect from &#39;./components/ApiSelect.vue&#39;; @@ -23,6 +23,7 @@ import ApiSelect from &#39;./components/ApiSelect.vue&#39;;
23 import { BasicUpload } from '/@/components/Upload'; 23 import { BasicUpload } from '/@/components/Upload';
24 import { StrengthMeter } from '/@/components/StrengthMeter'; 24 import { StrengthMeter } from '/@/components/StrengthMeter';
25 import { IconPicker } from '/@/components/Icon'; 25 import { IconPicker } from '/@/components/Icon';
  26 +import { CountdownInput } from '/@/components/CountDown';
26 27
27 const componentMap = new Map<ComponentType, Component>(); 28 const componentMap = new Map<ComponentType, Component>();
28 29
@@ -51,6 +52,7 @@ componentMap.set(&#39;WeekPicker&#39;, DatePicker.WeekPicker); @@ -51,6 +52,7 @@ componentMap.set(&#39;WeekPicker&#39;, DatePicker.WeekPicker);
51 componentMap.set('TimePicker', TimePicker); 52 componentMap.set('TimePicker', TimePicker);
52 componentMap.set('StrengthMeter', StrengthMeter); 53 componentMap.set('StrengthMeter', StrengthMeter);
53 componentMap.set('IconPicker', IconPicker); 54 componentMap.set('IconPicker', IconPicker);
  55 +componentMap.set('InputCountDown', CountdownInput);
54 56
55 componentMap.set('Upload', BasicUpload); 57 componentMap.set('Upload', BasicUpload);
56 58
src/hooks/component/useFormItem.ts
@@ -31,6 +31,7 @@ export function useRuleFormItem&lt;T extends Indexable&gt;( @@ -31,6 +31,7 @@ export function useRuleFormItem&lt;T extends Indexable&gt;(
31 }, 31 },
32 set(value) { 32 set(value) {
33 if (isEqual(value, defaultState.value)) return; 33 if (isEqual(value, defaultState.value)) return;
  34 +
34 innerState.value = value as T[keyof T]; 35 innerState.value = value as T[keyof T];
35 emit?.(changeEvent, value); 36 emit?.(changeEvent, value);
36 }, 37 },
src/views/demo/form/RuleForm.vue
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 <a-button @click="resetValidate" class="mr-2"> 清空校验信息 </a-button> 5 <a-button @click="resetValidate" class="mr-2"> 清空校验信息 </a-button>
6 <a-button @click="getFormValues" class="mr-2"> 获取表单值 </a-button> 6 <a-button @click="getFormValues" class="mr-2"> 获取表单值 </a-button>
7 <a-button @click="setFormValues" class="mr-2"> 设置表单值 </a-button> 7 <a-button @click="setFormValues" class="mr-2"> 设置表单值 </a-button>
  8 + <a-button @click="resetFields" class="mr-2"> 重置 </a-button>
8 </div> 9 </div>
9 <CollapseContainer title="表单校验"> 10 <CollapseContainer title="表单校验">
10 <BasicForm @register="register" @submit="handleSubmit" /> 11 <BasicForm @register="register" @submit="handleSubmit" />
@@ -47,6 +48,15 @@ @@ -47,6 +48,15 @@
47 required: true, 48 required: true,
48 }, 49 },
49 { 50 {
  51 + field: 'field44',
  52 + component: 'InputCountDown',
  53 + label: '验证码',
  54 + colProps: {
  55 + span: 8,
  56 + },
  57 + required: true,
  58 + },
  59 + {
50 field: 'field4', 60 field: 'field4',
51 component: 'Select', 61 component: 'Select',
52 label: '字段4', 62 label: '字段4',
@@ -150,15 +160,16 @@ @@ -150,15 +160,16 @@
150 components: { BasicForm, CollapseContainer, PageWrapper }, 160 components: { BasicForm, CollapseContainer, PageWrapper },
151 setup() { 161 setup() {
152 const { createMessage } = useMessage(); 162 const { createMessage } = useMessage();
153 - const [register, { validateFields, clearValidate, getFieldsValue, setFieldsValue }] = useForm(  
154 - {  
155 - labelWidth: 120,  
156 - schemas,  
157 - actionColOptions: {  
158 - span: 24,  
159 - },  
160 - }  
161 - ); 163 + const [
  164 + register,
  165 + { validateFields, clearValidate, getFieldsValue, resetFields, setFieldsValue },
  166 + ] = useForm({
  167 + labelWidth: 120,
  168 + schemas,
  169 + actionColOptions: {
  170 + span: 24,
  171 + },
  172 + });
162 async function validateForm() { 173 async function validateForm() {
163 try { 174 try {
164 const res = await validateFields(); 175 const res = await validateFields();
@@ -191,6 +202,7 @@ @@ -191,6 +202,7 @@
191 setFormValues, 202 setFormValues,
192 validateForm, 203 validateForm,
193 resetValidate, 204 resetValidate,
  205 + resetFields,
194 }; 206 };
195 }, 207 },
196 }); 208 });
src/views/demo/table/FetchTable.vue
@@ -24,9 +24,6 @@ @@ -24,9 +24,6 @@
24 }); 24 });
25 function handleReloadCurrent() { 25 function handleReloadCurrent() {
26 reload(); 26 reload();
27 - // reload({  
28 - // searchInfo: 'xxx',  
29 - // });  
30 } 27 }
31 28
32 function handleReload() { 29 function handleReload() {
src/views/sys/login/ForgetPasswordForm.vue
@@ -40,7 +40,7 @@ @@ -40,7 +40,7 @@
40 import { CountdownInput } from '/@/components/CountDown'; 40 import { CountdownInput } from '/@/components/CountDown';
41 41
42 import { useI18n } from '/@/hooks/web/useI18n'; 42 import { useI18n } from '/@/hooks/web/useI18n';
43 - import { useLoginState, useFormRules, useFormValid, LoginStateEnum } from './useLogin'; 43 + import { useLoginState, useFormRules, LoginStateEnum } from './useLogin';
44 44
45 export default defineComponent({ 45 export default defineComponent({
46 name: 'ForgetPasswordForm', 46 name: 'ForgetPasswordForm',
@@ -66,14 +66,12 @@ @@ -66,14 +66,12 @@
66 sms: '', 66 sms: '',
67 }); 67 });
68 68
69 - const { validForm } = useFormValid(formRef);  
70 -  
71 const getShow = computed(() => unref(getLoginState) === LoginStateEnum.RESET_PASSWORD); 69 const getShow = computed(() => unref(getLoginState) === LoginStateEnum.RESET_PASSWORD);
72 70
73 async function handleReset() { 71 async function handleReset() {
74 - const data = await validForm();  
75 - if (!data) return;  
76 - console.log(data); 72 + const form = unref(formRef);
  73 + if (!form) return;
  74 + await form.resetFields();
77 } 75 }
78 76
79 return { 77 return {