Browse Source

refactor: 优化代码 (#3695)

优化变量使用及定义的顺序
优化ts及tsx文件类型
使用===替换==
替换不必要的map为forEach
优化部分注释
pull/3701/head
Werheng Zhang 3 years ago
committed by GitHub
parent
commit
05030ee984
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      src/components/Form/src/BasicForm.vue
  2. 2
      src/components/Form/src/hooks/useFormEvents.ts
  3. 2
      src/components/Form/src/hooks/useFormValues.ts
  4. 52
      src/components/Scrollbar/src/bar.ts
  5. 2
      src/components/Table/src/components/TableImg.vue
  6. 40
      src/components/Table/src/components/editable/EditableCell.vue
  7. 12
      src/components/Table/src/components/settings/ColumnSetting.vue
  8. 2
      src/components/Table/src/hooks/useDataSource.ts
  9. 6
      src/components/Table/src/hooks/usePagination.ts
  10. 2
      src/components/Table/src/hooks/useTableExpand.ts
  11. 1
      src/hooks/web/usePermission.ts
  12. 2
      src/utils/helper/tsxHelper.ts
  13. 2
      src/utils/index.ts
  14. 4
      src/views/form-design/components/VFormDesign/components/ComponentProps.vue
  15. 2
      src/views/form-design/components/VFormDesign/components/FormProps.vue
  16. 2
      src/views/form-design/components/VFormDesign/config/componentPropsConfig.ts

2
src/components/Form/src/BasicForm.vue

@ -285,7 +285,7 @@
if (!autoSubmitOnEnter) return; if (!autoSubmitOnEnter) return;
if (e.key === 'Enter' && e.target && e.target instanceof HTMLElement) { if (e.key === 'Enter' && e.target && e.target instanceof HTMLElement) {
const target: HTMLElement = e.target as HTMLElement; const target: HTMLElement = e.target as HTMLElement;
if (target && target.tagName && target.tagName.toUpperCase() == 'INPUT') { if (target && target.tagName && target.tagName.toUpperCase() === 'INPUT') {
handleSubmit(); handleSubmit();
} }
} }

2
src/components/Form/src/hooks/useFormEvents.ts

@ -87,7 +87,7 @@ export function useFormEvents({
const defaultValueObj = schema?.defaultValueObj; const defaultValueObj = schema?.defaultValueObj;
const fieldKeys = Object.keys(defaultValueObj || {}); const fieldKeys = Object.keys(defaultValueObj || {});
if (fieldKeys.length) { if (fieldKeys.length) {
fieldKeys.map((field) => { fieldKeys.forEach((field) => {
formModel[field] = defaultValueObj![field]; formModel[field] = defaultValueObj![field];
}); });
} }

2
src/components/Form/src/hooks/useFormValues.ts

@ -138,7 +138,7 @@ export function useFormValues({
const { defaultValue, defaultValueObj } = item; const { defaultValue, defaultValueObj } = item;
const fieldKeys = Object.keys(defaultValueObj || {}); const fieldKeys = Object.keys(defaultValueObj || {});
if (fieldKeys.length) { if (fieldKeys.length) {
fieldKeys.map((field) => { fieldKeys.forEach((field) => {
obj[field] = defaultValueObj![field]; obj[field] = defaultValueObj![field];
if (formModel[field] === undefined) { if (formModel[field] === undefined) {
formModel[field] = defaultValueObj![field]; formModel[field] = defaultValueObj![field];

52
src/components/Scrollbar/src/bar.ts

@ -28,6 +28,35 @@ export default defineComponent({
}); });
const barStore = ref<Recordable>({}); const barStore = ref<Recordable>({});
const cursorDown = ref(); const cursorDown = ref();
const mouseMoveDocumentHandler = (e: any) => {
if (cursorDown.value === false) {
return;
}
const prevPage = barStore.value[bar.value.axis];
if (!prevPage) {
return;
}
const offset =
(instance?.vnode.el?.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) *
-1;
const thumbClickPosition = thumb.value[bar.value.offset] - prevPage;
const thumbPositionPercentage =
((offset - thumbClickPosition) * 100) / instance?.vnode.el?.[bar.value.offset];
wrap.value[bar.value.scroll] =
(thumbPositionPercentage * wrap.value[bar.value.scrollSize]) / 100;
};
const startDrag = (e: any) => {
e.stopImmediatePropagation();
cursorDown.value = true;
on(document, 'mousemove', mouseMoveDocumentHandler);
on(document, 'mouseup', mouseUpDocumentHandler);
document.onselectstart = () => false;
};
const clickThumbHandler = (e: any) => { const clickThumbHandler = (e: any) => {
// prevent click event of right button // prevent click event of right button
if (e.ctrlKey || e.button === 2) { if (e.ctrlKey || e.button === 2) {
@ -51,29 +80,6 @@ export default defineComponent({
wrap.value[bar.value.scroll] = wrap.value[bar.value.scroll] =
(thumbPositionPercentage * wrap.value[bar.value.scrollSize]) / 100; (thumbPositionPercentage * wrap.value[bar.value.scrollSize]) / 100;
}; };
const startDrag = (e: any) => {
e.stopImmediatePropagation();
cursorDown.value = true;
on(document, 'mousemove', mouseMoveDocumentHandler);
on(document, 'mouseup', mouseUpDocumentHandler);
document.onselectstart = () => false;
};
const mouseMoveDocumentHandler = (e: any) => {
if (cursorDown.value === false) return;
const prevPage = barStore.value[bar.value.axis];
if (!prevPage) return;
const offset =
(instance?.vnode.el?.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) *
-1;
const thumbClickPosition = thumb.value[bar.value.offset] - prevPage;
const thumbPositionPercentage =
((offset - thumbClickPosition) * 100) / instance?.vnode.el?.[bar.value.offset];
wrap.value[bar.value.scroll] =
(thumbPositionPercentage * wrap.value[bar.value.scrollSize]) / 100;
};
function mouseUpDocumentHandler() { function mouseUpDocumentHandler() {
cursorDown.value = false; cursorDown.value = false;

2
src/components/Table/src/components/TableImg.vue

@ -5,7 +5,7 @@
v-if="imgList && imgList.length" v-if="imgList && imgList.length"
:style="getWrapStyle" :style="getWrapStyle"
> >
<Badge :count="!showBadge || imgList.length == 1 ? 0 : imgList.length" v-if="simpleShow"> <Badge :count="!showBadge || imgList.length === 1 ? 0 : imgList.length" v-if="simpleShow">
<div class="img-div"> <div class="img-div">
<Image.PreviewGroup> <Image.PreviewGroup>
<template v-for="(img, index) in imgList" :key="img"> <template v-for="(img, index) in imgList" :key="img">

40
src/components/Table/src/components/editable/EditableCell.vue

@ -65,6 +65,19 @@
return ['Checkbox', 'Switch'].includes(component); return ['Checkbox', 'Switch'].includes(component);
}); });
const getDisable = computed(() => {
const { editDynamicDisabled } = props.column;
let disabled = false;
if (isBoolean(editDynamicDisabled)) {
disabled = editDynamicDisabled;
}
if (isFunction(editDynamicDisabled)) {
const { record } = props;
disabled = editDynamicDisabled({ record, currentValue: currentValueRef.value });
}
return disabled;
});
const getComponentProps = computed(() => { const getComponentProps = computed(() => {
const isCheckValue = unref(getIsCheckComp); const isCheckValue = unref(getIsCheckComp);
let compProps = props.column?.editComponentProps ?? ({} as any); let compProps = props.column?.editComponentProps ?? ({} as any);
@ -117,18 +130,7 @@
const dataKey = (dataIndex || key) as string; const dataKey = (dataIndex || key) as string;
set(record, dataKey, value); set(record, dataKey, value);
} }
const getDisable = computed(() => {
const { editDynamicDisabled } = props.column;
let disabled = false;
if (isBoolean(editDynamicDisabled)) {
disabled = editDynamicDisabled;
}
if (isFunction(editDynamicDisabled)) {
const { record } = props;
disabled = editDynamicDisabled({ record, currentValue: currentValueRef.value });
}
return disabled;
});
const getValues = computed(() => { const getValues = computed(() => {
const { editValueMap } = props.column; const { editValueMap } = props.column;
@ -149,6 +151,11 @@
return option?.label ?? value; return option?.label ?? value;
}); });
const getRowEditable = computed(() => {
const { editable } = props.record || {};
return !!editable;
});
const getWrapperStyle = computed((): CSSProperties => { const getWrapperStyle = computed((): CSSProperties => {
if (unref(getIsCheckComp) || unref(getRowEditable)) { if (unref(getIsCheckComp) || unref(getRowEditable)) {
return {}; return {};
@ -163,11 +170,6 @@
return `edit-cell-align-${align}`; return `edit-cell-align-${align}`;
}); });
const getRowEditable = computed(() => {
const { editable } = props.record || {};
return !!editable;
});
watchEffect(() => { watchEffect(() => {
// defaultValueRef.value = props.value; // defaultValueRef.value = props.value;
currentValueRef.value = props.value; currentValueRef.value = props.value;
@ -191,7 +193,7 @@
}); });
} }
async function handleChange(e: any) { async function handleChange(e: any, ...rest: any[]) {
const component = unref(getComponent); const component = unref(getComponent);
if (!e) { if (!e) {
currentValueRef.value = e; currentValueRef.value = e;
@ -205,7 +207,7 @@
currentValueRef.value = e; currentValueRef.value = e;
} }
const onChange = unref(getComponentProps)?.onChangeTemp; const onChange = unref(getComponentProps)?.onChangeTemp;
if (onChange && isFunction(onChange)) onChange(...arguments); if (onChange && isFunction(onChange)) onChange(e, ...rest);
table.emit?.('edit-change', { table.emit?.('edit-change', {
column: props.column, column: props.column,

12
src/components/Table/src/components/settings/ColumnSetting.vue

@ -149,6 +149,12 @@
return isFunction(attrs.getPopupContainer) ? attrs.getPopupContainer() : getParentContainer(); return isFunction(attrs.getPopupContainer) ? attrs.getPopupContainer() : getParentContainer();
}; };
// 默认值
let defaultIsIndexColumnShow: boolean = false;
let defaultIsRowSelectionShow: boolean = false;
let defaultRowSelection: TableRowSelection<Recordable<any>>;
let defaultColumnOptions: ColumnOptionsType[] = [];
// 是否已经从缓存恢复 // 是否已经从缓存恢复
let isRestored = false; let isRestored = false;
let isInnerChange = false; let isInnerChange = false;
@ -518,12 +524,6 @@
tableColumnsUpdate(); tableColumnsUpdate();
}; };
// 默认值
let defaultIsIndexColumnShow: boolean = false;
let defaultIsRowSelectionShow: boolean = false;
let defaultRowSelection: TableRowSelection<Recordable<any>>;
let defaultColumnOptions: ColumnOptionsType[] = [];
const init = async () => { const init = async () => {
if (!isRestored) { if (!isRestored) {
// 获取数据列 // 获取数据列

2
src/components/Table/src/hooks/useDataSource.ts

@ -211,7 +211,7 @@ export function useDataSource(
} }
function findTableDataRecord(keyValue: Key) { function findTableDataRecord(keyValue: Key) {
if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; if (!dataSourceRef.value || dataSourceRef.value.length === 0) return;
const { childrenColumnName = 'children' } = unref(propsRef); const { childrenColumnName = 'children' } = unref(propsRef);
const findRow = (array: any[]) => { const findRow = (array: any[]) => {

6
src/components/Table/src/hooks/usePagination.tsx → src/components/Table/src/hooks/usePagination.ts

@ -1,6 +1,6 @@
import type { PaginationProps } from '../types/pagination'; import type { PaginationProps } from '../types/pagination';
import type { BasicTableProps } from '../types/table'; import type { BasicTableProps } from '../types/table';
import { computed, unref, ref, ComputedRef, watch } from 'vue'; import { computed, unref, ref, ComputedRef, watch, h } from 'vue';
import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue'; import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
import { isBoolean } from '@/utils/is'; import { isBoolean } from '@/utils/is';
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '../const'; import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '../const';
@ -14,9 +14,9 @@ interface ItemRender {
function itemRender({ page, type, originalElement }: ItemRender) { function itemRender({ page, type, originalElement }: ItemRender) {
if (type === 'prev') { if (type === 'prev') {
return page === 0 ? null : <LeftOutlined />; return page === 0 ? null : h(LeftOutlined);
} else if (type === 'next') { } else if (type === 'next') {
return page === 1 ? null : <RightOutlined />; return page === 1 ? null : h(RightOutlined);
} }
return originalElement; return originalElement;
} }

2
src/components/Table/src/hooks/useTableExpand.ts

@ -105,7 +105,7 @@ export function useTableExpand(
} }
// 监听展开事件,用于支持手风琴展开效果 // 监听展开事件,用于支持手风琴展开效果
function handleTableExpand(expanded, record) { function handleTableExpand(expanded: boolean, record: Recordable) {
// 手风琴开关 // 手风琴开关
// isTreeTable 或 expandRowByClick 时支持 // isTreeTable 或 expandRowByClick 时支持
// 展开操作 // 展开操作

1
src/hooks/web/usePermission.ts

@ -40,7 +40,6 @@ export function usePermission() {
/** /**
* Reset and regain authority resource information * Reset and regain authority resource information
* 重置和重新获得权限资源信息 * 重置和重新获得权限资源信息
* @param id
*/ */
async function resume() { async function resume() {
const tabStore = useMultipleTabStore(); const tabStore = useMultipleTabStore();

2
src/utils/helper/tsxHelper.tsx → src/utils/helper/tsxHelper.ts

@ -27,7 +27,7 @@ export function getSlot(slots: Slots, slot = 'default', data?: any, opts?: Rende
export function extendSlots(slots: Slots, excludeKeys: string[] = []) { export function extendSlots(slots: Slots, excludeKeys: string[] = []) {
const slotKeys = Object.keys(slots); const slotKeys = Object.keys(slots);
const ret: any = {}; const ret: any = {};
slotKeys.map((key) => { slotKeys.forEach((key) => {
if (excludeKeys.includes(key)) { if (excludeKeys.includes(key)) {
return null; return null;
} }

2
src/utils/index.ts

@ -97,7 +97,7 @@ export function openWindow(
export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> { export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
const ret: Recordable = {}; const ret: Recordable = {};
Object.keys(props).map((key) => { Object.keys(props).forEach((key) => {
ret[key] = unref((props as Recordable)[key]); ret[key] = unref((props as Recordable)[key]);
}); });

4
src/views/form-design/components/VFormDesign/components/ComponentProps.vue

@ -199,14 +199,14 @@
// 控制性的选项 // 控制性的选项
const controlOptions = computed(() => { const controlOptions = computed(() => {
return allOptions.value.filter((item) => { return allOptions.value.filter((item) => {
return item.category == 'control'; return item.category === 'control';
}); });
}); });
// 非控制性选择 // 非控制性选择
const inputOptions = computed(() => { const inputOptions = computed(() => {
return allOptions.value.filter((item) => { return allOptions.value.filter((item) => {
return item.category == 'input'; return item.category === 'input';
}); });
}); });

2
src/views/form-design/components/VFormDesign/components/FormProps.vue

@ -65,7 +65,7 @@
</div> </div>
<FormItem label="表单属性"> <FormItem label="表单属性">
<Col <Col
><Checkbox v-model:checked="formConfig.colon" v-if="formConfig.layout == 'horizontal'" ><Checkbox v-model:checked="formConfig.colon" v-if="formConfig.layout === 'horizontal'"
>label后面显示冒号</Checkbox >label后面显示冒号</Checkbox
></Col ></Col
> >

2
src/views/form-design/components/VFormDesign/config/componentPropsConfig.ts

@ -1099,7 +1099,7 @@ const componentAttrs: IBaseComponentProps = {
function deleteProps(list: Omit<IBaseFormAttrs, 'tag'>[], key: string) { function deleteProps(list: Omit<IBaseFormAttrs, 'tag'>[], key: string) {
list.forEach((element, index) => { list.forEach((element, index) => {
if (element.name == key) { if (element.name === key) {
list.splice(index, 1); list.splice(index, 1);
} }
}); });

Loading…
Cancel
Save