-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(color-picker): add clearable
prop and deprecate allowEmpty
#8910
Changes from 4 commits
db87fef
8c46f48
b78503d
66867f8
2e43f30
5e844a2
e6acc84
1d1619a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,14 +98,31 @@ export class ColorPicker | |
* When `true`, an empty color (`null`) will be allowed as a `value`. | ||
* | ||
* When `false`, a color value is enforced, and clearing the input or blurring will restore the last valid `value`. | ||
* | ||
* @deprecated use `clearable` instead | ||
*/ | ||
|
||
@Prop({ reflect: true }) allowEmpty = false; | ||
jcfranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Watch("allowEmpty") | ||
@Watch("clearable") | ||
handleEmptyValues(): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you revisit this name? IMO, it doesn't convey what it's doing. |
||
this.isClearable = !!(this.clearable || this.allowEmpty); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
|
||
/** | ||
* When `true`, the component will allow updates to the color's alpha value. | ||
*/ | ||
@Prop() alphaChannel = false; | ||
|
||
/** | ||
* When `true`, an empty color (`null`) will be allowed as a `value`. | ||
* | ||
* When `false`, a color value is enforced, and clearing the input or blurring will restore the last valid `value`. | ||
*/ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, can you remove this newline? |
||
@Prop({ reflect: true }) clearable = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this prop and associated doc so that it is sorted alphabetically within the properties section? |
||
|
||
@Watch("alphaChannel") | ||
handleAlphaChannelChange(alphaChannel: boolean): void { | ||
const { format } = this; | ||
|
@@ -225,8 +242,8 @@ export class ColorPicker | |
|
||
@Watch("value") | ||
handleValueChange(value: ColorValue | null, oldValue: ColorValue | null): void { | ||
const { allowEmpty, format } = this; | ||
const checkMode = !allowEmpty || value; | ||
const { isClearable, format } = this; | ||
const checkMode = !isClearable || value; | ||
let modeChanged = false; | ||
|
||
if (checkMode) { | ||
|
@@ -258,7 +275,7 @@ export class ColorPicker | |
} | ||
|
||
const color = | ||
allowEmpty && !value | ||
isClearable && !value | ||
? null | ||
: Color( | ||
value != null && typeof value === "object" && alphaCompatible(this.mode) | ||
|
@@ -293,6 +310,8 @@ export class ColorPicker | |
return this.color || this.previousColor || DEFAULT_COLOR; | ||
} | ||
|
||
private isClearable: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this so that it is in alphabetical order within its fellow private vars? |
||
|
||
private checkerPattern: HTMLCanvasElement; | ||
|
||
private colorFieldRenderingContext: CanvasRenderingContext2D; | ||
|
@@ -417,11 +436,11 @@ export class ColorPicker | |
|
||
private handleHexInputChange = (event: Event): void => { | ||
event.stopPropagation(); | ||
const { allowEmpty, color } = this; | ||
const { isClearable, color } = this; | ||
const input = event.target as HTMLCalciteColorPickerHexInputElement; | ||
const hex = input.value; | ||
|
||
if (allowEmpty && !hex) { | ||
if (isClearable && !hex) { | ||
this.internalColorSet(null); | ||
return; | ||
} | ||
|
@@ -451,7 +470,7 @@ export class ColorPicker | |
|
||
let inputValue: string; | ||
|
||
if (this.allowEmpty && !input.value) { | ||
if (this.isClearable && !input.value) { | ||
inputValue = ""; | ||
} else { | ||
const value = Number(input.value); | ||
|
@@ -508,7 +527,7 @@ export class ColorPicker | |
const channelIndex = Number(input.getAttribute("data-channel-index")); | ||
const channels = [...this.channels] as this["channels"]; | ||
|
||
const shouldClearChannels = this.allowEmpty && !input.value; | ||
const shouldClearChannels = this.isClearable && !input.value; | ||
|
||
if (shouldClearChannels) { | ||
this.channels = [null, null, null, null]; | ||
|
@@ -665,10 +684,9 @@ export class ColorPicker | |
|
||
async componentWillLoad(): Promise<void> { | ||
setUpLoadableComponent(this); | ||
|
||
const { allowEmpty, color, format, value } = this; | ||
|
||
const willSetNoColor = allowEmpty && !value; | ||
this.handleEmptyValues(); | ||
const { isClearable, color, format, value } = this; | ||
jcfranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const willSetNoColor = isClearable && !value; | ||
const parsedMode = parseMode(value); | ||
const valueIsCompatible = | ||
willSetNoColor || (format === "auto" && parsedMode) || format === parsedMode; | ||
|
@@ -677,7 +695,6 @@ export class ColorPicker | |
if (!valueIsCompatible) { | ||
this.showIncompatibleColorWarning(value, format); | ||
} | ||
|
||
this.setMode(format, false); | ||
this.internalColorSet(initialColor, false, "initial"); | ||
|
||
|
@@ -722,7 +739,6 @@ export class ColorPicker | |
|
||
render(): VNode { | ||
const { | ||
allowEmpty, | ||
channelsDisabled, | ||
color, | ||
colorFieldScopeLeft, | ||
|
@@ -862,7 +878,7 @@ export class ColorPicker | |
{noHex ? null : ( | ||
<div class={CSS.hexOptions}> | ||
<calcite-color-picker-hex-input | ||
allowEmpty={allowEmpty} | ||
allowEmpty={this.isClearable} | ||
alphaChannel={alphaChannel} | ||
class={CSS.control} | ||
messages={messages} | ||
|
@@ -972,7 +988,7 @@ export class ColorPicker | |
}; | ||
|
||
private renderChannelsTab = (channelMode: this["channelMode"]): VNode => { | ||
const { allowEmpty, channelMode: activeChannelMode, channels, messages, alphaChannel } = this; | ||
const { isClearable, channelMode: activeChannelMode, channels, messages, alphaChannel } = this; | ||
const selected = channelMode === activeChannelMode; | ||
const isRgb = channelMode === "rgb"; | ||
const channelAriaLabels = isRgb | ||
|
@@ -990,7 +1006,7 @@ export class ColorPicker | |
|
||
if (isAlphaChannel) { | ||
channelValue = | ||
allowEmpty && !channelValue ? channelValue : alphaToOpacity(channelValue); | ||
isClearable && !channelValue ? channelValue : alphaToOpacity(channelValue); | ||
} | ||
|
||
/* the channel container is ltr, so we apply the host's direction */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you capitalize
use
? I'll submit a separate PR to ensure we capitalize deprecation messages.