From f871bf1dbdf4deda7488b2b670becf0eb2ae54a3 Mon Sep 17 00:00:00 2001 From: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com> Date: Mon, 31 Jan 2022 21:06:45 +0700 Subject: [PATCH] chore(tools): convert color-system to TypeScript (#44970) * Change file extension * Move the files to their own folder * Fix typecheck --- .../color-system.stories.tsx} | 0 .../color-system.tsx} | 36 +++++++++---------- tools/ui-components/src/color-system/types.ts | 10 ++++++ tools/ui-components/src/colors.css | 36 +++++++++++++++++++ tools/ui-components/src/declarations.d.ts | 1 + 5 files changed, 64 insertions(+), 19 deletions(-) rename tools/ui-components/src/{color-system.stories.js => color-system/color-system.stories.tsx} (100%) rename tools/ui-components/src/{color-system.js => color-system/color-system.tsx} (72%) create mode 100644 tools/ui-components/src/color-system/types.ts create mode 100644 tools/ui-components/src/declarations.d.ts diff --git a/tools/ui-components/src/color-system.stories.js b/tools/ui-components/src/color-system/color-system.stories.tsx similarity index 100% rename from tools/ui-components/src/color-system.stories.js rename to tools/ui-components/src/color-system/color-system.stories.tsx diff --git a/tools/ui-components/src/color-system.js b/tools/ui-components/src/color-system/color-system.tsx similarity index 72% rename from tools/ui-components/src/color-system.js rename to tools/ui-components/src/color-system/color-system.tsx index f4540aaa5c..ee9d97d3a5 100644 --- a/tools/ui-components/src/color-system.js +++ b/tools/ui-components/src/color-system/color-system.tsx @@ -1,19 +1,26 @@ -import PropTypes from 'prop-types'; import React from 'react'; -import colorList from './colors.css'; +import colorList from '../colors.css'; +import { Color, ColorList, PaletteProps } from './types'; // ---------------------------------------------------------- // // HELPER FUNCTIONS // // ---------------------------------------------------------- // -// Transform colorList from an object to an array of objects -const transformedColorList = Object.keys(colorList).map(colorName => ({ - label: colorName.replace('--', ''), - value: colorList[colorName] -})); +/** + * Transform colorList from an object to an array of objects + * @example + * Input: { '--blue10': 'var(--blue10)' } + * Output: [{ label: 'blue10', value: 'var(--blue10)' }] + */ +const transformedColorList = Object.keys(colorList as ColorList).map( + colorName => ({ + label: colorName.replace('--', ''), + value: (colorList as ColorList)[colorName] + }) +); // Get the background and text color values of each palette item -const getPaletteItemStyle = color => { +const getPaletteItemStyle = (color: Color) => { const itemTextColor = color.label.substring(color.label.length - 2); return { @@ -24,13 +31,13 @@ const getPaletteItemStyle = color => { }; }; -const getPaletteByColorName = name => +const getPaletteByColorName = (name: string) => transformedColorList.filter(color => color.label.includes(name)); // ---------------------------------------------------------- // // COMPONENTS // // ---------------------------------------------------------- // -const Palette = ({ colors }) => { +const Palette = ({ colors }: PaletteProps) => { return (