From c492600b2099707d3a4d82d4842c5635d5654de0 Mon Sep 17 00:00:00 2001 From: Kobe Date: Wed, 31 Jan 2024 11:31:50 -0500 Subject: [PATCH] color update for select component very much a pain bc react-select does not support easy color update of that text style https://github.com/JedWatson/react-select/issues/3953#issuecomment-761238366 --- packages/frontend/components/Form/Select.tsx | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/frontend/components/Form/Select.tsx b/packages/frontend/components/Form/Select.tsx index d255feb0c..f46261a73 100644 --- a/packages/frontend/components/Form/Select.tsx +++ b/packages/frontend/components/Form/Select.tsx @@ -4,6 +4,7 @@ import { FormErrorMessage, FormHelperText, } from "@chakra-ui/react"; +import { useState } from "react"; import { Control, FieldError, useController } from "react-hook-form"; import Select from "react-select"; @@ -39,8 +40,15 @@ export const PlanSelect: React.FC = ({ isSearchable, noValueOptionLabel, }) => { + const [selectIsFocused, setSelectIsFocused] = useState(false); + const { - field: { onChange: onChangeUpdateValue, value, ...fieldRest }, + field: { + onChange: onChangeUpdateValue, + onBlur: onUnfocusSelect, + value, + ...fieldRest + }, fieldState: { error }, } = useController({ name, control, rules }); @@ -66,6 +74,17 @@ export const PlanSelect: React.FC = ({ onChangeUpdateValue(val); }; + // action when select is unfocused + const onBlur = () => { + setSelectIsFocused(false); + onUnfocusSelect(); + }; + + // action when select is focused + const onFocus = () => { + setSelectIsFocused(true); + }; + let selectedValue = value; if (isNumeric) { selectedValue = value ? value.toString() : null; @@ -89,8 +108,16 @@ export const PlanSelect: React.FC = ({ onChange={onChange} value={selectedOption} isSearchable={isSearchable} + onBlur={onBlur} + onFocus={onFocus} defaultValue={noValueOption} {...fieldRest} + styles={{ + singleValue: (baseStyles) => ({ + ...baseStyles, + color: selectIsFocused ? "gray" : "black", + }), + }} /> {helperText && {helperText}} {error?.message}