-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maps] Safely handle empty string and invalid strings from EuiColorPi…
…cker (#62507) * [Maps] Safely handle empty string and invalid strings from EuiColorPicker * move RGBA_0000 to constants
- Loading branch information
Showing
9 changed files
with
117 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...y/plugins/maps/public/layers/styles/vector/components/color/mb_validated_color_picker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { Component } from 'react'; | ||
import { isValidHex, EuiColorPicker, EuiFormControlLayoutProps } from '@elastic/eui'; | ||
|
||
export const RGBA_0000 = 'rgba(0,0,0,0)'; | ||
|
||
interface Props { | ||
onChange: (color: string) => void; | ||
color: string; | ||
swatches?: string[]; | ||
append?: EuiFormControlLayoutProps['append']; | ||
} | ||
|
||
interface State { | ||
colorInputValue: string; | ||
} | ||
|
||
// EuiColorPicker treats '' or invalid colors as transparent. | ||
// Mapbox logs errors for '' or invalid colors. | ||
// MbValidatedColorPicker is a wrapper around EuiColorPicker that reconciles the behavior difference | ||
// between the two by returning a Mapbox safe RGBA_0000 for '' or invalid colors | ||
// while keeping invalid state local so EuiColorPicker's input properly handles text input. | ||
export class MbValidatedColorPicker extends Component<Props, State> { | ||
state = { | ||
colorInputValue: this.props.color === RGBA_0000 ? '' : this.props.color, | ||
}; | ||
|
||
_onColorChange = (color: string) => { | ||
// reflect all user input, whether valid or not | ||
this.setState({ colorInputValue: color }); | ||
// Only surface mapbox valid input to caller | ||
this.props.onChange(isValidHex(color) ? color : RGBA_0000); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<EuiColorPicker | ||
onChange={this._onColorChange} | ||
color={this.state.colorInputValue} | ||
swatches={this.props.swatches} | ||
append={this.props.append} | ||
compressed | ||
/> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters