Skip to content

Commit

Permalink
fix(ui): fix bug of useTwoWayBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Dec 17, 2021
1 parent 44f557d commit 6767097
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
16 changes: 6 additions & 10 deletions packages/ui/src/hooks/two-way-binding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Updater } from './immer';

import { isEqual } from 'lodash';
import { useCallback, useMemo } from 'react';
import { useCallback } from 'react';

import { DFormItemContext } from '../components/form';
import { useCustomContext } from './context';
Expand All @@ -25,16 +25,13 @@ export function useTwoWayBinding<T>(
const [autoValue, setAutoValue] = useImmer<T>(initialValue);
const value = input?.[0] ?? autoValue;

const dataRef = useMemo(() => {
return { current: formControlEnable ? (formControlValue as T) : value };
}, [formControlEnable, formControlValue, value]);
const currentValue = formControlEnable ? (formControlValue as T) : value;

const changeValue = useCallback(
(val: T) => {
if (formControlEnable) {
if (formControlName) {
if (!isEqual(val, dataRef.current)) {
dataRef.current = val;
if (!isEqual(val, currentValue)) {
onFormControlValueChange?.(val, formControlName);
}
} else {
Expand All @@ -43,14 +40,13 @@ export function useTwoWayBinding<T>(
} else {
setValue?.(val);
setAutoValue(val);
if (!isEqual(val, dataRef.current)) {
dataRef.current = val;
if (!isEqual(val, currentValue)) {
onValueChange?.(val);
}
}
},
[dataRef, formControlEnable, formControlName, onFormControlValueChange, onValueChange, setAutoValue, setValue]
[currentValue, formControlEnable, formControlName, onFormControlValueChange, onValueChange, setAutoValue, setValue]
);

return [dataRef.current, changeValue];
return [currentValue, changeValue];
}
1 change: 1 addition & 0 deletions packages/ui/src/styles/components/_dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ $dropdown-item-height: 32px;
}

@include e(title) {
flex-grow: 1;
flex-shrink: 0;

@include utils-ellipsis;
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/styles/components/_tabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,11 @@
@include utils-disabled;

@include m(dropdown) {
width: 100%;
display: flex;
height: 100%;

line-height: 22px;

@include e(close) {
margin-left: auto;
}
Expand Down

0 comments on commit 6767097

Please sign in to comment.