From 51394b9af6e8513486dd3e860a89ee0f48aaf384 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Wed, 5 Apr 2023 09:32:45 +0800 Subject: [PATCH] Revert "fix: useMergedState is not updated in time (#6402)" This reverts commit 670ff3f857d13b79515a7ce702e4206f00aa350b. --- components/_util/hooks/useMergedState.ts | 29 ++++++---------- .../__snapshots__/uploadlist.test.js.snap | 16 --------- components/upload/__tests__/requests.js | 4 --- .../upload/__tests__/uploadlist.test.js | 34 +------------------ 4 files changed, 12 insertions(+), 71 deletions(-) diff --git a/components/_util/hooks/useMergedState.ts b/components/_util/hooks/useMergedState.ts index a578bf814..179e7dc61 100644 --- a/components/_util/hooks/useMergedState.ts +++ b/components/_util/hooks/useMergedState.ts @@ -1,5 +1,5 @@ import type { Ref, UnwrapRef } from 'vue'; -import { computed, ref, toRaw, unref, watch } from 'vue'; +import { toRaw, watchEffect, unref, watch, ref } from 'vue'; export default function useMergedState>( defaultStateValue: T | (() => T), @@ -21,26 +21,19 @@ export default function useMergedState>( } const innerValue = ref(initValue) as Ref; - const innerMergedValue = ref(initValue) as Ref; - const exposeValue = computed({ - get: () => { - let val = innerValue.value; - if (option.postState) { - val = option.postState(val as T); - } - innerMergedValue.value = val as T; - - return innerMergedValue.value; - }, - set: (val: T) => { - triggerChange(val); - }, + const mergedValue = ref(initValue) as Ref; + watchEffect(() => { + let val = value.value !== undefined ? value.value : innerValue.value; + if (option.postState) { + val = option.postState(val as T); + } + mergedValue.value = val as T; }); function triggerChange(newValue: T) { - const preVal = innerMergedValue.value; + const preVal = mergedValue.value; innerValue.value = newValue; - if (toRaw(innerMergedValue.value) !== newValue && option.onChange) { + if (toRaw(mergedValue.value) !== newValue && option.onChange) { option.onChange(newValue, preVal); } } @@ -50,5 +43,5 @@ export default function useMergedState>( innerValue.value = value.value as T; }); - return [exposeValue as unknown as R, triggerChange]; + return [mergedValue as unknown as R, triggerChange]; } diff --git a/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap b/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap index 0f92810bd..7e7af66fc 100644 --- a/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap +++ b/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap @@ -32,22 +32,6 @@ exports[`Upload List handle error 2`] = ` `; -exports[`Upload List handle sync throw error 1`] = ` -
foo.png
-
-
-
-
-
-
-
-
-
-
-
-
-`; - exports[`Upload List should be uploading when upload a file 1`] = `
`; exports[`Upload List should non-image format file preview 1`] = ` diff --git a/components/upload/__tests__/requests.js b/components/upload/__tests__/requests.js index c5e38d107..d90ed171f 100644 --- a/components/upload/__tests__/requests.js +++ b/components/upload/__tests__/requests.js @@ -9,7 +9,3 @@ export const errorRequest = ({ onError }) => { onError(); }); }; - -export const syncErrorRequest = ({ onError }) => { - onError(); -}; diff --git a/components/upload/__tests__/uploadlist.test.js b/components/upload/__tests__/uploadlist.test.js index 1a903e805..063667877 100644 --- a/components/upload/__tests__/uploadlist.test.js +++ b/components/upload/__tests__/uploadlist.test.js @@ -1,7 +1,7 @@ import { mount } from '@vue/test-utils'; import * as Vue from 'vue'; import Upload from '..'; -import { errorRequest, successRequest, syncErrorRequest } from './requests'; +import { errorRequest, successRequest } from './requests'; import PropsTypes from '../../_util/vue-types'; import { uploadListProps } from '../interface'; import { sleep } from '../../../tests/utils'; @@ -165,38 +165,6 @@ describe('Upload List', () => { }, 0); }); - xit('handle sync throw error', done => { - const props = { - props: { - action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76', - customRequest: syncErrorRequest, - }, - listeners: { - change: ({ file }) => { - if (file.status !== 'uploading') { - expect(wrapper.html()).toMatchSnapshot(); - done(); - } - }, - }, - slots: { - default: () => h('button', 'upload'), - }, - sync: false, - }; - const wrapper = mount(Upload, props); - setTimeout(() => { - const mockFile = new File(['foo'], 'foo.png', { - type: 'image/png', - }); - wrapper.findComponent({ name: 'ajaxUploader' }).vm.onChange({ - target: { - files: [mockFile], - }, - }); - }, 0); - }); - xit('does concat filelist when beforeUpload returns false', done => { const handleChange = jest.fn(); const props = {