Skip to content

Commit 270bca3

Browse files
authored
fix: Controlled mode should lock operation (#262)
* fix: always reflict position with controlled color * fix: throw of ssr * test: add test case
1 parent 1342587 commit 270bca3

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/hooks/useColorDrag.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type EventHandle = (e: EventType) => void;
1212

1313
interface useColorDragProps {
1414
color: Color;
15-
offset?: TransformOffset;
1615
containerRef: React.RefObject<HTMLDivElement>;
1716
targetRef: React.RefObject<HTMLDivElement>;
1817
direction?: 'x' | 'y';
@@ -42,7 +41,6 @@ function useColorDrag(
4241
props: useColorDragProps,
4342
): [TransformOffset, EventHandle] {
4443
const {
45-
offset,
4644
targetRef,
4745
containerRef,
4846
direction,
@@ -52,21 +50,16 @@ function useColorDrag(
5250
color,
5351
disabledDrag,
5452
} = props;
55-
const [offsetValue, setOffsetValue] = useState(offset || { x: 0, y: 0 });
53+
const [offsetValue, setOffsetValue] = useState({ x: 0, y: 0 });
5654
const mouseMoveRef = useRef<(event: MouseEvent) => void>(null);
5755
const mouseUpRef = useRef<(event: MouseEvent) => void>(null);
58-
const dragRef = useRef({
59-
flag: false,
60-
});
6156

57+
// Always get position from `color`
6258
useEffect(() => {
63-
if (dragRef.current.flag === false) {
64-
const calcOffset = calculate?.(containerRef);
65-
if (calcOffset) {
66-
setOffsetValue(calcOffset);
67-
}
59+
if (containerRef.current) {
60+
setOffsetValue(calculate(containerRef));
6861
}
69-
}, [color, containerRef]);
62+
}, [color]);
7063

7164
useEffect(
7265
() => () => {
@@ -111,7 +104,7 @@ function useColorDrag(
111104
return false;
112105
}
113106

114-
setOffsetValue(calcOffset);
107+
// setOffsetValue(calcOffset);
115108
onDragChange?.(calcOffset);
116109
};
117110

@@ -122,7 +115,6 @@ function useColorDrag(
122115

123116
const onDragStop: EventHandle = e => {
124117
e.preventDefault();
125-
dragRef.current.flag = false;
126118
document.removeEventListener('mousemove', mouseMoveRef.current);
127119
document.removeEventListener('mouseup', mouseUpRef.current);
128120
document.removeEventListener('touchmove', mouseMoveRef.current);
@@ -141,7 +133,6 @@ function useColorDrag(
141133
return;
142134
}
143135
updateOffset(e);
144-
dragRef.current.flag = true;
145136
document.addEventListener('mousemove', onDragMove);
146137
document.addEventListener('mouseup', onDragStop);
147138
document.addEventListener('touchmove', onDragMove);

src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const calculateOffset = (
7676
(targetWidth === 0 && targetHeight === 0) ||
7777
targetWidth !== targetHeight
7878
) {
79-
return;
79+
return { x: 0, y: 0 };
8080
}
8181

8282
if (type) {

tests/index.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ describe('ColorPicker', () => {
109109
spyDom.mockRestore();
110110
});
111111

112+
it('Should not move position when control locked', () => {
113+
const spyDom = spyElementPrototypes(HTMLElement, {
114+
getBoundingClientRect: () => ({
115+
x: 0,
116+
y: 100,
117+
width: 100,
118+
height: 100,
119+
}),
120+
});
121+
const handleChange = vi.fn();
122+
const { container } = render(
123+
<ColorPicker value="#939393" onChange={handleChange} />,
124+
);
125+
126+
const offsetHandleEle = container.querySelector('.rc-color-picker-palette')
127+
.firstChild as HTMLDivElement;
128+
const { left, top } = offsetHandleEle.style;
129+
130+
doMouseMove(container, 0, 999);
131+
expect(handleChange).toHaveBeenCalled();
132+
expect(offsetHandleEle.style.left).toBe(left);
133+
expect(offsetHandleEle.style.top).toBe(top);
134+
135+
spyDom.mockRestore();
136+
});
137+
112138
it('Should pick color work by mouse', () => {
113139
const spyDom = spyElementPrototypes(HTMLElement, {
114140
getBoundingClientRect: () => ({

0 commit comments

Comments
 (0)