Skip to content

Commit

Permalink
refactor: 优化代码 (#3695)
Browse files Browse the repository at this point in the history
优化变量使用及定义的顺序
优化ts及tsx文件类型
使用===替换==
替换不必要的map为forEach
优化部分注释
  • Loading branch information
werheng committed Mar 27, 2024
1 parent 09f795e commit 05030ee
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/components/Form/src/BasicForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
if (!autoSubmitOnEnter) return;
if (e.key === 'Enter' && e.target && e.target instanceof 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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Form/src/hooks/useFormEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function useFormEvents({
const defaultValueObj = schema?.defaultValueObj;
const fieldKeys = Object.keys(defaultValueObj || {});
if (fieldKeys.length) {
fieldKeys.map((field) => {
fieldKeys.forEach((field) => {
formModel[field] = defaultValueObj![field];
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Form/src/hooks/useFormValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function useFormValues({
const { defaultValue, defaultValueObj } = item;
const fieldKeys = Object.keys(defaultValueObj || {});
if (fieldKeys.length) {
fieldKeys.map((field) => {
fieldKeys.forEach((field) => {
obj[field] = defaultValueObj![field];
if (formModel[field] === undefined) {
formModel[field] = defaultValueObj![field];
Expand Down
52 changes: 29 additions & 23 deletions src/components/Scrollbar/src/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ export default defineComponent({
});
const barStore = ref<Recordable>({});
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) => {
// prevent click event of right button
if (e.ctrlKey || e.button === 2) {
Expand All @@ -51,29 +80,6 @@ export default defineComponent({
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 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() {
cursorDown.value = false;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/src/components/TableImg.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
v-if="imgList && imgList.length"
: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">
<Image.PreviewGroup>
<template v-for="(img, index) in imgList" :key="img">
Expand Down
40 changes: 21 additions & 19 deletions src/components/Table/src/components/editable/EditableCell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@
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 isCheckValue = unref(getIsCheckComp);
let compProps = props.column?.editComponentProps ?? ({} as any);
Expand Down Expand Up @@ -117,18 +130,7 @@
const dataKey = (dataIndex || key) as string;
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 { editValueMap } = props.column;
Expand All @@ -149,6 +151,11 @@
return option?.label ?? value;
});
const getRowEditable = computed(() => {
const { editable } = props.record || {};
return !!editable;
});
const getWrapperStyle = computed((): CSSProperties => {
if (unref(getIsCheckComp) || unref(getRowEditable)) {
return {};
Expand All @@ -163,11 +170,6 @@
return `edit-cell-align-${align}`;
});
const getRowEditable = computed(() => {
const { editable } = props.record || {};
return !!editable;
});
watchEffect(() => {
// defaultValueRef.value = props.value;
currentValueRef.value = props.value;
Expand All @@ -191,7 +193,7 @@
});
}
async function handleChange(e: any) {
async function handleChange(e: any, ...rest: any[]) {
const component = unref(getComponent);
if (!e) {
currentValueRef.value = e;
Expand All @@ -205,7 +207,7 @@
currentValueRef.value = e;
}
const onChange = unref(getComponentProps)?.onChangeTemp;
if (onChange && isFunction(onChange)) onChange(...arguments);
if (onChange && isFunction(onChange)) onChange(e, ...rest);
table.emit?.('edit-change', {
column: props.column,
Expand Down
12 changes: 6 additions & 6 deletions src/components/Table/src/components/settings/ColumnSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
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 isInnerChange = false;
Expand Down Expand Up @@ -518,12 +524,6 @@
tableColumnsUpdate();
};
// 默认值
let defaultIsIndexColumnShow: boolean = false;
let defaultIsRowSelectionShow: boolean = false;
let defaultRowSelection: TableRowSelection<Recordable<any>>;
let defaultColumnOptions: ColumnOptionsType[] = [];
const init = async () => {
if (!isRestored) {
// 获取数据列
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/src/hooks/useDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export function useDataSource(
}

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 findRow = (array: any[]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PaginationProps } from '../types/pagination';
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 { isBoolean } from '@/utils/is';
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '../const';
Expand All @@ -14,9 +14,9 @@ interface ItemRender {

function itemRender({ page, type, originalElement }: ItemRender) {
if (type === 'prev') {
return page === 0 ? null : <LeftOutlined />;
return page === 0 ? null : h(LeftOutlined);
} else if (type === 'next') {
return page === 1 ? null : <RightOutlined />;
return page === 1 ? null : h(RightOutlined);
}
return originalElement;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/src/hooks/useTableExpand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function useTableExpand(
}

// 监听展开事件,用于支持手风琴展开效果
function handleTableExpand(expanded, record) {
function handleTableExpand(expanded: boolean, record: Recordable) {
// 手风琴开关
// isTreeTable 或 expandRowByClick 时支持
// 展开操作
Expand Down
1 change: 0 additions & 1 deletion src/hooks/web/usePermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export function usePermission() {
/**
* Reset and regain authority resource information
* 重置和重新获得权限资源信息
* @param id
*/
async function resume() {
const tabStore = useMultipleTabStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function getSlot(slots: Slots, slot = 'default', data?: any, opts?: Rende
export function extendSlots(slots: Slots, excludeKeys: string[] = []) {
const slotKeys = Object.keys(slots);
const ret: any = {};
slotKeys.map((key) => {
slotKeys.forEach((key) => {
if (excludeKeys.includes(key)) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function openWindow(
export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
const ret: Recordable = {};

Object.keys(props).map((key) => {
Object.keys(props).forEach((key) => {
ret[key] = unref((props as Recordable)[key]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@
// 控制性的选项
const controlOptions = computed(() => {
return allOptions.value.filter((item) => {
return item.category == 'control';
return item.category === 'control';
});
});
// 非控制性选择
const inputOptions = computed(() => {
return allOptions.value.filter((item) => {
return item.category == 'input';
return item.category === 'input';
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</div>
<FormItem label="表单属性">
<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
></Col
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ const componentAttrs: IBaseComponentProps = {

function deleteProps(list: Omit<IBaseFormAttrs, 'tag'>[], key: string) {
list.forEach((element, index) => {
if (element.name == key) {
if (element.name === key) {
list.splice(index, 1);
}
});
Expand Down

0 comments on commit 05030ee

Please sign in to comment.