-
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 1 commit
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 |
---|---|---|
|
@@ -99,6 +99,9 @@ export class ColorPicker | |
* | ||
* When `false`, a color value is enforced, and clearing the input or blurring will restore the last valid `value`. | ||
*/ | ||
|
||
@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 also move this so it's in alphabetical order within the properties section? |
||
|
||
@Prop({ reflect: true }) allowEmpty = false; | ||
jcfranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
|
@@ -118,6 +121,24 @@ export class ColorPicker | |
} | ||
} | ||
|
||
@Watch("allowEmpty") | ||
handleAllowEmpty(): void { | ||
if (this.allowEmpty === true) { | ||
this.allowNullValues = true; | ||
} else if (this.allowEmpty == false && this.clearable == false) { | ||
this.allowNullValues = false; | ||
} | ||
} | ||
|
||
@Watch("clearable") | ||
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. This and the above watcher have very similar code. Please consolidate into a single watcher to be used for both props (see Stencil |
||
handleClearableChange(): void { | ||
if (this.clearable === true) { | ||
this.allowNullValues = true; | ||
} else if (this.allowEmpty == false && this.clearable == false) { | ||
this.allowNullValues = false; | ||
} | ||
} | ||
|
||
/** When `true`, hides the RGB/HSV channel inputs. */ | ||
@Prop() channelsDisabled = false; | ||
|
||
|
@@ -225,8 +246,8 @@ export class ColorPicker | |
|
||
@Watch("value") | ||
handleValueChange(value: ColorValue | null, oldValue: ColorValue | null): void { | ||
const { allowEmpty, format } = this; | ||
const checkMode = !allowEmpty || value; | ||
const { allowNullValues, format } = this; | ||
const checkMode = !allowNullValues || value; | ||
let modeChanged = false; | ||
|
||
if (checkMode) { | ||
|
@@ -258,7 +279,7 @@ export class ColorPicker | |
} | ||
|
||
const color = | ||
allowEmpty && !value | ||
allowNullValues && !value | ||
? null | ||
: Color( | ||
value != null && typeof value === "object" && alphaCompatible(this.mode) | ||
|
@@ -315,6 +336,8 @@ export class ColorPicker | |
|
||
private shiftKeyChannelAdjustment = 0; | ||
|
||
private allowNullValues = !!(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. I know we talked about this, but let's make sure this variable is assigned in a lifecycle method vs when the component is instantiated. This works fine in the lazy-loaded output target, but does not get properly assigned in the 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 we rename this to |
||
|
||
@State() defaultMessages: ColorPickerMessages; | ||
|
||
@State() channelMode: ColorMode = "rgb"; | ||
|
@@ -417,11 +440,11 @@ export class ColorPicker | |
|
||
private handleHexInputChange = (event: Event): void => { | ||
event.stopPropagation(); | ||
const { allowEmpty, color } = this; | ||
const { allowNullValues, color } = this; | ||
const input = event.target as HTMLCalciteColorPickerHexInputElement; | ||
const hex = input.value; | ||
|
||
if (allowEmpty && !hex) { | ||
if (allowNullValues && !hex) { | ||
this.internalColorSet(null); | ||
return; | ||
} | ||
|
@@ -451,7 +474,7 @@ export class ColorPicker | |
|
||
let inputValue: string; | ||
|
||
if (this.allowEmpty && !input.value) { | ||
if (this.allowNullValues && !input.value) { | ||
inputValue = ""; | ||
} else { | ||
const value = Number(input.value); | ||
|
@@ -508,7 +531,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.allowNullValues && !input.value; | ||
|
||
if (shouldClearChannels) { | ||
this.channels = [null, null, null, null]; | ||
|
@@ -666,9 +689,8 @@ export class ColorPicker | |
async componentWillLoad(): Promise<void> { | ||
setUpLoadableComponent(this); | ||
|
||
const { allowEmpty, color, format, value } = this; | ||
|
||
const willSetNoColor = allowEmpty && !value; | ||
const { allowNullValues, color, format, value } = this; | ||
const willSetNoColor = allowNullValues && !value; | ||
const parsedMode = parseMode(value); | ||
const valueIsCompatible = | ||
willSetNoColor || (format === "auto" && parsedMode) || format === parsedMode; | ||
|
@@ -677,7 +699,7 @@ export class ColorPicker | |
if (!valueIsCompatible) { | ||
this.showIncompatibleColorWarning(value, format); | ||
} | ||
|
||
console.log(allowNullValues); | ||
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. Please remove log statements. |
||
this.setMode(format, false); | ||
this.internalColorSet(initialColor, false, "initial"); | ||
|
||
|
@@ -722,7 +744,6 @@ export class ColorPicker | |
|
||
render(): VNode { | ||
const { | ||
allowEmpty, | ||
channelsDisabled, | ||
color, | ||
colorFieldScopeLeft, | ||
|
@@ -862,7 +883,7 @@ export class ColorPicker | |
{noHex ? null : ( | ||
<div class={CSS.hexOptions}> | ||
<calcite-color-picker-hex-input | ||
allowEmpty={allowEmpty} | ||
allowEmpty={this.allowNullValues} | ||
alphaChannel={alphaChannel} | ||
class={CSS.control} | ||
messages={messages} | ||
|
@@ -972,7 +993,13 @@ export class ColorPicker | |
}; | ||
|
||
private renderChannelsTab = (channelMode: this["channelMode"]): VNode => { | ||
const { allowEmpty, channelMode: activeChannelMode, channels, messages, alphaChannel } = this; | ||
const { | ||
allowNullValues, | ||
channelMode: activeChannelMode, | ||
channels, | ||
messages, | ||
alphaChannel, | ||
} = this; | ||
const selected = channelMode === activeChannelMode; | ||
const isRgb = channelMode === "rgb"; | ||
const channelAriaLabels = isRgb | ||
|
@@ -990,7 +1017,7 @@ export class ColorPicker | |
|
||
if (isAlphaChannel) { | ||
channelValue = | ||
allowEmpty && !channelValue ? channelValue : alphaToOpacity(channelValue); | ||
allowNullValues && !channelValue ? channelValue : alphaToOpacity(channelValue); | ||
} | ||
|
||
/* the channel container is ltr, so we apply the host's direction */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ <h1 style="margin: 0 auto; text-align: center">Color picker</h1> | |
<div class="parent"> | ||
<div class="child right-aligned-text">Default</div> | ||
<div class="child"> | ||
<calcite-color-picker scale="s"></calcite-color-picker> | ||
<calcite-color-picker clearable scale="s"></calcite-color-picker> | ||
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. This is unrelated to the existing demo block. Please revert. |
||
</div> | ||
|
||
<div class="child"> | ||
|
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.
This should be removed since we don't want to promote its use.