|
@@ -0,0 +1,146 @@
|
|
|
+import {ElForm, ElSelect} from "element-plus";
|
|
|
+import type {InjectionKey} from "vue";
|
|
|
+import {ExtractPropTypes, PropType} from "vue";
|
|
|
+import sleep from "@/utils/sleep";
|
|
|
+import XEUtils from "xe-utils";
|
|
|
+
|
|
|
+export type Aaa = {
|
|
|
+ aa: string
|
|
|
+}
|
|
|
+
|
|
|
+export const Props = {
|
|
|
+ modelValue: {
|
|
|
+ type: [Object, String, Array, Number] as PropType<string | any[] | object>,
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
+ multiple: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false,
|
|
|
+ },
|
|
|
+ tableHeader: {
|
|
|
+ type: Array as PropType<{
|
|
|
+ prop: string,
|
|
|
+ label: string
|
|
|
+ width?: string | number
|
|
|
+ }[]>,
|
|
|
+ default: [],
|
|
|
+ },
|
|
|
+ rowKey: {
|
|
|
+ type: [String, Function] as PropType<string | ((row: any) => string)>,
|
|
|
+ default: 'code',
|
|
|
+ },
|
|
|
+ remoteMethod: {
|
|
|
+ type: Function as PropType<(value: string) => null | Promise<any[] | unknown>>,
|
|
|
+ default: () => [],
|
|
|
+ },
|
|
|
+ tableHeight: {
|
|
|
+ type: [String, Number],
|
|
|
+ default: 300,
|
|
|
+ },
|
|
|
+ pagination: {
|
|
|
+ type: [Object] as PropType<{
|
|
|
+ localPage: boolean,
|
|
|
+ pageSize: number,
|
|
|
+ pageSizeName: string,
|
|
|
+ currentPageName: string,
|
|
|
+ total: string,
|
|
|
+ data: string
|
|
|
+ }>,
|
|
|
+ default: {
|
|
|
+ localPage: true,
|
|
|
+ pageSize: 5,
|
|
|
+ pageSizeName: "pageSize",
|
|
|
+ currentPageName: "currentPage",
|
|
|
+ total: "total",
|
|
|
+ data: "data"
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export const useSelectTable = (props: Readonly<Required<ExtractPropTypes<typeof Props>>>) => {
|
|
|
+ const selectRef = shallowRef<InstanceType<typeof ElSelect>>()
|
|
|
+ const formRef = shallowRef<InstanceType<typeof ElForm>>()
|
|
|
+ const tableContentRef = shallowRef<HTMLDivElement>()
|
|
|
+
|
|
|
+ const store = reactive({
|
|
|
+ modelValue: "",
|
|
|
+ tableData: [] as any[],
|
|
|
+ searchValue: "",
|
|
|
+
|
|
|
+ loading: false,
|
|
|
+ })
|
|
|
+
|
|
|
+ const tmpTable = computed(() => {
|
|
|
+ if (props.pagination.localPage) {
|
|
|
+ return store.tableData.slice((pageInfo.currentPage - 1) * pageInfo.pageSize, pageInfo.currentPage * pageInfo.pageSize);
|
|
|
+ }
|
|
|
+ return store.tableData;
|
|
|
+ })
|
|
|
+
|
|
|
+ const pageInfo = reactive({
|
|
|
+ queryParams: {},
|
|
|
+ total: 0,
|
|
|
+ pageSize: props.pagination.pageSize,
|
|
|
+ currentPage: 1,
|
|
|
+ })
|
|
|
+
|
|
|
+ const mutation = {
|
|
|
+ onSearch: async () => {
|
|
|
+ if (!props.remoteMethod) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (store.loading) return
|
|
|
+ store.loading = true
|
|
|
+ if (props.pagination.localPage) {
|
|
|
+ // @ts-ignore
|
|
|
+ store.tableData = await props.remoteMethod(store.searchValue)
|
|
|
+ .catch(() => {
|
|
|
+ return [];
|
|
|
+ })
|
|
|
+ pageInfo.total = store.tableData.length
|
|
|
+ } else {
|
|
|
+ await props.remoteMethod(store.searchValue).then(res => {
|
|
|
+ store.tableData = XEUtils.get(res, props.pagination.data)
|
|
|
+ pageInfo.total = XEUtils.get(res, props.pagination.total)
|
|
|
+ }).catch(() => {
|
|
|
+ store.tableData = []
|
|
|
+ pageInfo.total = 0
|
|
|
+ pageInfo.currentPage = 1
|
|
|
+ pageInfo.pageSize = props.pagination.pageSize
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ store.loading = false;
|
|
|
+
|
|
|
+ },
|
|
|
+ visibleChange: async (val: string) => {
|
|
|
+ if (!val) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const div = tableContentRef.value;
|
|
|
+ const inputs = div.querySelectorAll('input');
|
|
|
+ if (inputs && inputs.length && inputs.length > 0) {
|
|
|
+ // onMounted(async () => {
|
|
|
+ await sleep(200)
|
|
|
+ // inputs[0].focus();
|
|
|
+ // })
|
|
|
+ inputs[0].focus();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ selectRef,
|
|
|
+ formRef,
|
|
|
+ store,
|
|
|
+ props,
|
|
|
+ mutation,
|
|
|
+ tableContentRef,
|
|
|
+ pageInfo,
|
|
|
+ tmpTable
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export const key: InjectionKey<ReturnType<typeof useSelectTable>> =
|
|
|
+ Symbol('CySelectTable')
|
|
|
+
|