Skip to content

Commit

Permalink
perf(components/resizer): 简化 resizer 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed Nov 27, 2023
1 parent d2381a1 commit 3725c75
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions packages/components/src/resizer/Resizer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getComponentClass, getElRealSize, useForwardRef } from '@pkg/shared';
import { getClassNames, getSafeNum } from '@tool-pack/basic';
import type { RequiredPart } from '@tool-pack/types';
import type { ResizerProps } from './resizer.types';
import { getClassNames } from '@tool-pack/basic';
import { onDragEvent } from '@tool-pack/dom';
import React, { useEffect } from 'react';

Expand Down Expand Up @@ -35,29 +35,25 @@ export const Resizer: React.FC<ResizerProps> = React.forwardRef<
let h = 0;
let w = 0;

onDown(() => {
[w, h] = getElRealSize(parent);
});
onDown(() => ([w, h] = getElRealSize(parent)));
const safeValue = (value: number) => getSafeNum(value, min, max) + 'px';

const handlerMap: Record<
typeof placement,
Parameters<typeof onMove>[0]
> = {
bottom(_e, curr, _lastXY, down) {
const height = Math.min(max, Math.max(min, h + (curr.y - down.y)));
parent.style.height = height + 'px';
parent.style.height = safeValue(h + (curr.y - down.y));
},
top(_e, curr, _lastXY, down) {
const height = Math.min(max, Math.max(min, h - (curr.y - down.y)));
parent.style.height = height + 'px';
parent.style.height = safeValue(h - (curr.y - down.y));
},
// eslint-disable-next-line perfectionist/sort-objects
right(_e, curr, _lastXY, down) {
const width = Math.min(max, Math.max(min, w + (curr.x - down.x)));
parent.style.width = width + 'px';
parent.style.width = safeValue(w + (curr.x - down.x));
},
left(_e, curr, _lastXY, down) {
const width = Math.min(max, Math.max(min, w - (curr.x - down.x)));
parent.style.width = width + 'px';
parent.style.width = safeValue(w - (curr.x - down.x));
},
};
onMove(handlerMap[placement]);
Expand Down

0 comments on commit 3725c75

Please sign in to comment.