Skip to content

Commit

Permalink
color update for select component
Browse files Browse the repository at this point in the history
very much a pain bc react-select does not support easy color update of that text style JedWatson/react-select#3953 (comment)
  • Loading branch information
KobeZ123 committed Jan 31, 2024
1 parent 66ece94 commit c492600
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/frontend/components/Form/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -39,8 +40,15 @@ export const PlanSelect: React.FC<PlanSelectProps> = ({
isSearchable,
noValueOptionLabel,
}) => {
const [selectIsFocused, setSelectIsFocused] = useState<boolean>(false);

const {
field: { onChange: onChangeUpdateValue, value, ...fieldRest },
field: {
onChange: onChangeUpdateValue,
onBlur: onUnfocusSelect,
value,
...fieldRest
},
fieldState: { error },
} = useController({ name, control, rules });

Expand All @@ -66,6 +74,17 @@ export const PlanSelect: React.FC<PlanSelectProps> = ({
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;
Expand All @@ -89,8 +108,16 @@ export const PlanSelect: React.FC<PlanSelectProps> = ({
onChange={onChange}
value={selectedOption}
isSearchable={isSearchable}
onBlur={onBlur}
onFocus={onFocus}
defaultValue={noValueOption}
{...fieldRest}
styles={{
singleValue: (baseStyles) => ({
...baseStyles,
color: selectIsFocused ? "gray" : "black",
}),
}}
/>
{helperText && <FormHelperText>{helperText}</FormHelperText>}
<FormErrorMessage>{error?.message}</FormErrorMessage>
Expand Down

0 comments on commit c492600

Please sign in to comment.