fix: ColorPicker hex precision (#50843)

* test: test driven

* fix: format logic

* chore: comment

* chore: rm useless lint rule

* chore: rm useless lint rule

* chore: rm useless lint rule

* chore: rm useless lint rule
This commit is contained in:
二货爱吃白萝卜 2024-09-13 15:41:53 +08:00 committed by GitHub
parent 22fb6f67fa
commit ce1356559d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 4 deletions

View File

@ -57,7 +57,11 @@
"noAccumulatingSpread": "off"
},
"a11y": {
"useKeyWithClickEvents": "off"
"noAriaHiddenOnFocusable": "off",
"noLabelWithoutControl": "off",
"useFocusableInteractive": "off",
"useKeyWithClickEvents": "off",
"useSemanticElements": "off"
}
}
},

View File

@ -936,4 +936,16 @@ describe('ColorPicker', () => {
});
});
});
it('input precision', async () => {
const onChange = jest.fn();
const { container } = render(<ColorPicker open onChange={onChange} />);
fireEvent.change(container.querySelector('.ant-color-picker-hex-input input')!, {
target: { value: '2ddcb4' },
});
const onChangeColor = onChange.mock.calls[0][0];
expect(onChangeColor.toHexString()).toBe('#2ddcb4');
});
});

View File

@ -19,9 +19,18 @@ export const getColorAlpha = (color: AggregationColor) => getRoundNumber(color.t
/** Return the color whose `alpha` is 1 */
export const genAlphaColor = (color: AggregationColor, alpha?: number) => {
const hsba = color.toHsb();
hsba.a = alpha || 1;
return generateColor(hsba);
const rgba = color.toRgb();
// Color from hsb input may get `rgb` is (0/0/0) when `hsb.b` is 0
// So if rgb is empty, we should get from hsb
if (!rgba.r && !rgba.g && !rgba.b) {
const hsba = color.toHsb();
hsba.a = alpha || 1;
return generateColor(hsba);
}
rgba.a = alpha || 1;
return generateColor(rgba);
};
/**