Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loading): supports custom configuration for v-loading #2318

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/loading/_example/directive.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

<div v-loading.fullscreen="loading2" class="loading-container">Loading...</div>
<t-button size="small" :disabled="loading2" @click="handleLoadingFullscreen">支持 fullscreen 修饰符</t-button>

<div v-loading="loading3" class="loading-container">Loading...</div>
<t-button size="small" :disabled="loading2" @click="handleLoadingCustom">支持传递 Object 配置</t-button>
</t-space>
</template>
<script>
Expand All @@ -15,6 +18,11 @@ export default {
return {
loading1: false,
loading2: false,
loading3: {
loading: false,
delay: 300,
showOverlay: true,
},
};
},
methods: {
Expand All @@ -32,6 +40,13 @@ export default {
clearTimeout(timer);
}, 1000);
},
handleLoadingCustom() {
this.loading3 = { ...this.loading3, loading: true };
const timer = setTimeout(() => {
this.loading3 = { ...this.loading3, loading: false };
clearTimeout(timer);
}, 1000);
},
},
};
</script>
Expand Down
14 changes: 12 additions & 2 deletions src/loading/directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { DirectiveBinding } from 'vue/types/options';
import isObject from 'lodash/isObject';
import mapKeys from 'lodash/mapKeys';
import isEqual from 'lodash/isEqual';
import { TdLoadingProps } from './type';
import produceLoading from './plugin';

Expand All @@ -13,6 +16,12 @@ const createInstance = (el: HTMLElement, binding: DirectiveBinding) => {
loading: binding.value,
};

if (isObject(binding.value)) {
mapKeys(binding.value, (value, key) => {
options[key] = value;
});
}

// eslint-disable-next-line no-param-reassign
el[INSTANCE_KEY] = {
options,
Expand All @@ -29,8 +38,9 @@ const vLoading = {
update(el: HTMLElement, binding: DirectiveBinding) {
const instance = el[INSTANCE_KEY];
const { value, oldValue } = binding;
if (!!oldValue !== !!value) {
if (value) {
if (!isEqual(value, oldValue)) {
const loading = value?.loading ?? value;
if (loading) {
createInstance(el, binding);
} else {
instance?.instance.hide();
Expand Down
2 changes: 1 addition & 1 deletion src/loading/loading.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
:: BASE_DOC ::

### 指令方式调用
支持 `v-loading` 指令调用 `Loading`,只需要绑定 `boolean` 值即可,支持 `fullscreen` 和 `inheritColor` 修饰符,分别对应其属性。
支持 `v-loading` 指令调用 `Loading`,只需要绑定 `boolean` 值即可,支持 `fullscreen` 和 `inheritColor` 修饰符以及 `Object` 形式的自定义配置,分别对应其属性。

{{ directive }}

Expand Down