Skip to content

Commit cf92c9b

Browse files
zombieJlinxianxi
authored andcommitted
enhance: color picker click (ant-design#50148)
1 parent 9c32e46 commit cf92c9b

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

components/color-picker/__tests__/index.test.tsx

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ function doMouseMove(
1919
container: HTMLElement,
2020
start: number,
2121
end: number,
22-
element = 'ant-color-picker-handler',
22+
element: string | HTMLElement = 'ant-color-picker-handler',
2323
) {
24-
const mouseDown = createEvent.mouseDown(container.getElementsByClassName(element)[0], {
24+
const ele =
25+
element instanceof HTMLElement ? element : container.getElementsByClassName(element)[0];
26+
27+
const mouseDown = createEvent.mouseDown(ele, {
2528
pageX: start,
2629
pageY: start,
2730
});
@@ -30,7 +33,7 @@ function doMouseMove(
3033
pageY: { get: () => start },
3134
});
3235

33-
fireEvent(container.getElementsByClassName(element)[0], mouseDown);
36+
fireEvent(ele, mouseDown);
3437
// Drag
3538
const mouseMove: any = new Event('mousemove');
3639
mouseMove.pageX = end;
@@ -797,4 +800,52 @@ describe('ColorPicker', () => {
797800
);
798801
expect(container.querySelector('.ant-color-picker-trigger-text')?.innerHTML).toBe('123456');
799802
});
803+
804+
describe('transparent to valuable', () => {
805+
let spyRect: ReturnType<typeof spyElementPrototypes>;
806+
807+
beforeEach(() => {
808+
spyRect = spyElementPrototypes(HTMLElement, {
809+
getBoundingClientRect: () => ({
810+
x: 0,
811+
y: 100,
812+
width: 100,
813+
height: 100,
814+
}),
815+
});
816+
});
817+
818+
afterEach(() => {
819+
spyRect.mockRestore();
820+
});
821+
822+
it('init with hue', async () => {
823+
const onChange = jest.fn();
824+
const { container } = render(<ColorPicker defaultValue={null} open onChange={onChange} />);
825+
doMouseMove(container, 0, 50, 'ant-color-picker-slider-handle');
826+
827+
expect(onChange).toHaveBeenCalledWith(
828+
expect.anything(),
829+
// Safe to change with any value but (0/0/0/0)
830+
'rgb(0,255,255)',
831+
);
832+
});
833+
834+
it('init with alpha', async () => {
835+
const onChange = jest.fn();
836+
const { container } = render(<ColorPicker defaultValue={null} open onChange={onChange} />);
837+
doMouseMove(
838+
container,
839+
0,
840+
50,
841+
container.querySelectorAll<HTMLElement>('.ant-color-picker-slider-handle')[1]!,
842+
);
843+
844+
expect(onChange).toHaveBeenCalledWith(
845+
expect.anything(),
846+
// Safe to change with any value but (0/0/0/0)
847+
'rgba(255,0,0,0.5)',
848+
);
849+
});
850+
});
800851
});

components/color-picker/components/PanelPicker/index.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,37 @@ const PanelPicker: FC = () => {
9595
return new AggregationColor(nextColors);
9696
};
9797

98-
const onInternalChange = (colorValue: AggregationColor | Color, fromPicker?: boolean) => {
98+
const onInternalChange = (
99+
colorValue: AggregationColor | Color,
100+
fromPicker?: boolean,
101+
info?: {
102+
type?: 'hue' | 'alpha';
103+
value?: number;
104+
},
105+
) => {
99106
const nextColor = generateColor(colorValue);
100107

101-
onChange(fillColor(value.cleared ? genAlphaColor(nextColor) : nextColor), fromPicker);
108+
let submitColor = nextColor;
109+
110+
if (value.cleared) {
111+
const rgb = submitColor.toRgb();
112+
113+
// Auto fill color if origin is `0/0/0` to enhance user experience
114+
if (!rgb.r && !rgb.g && !rgb.b && info) {
115+
const { type: infoType, value: infoValue = 0 } = info;
116+
117+
submitColor = new AggregationColor({
118+
h: infoType === 'hue' ? infoValue : 0,
119+
s: 1,
120+
b: 1,
121+
a: infoType === 'alpha' ? infoValue / 100 : 1,
122+
});
123+
} else {
124+
submitColor = genAlphaColor(submitColor);
125+
}
126+
}
127+
128+
onChange(fillColor(submitColor), fromPicker);
102129
};
103130

104131
const onInternalChangeComplete = (nextColor: AggregationColor) => {
@@ -140,8 +167,8 @@ const PanelPicker: FC = () => {
140167
prefixCls={prefixCls}
141168
value={activeColor?.toHsb()}
142169
disabledAlpha={disabledAlpha}
143-
onChange={(colorValue) => {
144-
onInternalChange(colorValue, true);
170+
onChange={(colorValue, info) => {
171+
onInternalChange(colorValue, true, info);
145172
}}
146173
onChangeComplete={(colorValue) => {
147174
onInternalChangeComplete(generateColor(colorValue));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"@ant-design/react-slick": "~1.1.2",
107107
"@babel/runtime": "^7.24.8",
108108
"@ctrl/tinycolor": "^3.6.1",
109-
"@rc-component/color-picker": "~1.9.0",
109+
"@rc-component/color-picker": "~2.0.0",
110110
"@rc-component/mutate-observer": "^1.1.0",
111111
"@rc-component/qrcode": "~1.0.0",
112112
"@rc-component/tour": "~1.15.0",

0 commit comments

Comments
 (0)