From e0753d59ec085ff2604fc177f71bc9a32e6e70bc Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 23 Sep 2024 18:06:51 +0300 Subject: [PATCH] fix: generation input styles for different names of states --- .../appearance.style-generator.ts | 71 +++++++++---------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/generators/input/src/style-generators/appearance.style-generator.ts b/generators/input/src/style-generators/appearance.style-generator.ts index 2126eeb7..2957a713 100644 --- a/generators/input/src/style-generators/appearance.style-generator.ts +++ b/generators/input/src/style-generators/appearance.style-generator.ts @@ -1,4 +1,4 @@ -import type { InputColorSchemes } from '../input-generator.interfaces.js' +import type { InputColorSchemes } from '../input-generator.interfaces.js' import { writeFileSync } from 'fs' @@ -15,6 +15,8 @@ export class InputAppearanceStyleGenerator { #variants: Array = [] + #states: Array = [] + constructor(private readonly colorSchemes: InputColorSchemes) { const allVariants = Object.keys(colorSchemes).reduce>( (array, key) => (key.startsWith('input.') ? [...array, key.split('.')[1]] : array), @@ -22,6 +24,14 @@ export class InputAppearanceStyleGenerator { ) this.#variants = [...new Set(allVariants)] + + const allStates = Object.keys(this.colorSchemes).reduce>( + (array, key) => + key.startsWith(`input.${this.#variants[0]}`) ? [...array, key.split('.')[2]] : array, + [] + ) + + this.#states = [...new Set(allStates)] } generateAppearanceStyles(): Record<'appearanceStyles' | 'imports' | 'statefulStyles', string> { @@ -29,26 +39,9 @@ export class InputAppearanceStyleGenerator { this.#variants.map((variant) => this.generateVariantStatefulStyles(variant)).join('\n\n') ) - const appearanceStyles = pretty(` - export const appearanceVariant = { - ${this.generateVariantAppearanceStyles(this.#variants, 'Default', false)} - } - - export const appearanceHover = { - ${this.generateVariantAppearanceStyles(this.#variants, 'Hover')} - } - - export const appearanceFocus = { - ${this.generateVariantAppearanceStyles(this.#variants, 'Focus')} - } - - export const appearanceActive = { - ${this.generateVariantAppearanceStyles(this.#variants, 'Active')} - } - - export const appearanceDisabled = { - ${this.generateVariantAppearanceStyles(this.#variants, 'Disabled')} - }`) + const appearanceStyles = pretty( + this.#states.map((state) => this.generateVariantAppearanceStyles(state)).join('\n\n') + ) const imports = pretty( this.requiredImports @@ -78,14 +71,7 @@ export class InputAppearanceStyleGenerator { private generateVariantStatefulStyles(variant: string): string { const lines: Array = [] - const allStates: Array = Object.keys(this.colorSchemes).reduce>( - (array, key) => (key.startsWith(`input.${variant}`) ? [...array, key.split('.')[2]] : array), - [] - ) - - const uniqueStates = [...new Set(allStates)] - - for (const state of uniqueStates) { + for (const state of this.#states) { lines.push(`const ${getAppearanceStylesName(variant, state)} = createAppearanceStyles({ fontColor: vars.colors['input.${variant}.${state}.font'], backgroundColor: vars.colors['input.${variant}.${state}.background'], @@ -96,14 +82,25 @@ export class InputAppearanceStyleGenerator { return lines.join('\n\n') } - private generateVariantAppearanceStyles( - variants: Array, - state: string, - addSuffix: boolean = true - ): string { - const suffix = addSuffix ? state : '' - return variants - .map((variant) => `${variant}${suffix}: ${getAppearanceStylesName(variant, state)},`) + private generateVariantAppearanceStyles(state: string): string { + if (state === 'default') { + const variantStyles = this.#variants + .map((variant) => `${variant}: ${getAppearanceStylesName(variant, 'Default')},`) + .join('\n') + + return `export const appearanceVariant = { + ${variantStyles} + }` + } + + const stateName = capitalizeFirstLetter(state) + + const variantStyles = this.#variants + .map((variant) => `${variant}${stateName}: ${getAppearanceStylesName(variant, stateName)},`) .join('\n') + + return `export const appearance${stateName} = { + ${variantStyles} + }` } }