Skip to content

Commit fc12eb1

Browse files
authored
fix: preserve empty color in ColorPreviewInput instead of coercing to black (#4856)
1 parent 1631b79 commit fc12eb1

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

framework/core/js/src/common/components/ColorPreviewInput.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ export default class ColorPreviewInput<
2626
attrs.onchange?.({ target: { value: attrs.value } });
2727
}
2828

29-
// Validate the color
30-
if (!/^#[a-f0-9]{6}$/i.test(attrs.value)) {
29+
// An empty field is a valid "no colour" state and must be preserved as
30+
// such (stored as an empty string / null), not coerced to a colour. Only
31+
// a non-empty value that fails validation is corrected.
32+
if (attrs.value !== '' && !/^#[a-f0-9]{6}$/i.test(attrs.value)) {
3133
attrs.value = '#000000';
3234
attrs.onchange?.({ target: { value: attrs.value } });
3335
}

framework/core/js/tests/integration/common/components/ColorPreviewInput.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,28 @@ describe('ColorPreviewInput displays as expected', () => {
2929
input.trigger('input[type=color]', 'blur', { target: {} });
3030
expect(onchange).toHaveBeenCalled();
3131
});
32+
33+
it('preserves an empty value instead of coercing it to a colour', () => {
34+
// Clearing the field is a valid "no colour" state and must be saved as an
35+
// empty string, not turned into #000000 on blur.
36+
const onchange = jest.fn();
37+
const input = mq(ColorPreviewInput, { value: '', onchange });
38+
39+
// @ts-ignore
40+
input.trigger('input[type=color]', 'blur', { target: {} });
41+
42+
expect(onchange).not.toHaveBeenCalledWith({ target: { value: '#000000' } });
43+
});
44+
45+
it('keeps a deliberately-chosen black (#000000)', () => {
46+
// The empty-value exemption must not stop a user setting black on purpose:
47+
// #000000 is a valid colour and blur must leave it untouched.
48+
const onchange = jest.fn();
49+
const input = mq(ColorPreviewInput, { value: '#000000', onchange });
50+
51+
// @ts-ignore
52+
input.trigger('input[type=color]', 'blur', { target: {} });
53+
54+
expect(onchange).not.toHaveBeenCalled();
55+
});
3256
});

0 commit comments

Comments
 (0)