Skip to content

Commit

Permalink
feat(loading): supports custom configuration for v-loading (#2318)
Browse files Browse the repository at this point in the history
* feat(loading): supports custom conf for v-loading

* docs(loading): improve the v-loading instructions
  • Loading branch information
akinoccc committed Apr 11, 2023
1 parent a7a9b01 commit 4a40a06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
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

0 comments on commit 4a40a06

Please sign in to comment.