I'm faced with the challenge of styling a button based on the selection made in a select box. The code snippet I have currently is as follows:
const buttonStyles = {
backgroundColor: buttonStyle === 'Black' ? colors.interactiveForegroundSecondary : buttonStyle === 'Green' ? colors.backgroundInverseSecondary : buttonStyle === 'White' ? colors.backgroundPrimary : buttonStyle === 'Lime Green' ? colors.graphTertiary : '',
color: buttonStyle === 'Black' ? colors.textInversePrimary : buttonStyle === 'Green' ? colors.textInversePrimary : buttonStyle === 'White' ? colors.interactiveForegroundSecondary : buttonStyle === 'Lime Green' ? colors.backgroundInverseSecondary : '',
"&:hover": {
background: buttonStyle === 'Black' ? colors.backgroundInversePrimary : buttonStyle === 'Green' ? colors.accentPrimary : buttonStyle === 'White' ? colors.errorBackground : buttonStyle === 'Lime Green' ? colors.backgroundInverseSecondary : '',
color: buttonStyle === 'Lime Green' ? colors.graphTertiary : ''
},
} as CrateStyleObject
My aim is to refactor this code into an Enum map in typescript. I have started with the following approach:
enum ButtonBackground {
Black = colors.interactiveForegroundSecondary,
Green = colors.backgroundInverseSecondary,
White = colors.backgroundPrimary,
Lime Green = colors.graphTertiary ,
}
const submitBackground = (Object.keys(ButtonBackground) as (keyof typeof ButtonBackground)[]).map(
(key, index) => {
console.log(key);
return ButtonBackground[key] + index;
},
);
Then I plan on applying it to the component as shown below:
<Button
style={
buttonStyles
}
type="submit">
{text}
</Button/>
However, I'm facing a challenge in assigning
colors.interactiveForegroundSecondary
to one of the keys due to it not being a string. Additionally, I need the background color and text color of the button to change based on the selected value. Would creating separate Enums for each background color, text color, hover background color, and hover text color be the solution to this issue?