Skip to content

Commit

Permalink
fix(color-picker): ensure shorthand hex is expanded regardless of alp…
Browse files Browse the repository at this point in the history
…ha channel presence (#11188)

**Related Issue:** #11187 

## Summary

Fixes a regression from
#9701, where only RGBA
shorthand hex values would be accepted by
`calcite-color-picker-hex-input`.
  • Loading branch information
jcfranco authored Jan 8, 2025
1 parent 4b63087 commit dc7a0ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("calcite-color-picker-hex-input", () => {

it("commits shorthand hex on blur", async () => {
const defaultHex = "#b33f33";
const editedHex = "#aabbcc";
const expandedHex = "#aabbcc";
const page = await newE2EPage();
await page.setContent(`<calcite-color-picker-hex-input value='${defaultHex}'></calcite-color-picker-hex-input>`);

Expand All @@ -129,45 +129,53 @@ describe("calcite-color-picker-hex-input", () => {
await page.keyboard.press("Tab");
await page.waitForChanges();

expect(await input.getProperty("value")).toBe(editedHex);
expect(await input.getProperty("value")).toBe(expandedHex);

await selectText(input);
await page.keyboard.type("abcd");
await page.keyboard.press("Tab");
await page.waitForChanges();

expect(await input.getProperty("value")).toBe(editedHex);
expect(await input.getProperty("value")).toBe(expandedHex);
});

it("commits shorthand hexa on blur", async () => {
it("commits shorthand hex and hexa on blur", async () => {
const defaultHexa = "#b33f33ff";
const editedHexa = "#aabbccdd";
const expandedHexa = "#aabbccdd";
const expandedHex = "#aabbccff";
const page = await newE2EPage();
await page.setContent(
`<calcite-color-picker-hex-input alpha-channel value='${defaultHexa}'></calcite-color-picker-hex-input>`,
);

const input = await page.find(`calcite-color-picker-hex-input`);
await selectText(input);
await page.keyboard.type("abc");
await page.keyboard.type("ab");
await page.keyboard.press("Tab");
await page.waitForChanges();

expect(await input.getProperty("value")).toBe(defaultHexa);

await selectText(input);
await page.keyboard.type("abc");
await page.keyboard.press("Tab");
await page.waitForChanges();

expect(await input.getProperty("value")).toBe(expandedHex);

await selectText(input);
await page.keyboard.type("abcd");
await page.keyboard.press("Tab");
await page.waitForChanges();

expect(await input.getProperty("value")).toBe(editedHexa);
expect(await input.getProperty("value")).toBe(expandedHexa);

await selectText(input);
await page.keyboard.type("abcde");
await page.keyboard.press("Tab");
await page.waitForChanges();

expect(await input.getProperty("value")).toBe(editedHexa);
expect(await input.getProperty("value")).toBe(expandedHexa);
});

it("normalizes value when initialized", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ export class ColorPickerHexInput extends LitElement implements LoadableComponent
const { allowEmpty, internalColor } = this;
const willClearValue = allowEmpty && !inputValue;
const isLonghand = isLonghandHex(hex);
const anyShorthand = isShorthandHex(hex, true) || isShorthandHex(hex, false);

if (isShorthandHex(hex, this.alphaChannel)) {
if (anyShorthand) {
// ensure modified pasted hex values are committed since we prevent default to remove the # char.
this.onHexInputChange();
}
Expand Down

0 comments on commit dc7a0ef

Please sign in to comment.