Skip to content

Commit

Permalink
fix(engine): 🐛 fixed CSSUIPanel value not corrent
Browse files Browse the repository at this point in the history
affects: engine
  • Loading branch information
hlerenow committed Aug 29, 2024
1 parent 4d3934f commit 1a4dc64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import styles from '../style.module.scss';
import { forwardRef, useCallback, useImperativeHandle, useMemo, useState } from 'react';
import { InputCommonRef } from '../type';
import clsx from 'clsx';
import { pick } from 'lodash-es';

type Value = Record<'left' | 'right' | 'top' | 'bottom' | 'all', string>;
type Value = Record<string, string>;

export type MarginAndPaddingInputProps = {
value?: Value;
Expand All @@ -29,14 +30,16 @@ const getEmptyVal = () => ({
right: '',
top: '',
bottom: '',
all: '',
});

export const MarginAndPaddingInput = forwardRef<InputCommonRef, MarginAndPaddingInputProps>((props, ref) => {
const [innerVal, setInnerVal] = useState<Value>(props.initialValue ?? getEmptyVal());
const keyList = useMemo(() => {
return ['left', 'top', 'right', 'bottom'].map((el) => `${props.prefix}-${el}`);
}, [props.prefix]);

const updateInnerVal = useCallback(
(newVal: Partial<Value>, noTrigger?: boolean) => {
(newVal: Partial<any>, noTrigger?: boolean) => {
setInnerVal((oldVal) => {
const finalVal = {
...oldVal,
Expand All @@ -51,7 +54,7 @@ export const MarginAndPaddingInput = forwardRef<InputCommonRef, MarginAndPadding
return res;
}, {} as any);
if (noTrigger !== true) {
props.onChange?.(outVal);
props.onChange?.(pick(outVal, keyList));
}
return finalVal;
});
Expand Down
44 changes: 37 additions & 7 deletions packages/engine/src/component/CSSUIPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export const CSSUIPanel = forwardRef<CSSUIPanelRef, CSSUIPanelProps>(({ value, i
initialValue={initialVal as any}
value={value as any}
onChange={(val) => {
onValueChange?.({ ...value, ...val });
const newVal = {
...(tempValueRef.current || {}),
...val,
};
tempValueRef.current = newVal;
onValueChange?.(newVal);
}}
/>
),
Expand All @@ -83,7 +88,12 @@ export const CSSUIPanel = forwardRef<CSSUIPanelRef, CSSUIPanelProps>(({ value, i
initialValue={initialVal as any}
value={value as any}
onChange={(val) => {
onValueChange?.({ ...value, ...val });
const newVal = {
...(tempValueRef.current || {}),
...val,
};
tempValueRef.current = newVal;
onValueChange?.(newVal);
}}
/>
),
Expand All @@ -98,7 +108,12 @@ export const CSSUIPanel = forwardRef<CSSUIPanelRef, CSSUIPanelProps>(({ value, i
prefix="padding"
value={value as any}
onChange={(val) => {
onValueChange?.({ ...value, ...val });
const newVal = {
...(tempValueRef.current || {}),
...val,
};
tempValueRef.current = newVal;
onValueChange?.(newVal);
}}
/>
),
Expand All @@ -112,7 +127,12 @@ export const CSSUIPanel = forwardRef<CSSUIPanelRef, CSSUIPanelProps>(({ value, i
initialValue={initialVal as any}
value={value as any}
onChange={(val) => {
onValueChange?.({ ...value, ...val });
const newVal = {
...(tempValueRef.current || {}),
...val,
};
tempValueRef.current = newVal;
onValueChange?.(newVal);
}}
/>
),
Expand All @@ -126,7 +146,12 @@ export const CSSUIPanel = forwardRef<CSSUIPanelRef, CSSUIPanelProps>(({ value, i
value={value as any}
initialValue={initialVal as any}
onChange={(val) => {
onValueChange?.({ ...value, ...val });
const newVal = {
...(tempValueRef.current || {}),
...val,
};
tempValueRef.current = newVal;
onValueChange?.(newVal);
}}
/>
),
Expand All @@ -139,7 +164,12 @@ export const CSSUIPanel = forwardRef<CSSUIPanelRef, CSSUIPanelProps>(({ value, i
ref={borderRef}
initialValue={initialVal as any}
onChange={(val) => {
onValueChange?.({ ...value, ...val });
const newVal = {
...(tempValueRef.current || {}),
...val,
};
tempValueRef.current = newVal;
onValueChange?.(newVal);
}}
/>
),
Expand All @@ -153,7 +183,7 @@ export const CSSUIPanel = forwardRef<CSSUIPanelRef, CSSUIPanelProps>(({ value, i
initialValue={initialVal as any}
value={value as any}
onChange={(val) => {
onValueChange?.({ ...value, ...val });
onValueChange?.({ ...val });
}}
/>
),
Expand Down

0 comments on commit 1a4dc64

Please sign in to comment.