Skip to content
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

Prevent an empty badge color from blowing things up #1741

Merged
merged 3 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/badge/badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const EuiBadge = ({

function checkValidColor(props, propName, componentName) {
const validHex = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(props.color);
if (props.color && !validHex && !COLORS.includes(props.color)) {
if (props.color != null && !validHex && !COLORS.includes(props.color)) {
throw new Error(
`${componentName} needs to pass a valid color. This can either be a three ` +
`or six character hex value or one of the following: ${COLORS}`
Expand Down
11 changes: 9 additions & 2 deletions src/services/color/hex_to_rgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export function hexToRgb(hex: string): rgbDef {
(m, r1, g1, b1) => r1 + r1 + g1 + g1 + b1 + b1
);

const [, r, g, b] = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)!;
return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)];
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)!;

if (result) {
const [, r, g, b] = result;
return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)];
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
}

😅

// fallback to prevent errors
return [0, 0, 0];
}
}