From 9f4112bb292227d89495bd89d2b8cc83234c1329 Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Tue, 16 Apr 2019 10:07:58 -0500 Subject: [PATCH 1/3] distinguish between named colors and arbitrary strings --- src/components/icon/icon.test.tsx | 5 +++-- src/components/icon/icon.tsx | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/components/icon/icon.test.tsx b/src/components/icon/icon.test.tsx index cd425c82ca5..c00de1e3f08 100644 --- a/src/components/icon/icon.test.tsx +++ b/src/components/icon/icon.test.tsx @@ -43,12 +43,13 @@ describe('EuiIcon', () => { }); describe('color', () => { - COLORS.concat([ + [ + ...COLORS, '#fde', '#885522', 'rgb(100, 150, 200)', 'hsla(270, 60%, 70%, 0.9)', - ]).forEach(color => { + ].forEach(color => { test(`${color} is rendered`, () => { const component = render(); diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx index 03788e08477..c3109fd7572 100644 --- a/src/components/icon/icon.tsx +++ b/src/components/icon/icon.tsx @@ -603,7 +603,7 @@ export const TYPES: IconType[] = keysOf(typeToIconMap); export type IconType = keyof typeof typeToIconMap; -const colorToClassMap: { [color: string]: string | null } = { +const colorToClassMap = { default: null, primary: 'euiIcon--primary', secondary: 'euiIcon--secondary', @@ -616,10 +616,20 @@ const colorToClassMap: { [color: string]: string | null } = { ghost: 'euiIcon--ghost', }; -export const COLORS: IconColor[] = keysOf(colorToClassMap); +const indexedColorToClassMap: { + [color: string]: string | null; +} = colorToClassMap; + +export const COLORS: NamedColor[] = keysOf(colorToClassMap); + +type NamedColor = keyof typeof colorToClassMap; + +function isNamedColor(name: string): name is NamedColor { + return Object.keys(colorToClassMap).indexOf(name) !== -1; +} // We accept arbitrary color strings, which are impossible to type. -export type IconColor = string | keyof typeof colorToClassMap; +export type IconColor = string | NamedColor; const sizeToClassNameMap = { original: null, @@ -659,8 +669,8 @@ export const EuiIcon: FunctionComponent = ({ let optionalCustomStyles = null; if (color) { - if (COLORS.indexOf(color) > -1) { - optionalColorClass = colorToClassMap[color]; + if (isNamedColor(color)) { + optionalColorClass = indexedColorToClassMap[color]; } else { optionalCustomStyles = { fill: color }; } From 450e53c4f6250ed8dd41ffad54d60e7853f59a54 Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Tue, 16 Apr 2019 10:52:46 -0500 Subject: [PATCH 2/3] clean up --- src/components/icon/icon.tsx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx index c3109fd7572..80db2e2bf76 100644 --- a/src/components/icon/icon.tsx +++ b/src/components/icon/icon.tsx @@ -616,16 +616,12 @@ const colorToClassMap = { ghost: 'euiIcon--ghost', }; -const indexedColorToClassMap: { - [color: string]: string | null; -} = colorToClassMap; - export const COLORS: NamedColor[] = keysOf(colorToClassMap); type NamedColor = keyof typeof colorToClassMap; function isNamedColor(name: string): name is NamedColor { - return Object.keys(colorToClassMap).indexOf(name) !== -1; + return colorToClassMap.hasOwnProperty(name); } // We accept arbitrary color strings, which are impossible to type. @@ -670,7 +666,7 @@ export const EuiIcon: FunctionComponent = ({ if (color) { if (isNamedColor(color)) { - optionalColorClass = indexedColorToClassMap[color]; + optionalColorClass = colorToClassMap[color]; } else { optionalCustomStyles = { fill: color }; } From 8f8e3656354ab78a2285e9483b7235942872e49f Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Tue, 16 Apr 2019 11:09:06 -0500 Subject: [PATCH 3/3] CL --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4073f74ae3c..6866bd4c25c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [`master`](https://github.com/elastic/eui/tree/master) -No public interface changes since `10.0.0`. +- Convert `EuiText`, `EuiTextColor` and `EuiTextAlign` to TS ([#1791](https://github.com/elastic/eui/pull/1791)) +- Updated `IconColor` type to better distinguish between accepted types ([#1842](https://github.com/elastic/eui/pull/1842)) ## [`10.0.0`](https://github.com/elastic/eui/tree/v10.0.0) @@ -9,7 +10,6 @@ No public interface changes since `10.0.0`. - Updated the overflow shadows for `EuiModal` and `EuiFlyout` ([#1829](https://github.com/elastic/eui/pull/1829)) - Added `confirmButtonDisabled` prop to `EuiConfirmModal` ([#1829](https://github.com/elastic/eui/pull/1829)) - Fixed `EuiNavDrawer` overflow scroll behavior on Firefox ([#1837](https://github.com/elastic/eui/pull/1837)) -- Convert `EuiText`, `EuiTextColor` and `EuiTextAlign` to TS ([#1791](https://github.com/elastic/eui/pull/1791)) **Bug fixes**