cl-form-tabs 添加 lazy 参数

This commit is contained in:
神仙都没用 2023-11-23 21:00:25 +08:00
parent d909c1fadb
commit eda07f5ad7
9 changed files with 98 additions and 27 deletions

View File

@ -9,7 +9,7 @@
"lint:eslint": "eslint \"{src}/**/*.{vue,ts,tsx}\" --fix" "lint:eslint": "eslint \"{src}/**/*.{vue,ts,tsx}\" --fix"
}, },
"dependencies": { "dependencies": {
"@cool-vue/crud": "^7.0.2", "@cool-vue/crud": "^7.0.3",
"@element-plus/icons-vue": "^2.1.0", "@element-plus/icons-vue": "^2.1.0",
"@vueuse/core": "^10.4.0", "@vueuse/core": "^10.4.0",
"@wangeditor/editor": "^5.1.23", "@wangeditor/editor": "^5.1.23",

View File

@ -422,6 +422,17 @@ declare namespace ClTable {
} }
} }
declare namespace ClFormTabs {
type labels = {
label: string;
value: string;
name?: string;
icon?: any;
lazy?: boolean;
[key: string]: any;
}[];
}
declare namespace ClForm { declare namespace ClForm {
type CloseAction = "close" | "save"; type CloseAction = "close" | "save";
@ -455,7 +466,7 @@ declare namespace ClForm {
type?: "tabs"; type?: "tabs";
prop?: string; prop?: string;
props?: { props?: {
labels?: Array<{ label: string; value: string; name?: string; icon?: any }>; labels?: ClFormTabs.labels;
justify?: "left" | "center" | "right"; justify?: "left" | "center" | "right";
color?: string; color?: string;
mergeProp?: boolean; mergeProp?: boolean;

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-vue/crud", "name": "@cool-vue/crud",
"version": "7.0.2", "version": "7.0.3",
"private": false, "private": false,
"main": "./dist/index.umd.min.js", "main": "./dist/index.umd.min.js",
"typings": "types/index.d.ts", "typings": "types/index.d.ts",

View File

@ -1,20 +1,71 @@
import { ref } from "vue"; import { computed, ref } from "vue";
export function useTabs({ config, Form }: { config: ClForm.Config; Form: Vue.Ref<any> }) { export function useTabs({ config, Form }: { config: ClForm.Config; Form: Vue.Ref<any> }) {
// 选中 // 选中
const active = ref<any>(); const active = ref<string | undefined>();
// 列表
const list = computed(() => {
return get()?.props?.labels || [];
});
// 获取选项
function getItem(value: any) {
return list.value.find((e) => e.value == value);
}
// 是否已加载
function isLoaded(value: any) {
const d = getItem(value);
return d?.lazy ? d.loaded : true;
}
// 加载后
function onLoad(value: any) {
const d = getItem(value);
d!.loaded = true;
}
// 查找分组
function findGroup(list: ClForm.Item[], prop: string) {
let name;
function deep(d: ClForm.Item) {
if (d.prop == prop) {
name = d.group;
} else {
if (d.children) {
d.children.forEach(deep);
}
}
}
list.forEach(deep);
return name;
}
// 获取参数 // 获取参数
function get() { function get() {
return config.items.find((e) => e.type === "tabs"); return config.items.find((e) => e.type === "tabs");
} }
// 设置参数
function set(data: any) { function set(data: any) {
active.value = data; active.value = data;
} }
// 清空
function clear() { function clear() {
active.value = null; // 清空选中
active.value = undefined;
// 清空加载状态
list.value.forEach((e) => {
if (e.lazy && e.loaded) {
e.loaded = undefined;
}
});
} }
// 切换 // 切换
@ -29,8 +80,8 @@ export function useTabs({ config, Form }: { config: ClForm.Config; Form: Vue.Ref
let isError = false; let isError = false;
const arr = config.items const arr = config.items
.filter((e: any) => e.group == active.value && !e._hidden && e.prop) .filter((e) => e.group == active.value && !e._hidden && e.prop)
.map((e: any) => { .map((e) => {
return new Promise((r: Function) => { return new Promise((r: Function) => {
// 验证表单 // 验证表单
Form.value.validateField(e.prop, (valid: string) => { Form.value.validateField(e.prop, (valid: string) => {
@ -75,10 +126,14 @@ export function useTabs({ config, Form }: { config: ClForm.Config; Form: Vue.Ref
return { return {
active, active,
list,
isLoaded,
onLoad,
get, get,
set, set,
change, change,
clear, clear,
mergeProp mergeProp,
findGroup
}; };
} }

View File

@ -181,14 +181,11 @@ export default defineComponent({
done(); done();
} }
} else { } else {
// 判断是否使用form-tabs切换到对应的选项卡 // 判断是否使用 cl-form-tabs切换到对应的选项卡
const keys = Object.keys(error);
if (Tabs.active.value) { if (Tabs.active.value) {
const el = refs.form.querySelector(`[data-prop="${keys[0]}"]`); const group = Tabs.findGroup(config.items, Object.keys(error)[0]);
if (el) { if (group) {
const group = el.getAttribute("data-group");
Tabs.set(group); Tabs.set(group);
} }
} }
@ -332,7 +329,9 @@ export default defineComponent({
const { isDisabled } = config._data; const { isDisabled } = config._data;
if (e.type == "tabs") { if (e.type == "tabs") {
return <cl-form-tabs v-model={Tabs.active.value} {...e.props} />; return (
<cl-form-tabs v-model={Tabs.active.value} {...e.props} onChange={Tabs.onLoad} />
);
} }
// 是否隐藏 // 是否隐藏
@ -341,13 +340,13 @@ export default defineComponent({
}); });
// 分组显示 // 分组显示
const inGroup = const inGroup = e.group ? e.group === Tabs.active.value : true;
isEmpty(Tabs.active.value) || isEmpty(e.group)
? true // 是否已加载完成
: e.group === Tabs.active.value; const isLoaded = e.component && Tabs.isLoaded(e.group);
// 表单项 // 表单项
const FormItem = e.component const FormItem = isLoaded
? h( ? h(
<el-form-item <el-form-item
class={{ class={{

View File

@ -27,10 +27,12 @@ export declare function useForm(): {
props?: { props?: {
[x: string]: any; [x: string]: any;
labels?: { labels?: {
[x: string]: any;
label: string; label: string;
value: string; value: string;
name?: string | undefined; name?: string | undefined;
icon?: any; icon?: any;
lazy?: boolean | undefined;
}[] | undefined; }[] | undefined;
justify?: "center" | "left" | "right" | undefined; justify?: "center" | "left" | "right" | undefined;
color?: string | undefined; color?: string | undefined;

View File

@ -3,10 +3,14 @@ export declare function useTabs({ config, Form }: {
config: ClForm.Config; config: ClForm.Config;
Form: Vue.Ref<any>; Form: Vue.Ref<any>;
}): { }): {
active: import("vue").Ref<any>; active: import("vue").Ref<string | undefined>;
list: import("vue").ComputedRef<ClFormTabs.labels>;
isLoaded: (value: any) => any;
onLoad: (value: any) => void;
get: () => ClForm.Item | undefined; get: () => ClForm.Item | undefined;
set: (data: any) => void; set: (data: any) => void;
change: (value: any, isValid?: boolean) => Promise<unknown>; change: (value: any, isValid?: boolean) => Promise<unknown>;
clear: () => void; clear: () => void;
mergeProp: (item: ClForm.Item) => void; mergeProp: (item: ClForm.Item) => void;
findGroup: (list: ClForm.Item[], prop: string) => undefined;
}; };

View File

@ -63,7 +63,7 @@ function onUpdate() {
// 提示 // 提示
if (isDev) { if (isDev) {
console.log("[eps] update", service); console.log("[eps] update");
} }
} }

View File

@ -295,10 +295,10 @@
mitt "^3.0.1" mitt "^3.0.1"
vue "^3.3.4" vue "^3.3.4"
"@cool-vue/crud@^7.0.2": "@cool-vue/crud@^7.0.3":
version "7.0.2" version "7.0.3"
resolved "https://registry.yarnpkg.com/@cool-vue/crud/-/crud-7.0.2.tgz#582b31a1eaadfe39f8189c2b587ccc1bdd2ed442" resolved "https://registry.yarnpkg.com/@cool-vue/crud/-/crud-7.0.3.tgz#a6e03ac5bcd22690fae3f61549fe9ff744291a1b"
integrity sha512-5q/xvymhUYEyV/bwBJDyYglhQcKsm8L7ngpUymgccLkEJQY+XUogPK453FGEsqsTOCVj/1Nd0Mx66Gv96SMEuw== integrity sha512-DyzIZGHL8CxY/Mr8SGfBQgLpl1SLxY6COBKR5khoXqhUQ5XqHKIbjYz5h2UwZh9GfY+nswYaW4vCbKumKiS8KQ==
dependencies: dependencies:
"@cool-vue/crud" "^7.0.1-beta14" "@cool-vue/crud" "^7.0.1-beta14"
array.prototype.flat "^1.2.4" array.prototype.flat "^1.2.4"