Skip to content

Commit

Permalink
TextInput: add optional labelColor property to control label color (#463
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hoorayimhelping authored Aug 14, 2024
1 parent 6f8207f commit 0ef6239
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/Input/InputWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,16 @@ const Wrapper = styled.div<{
`}
`;

const StyledLabel = styled(Label)<{ $labelColor?: string }>`
${({ $labelColor }) => `
${$labelColor ? `color: ${$labelColor};` : ""}
`}
`;

export interface WrapperProps {
id: string;
label?: ReactNode;
labelColor?: string;
error?: ReactNode;
disabled?: boolean;
children: ReactNode;
Expand All @@ -135,6 +142,7 @@ export interface WrapperProps {
export const InputWrapper = ({
id,
label = "",
labelColor,
error,
disabled,
children,
Expand All @@ -160,13 +168,14 @@ export const InputWrapper = ({
{!!error && error !== true && <Error>{error}</Error>}
</FormElementContainer>
{label && (
<Label
<StyledLabel
htmlFor={id}
disabled={disabled}
error={!!error}
$labelColor={labelColor}
>
{label}
</Label>
</StyledLabel>
)}
</FormRoot>
);
Expand Down
11 changes: 11 additions & 0 deletions src/components/Input/TextField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ export const Playground = {
placeholder: "Placeholder",
},
};

export const LabelColor = {
args: {
label: "Label",
labelColor: "rgb(193, 0, 0)",
clear: false,
type: "text",
disabled: false,
placeholder: "Placeholder",
},
};
60 changes: 60 additions & 0 deletions src/components/Input/TextField.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { TextField } from "@/components/Input/TextField";
import { renderCUI } from "@/utils/test-utils";
import { useState } from "react";

const TextFieldWrapper = ({
initialText = "",
label,
labelColor,
}: {
initialText?: string;
label: string;
labelColor?: string;
}) => {
const [text, setText] = useState<string>(initialText);

console.log(text);
return (
<TextField
label={label}
labelColor={labelColor}
onChange={setText}
value={text}
/>
);
};

describe("TextField", () => {
describe("label color", () => {
it("is the default color when labelColor is not set", () => {
const label = "Hello there!";
const text = "General Kenobi";

const { getByText } = renderCUI(
<TextFieldWrapper
label={label}
initialText={text}
/>
);

const labelElement = getByText(label);
expect(labelElement).toBeInTheDocument();
expect(labelElement).toHaveStyle("color: rgb(179, 182, 189)");
});

it("is the color of the passed in labelColor", () => {
const label = "Hello there!";

const { getByText } = renderCUI(
<TextFieldWrapper
label={label}
labelColor="#FF0000"
/>
);

const labelElement = getByText(label);
expect(labelElement).toBeInTheDocument();
expect(labelElement).toHaveStyle("color: #FF0000");
});
});
});
3 changes: 3 additions & 0 deletions src/components/Input/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface TextFieldProps
> {
type?: "text" | "email" | "tel" | "url";
loading?: boolean;
labelColor?: string;
value?: string;
clear?: boolean;
onChange: (inputValue: string, e?: ChangeEvent<HTMLInputElement>) => void;
Expand All @@ -25,6 +26,7 @@ export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
type,
disabled,
label,
labelColor,
error,
id,
loading,
Expand All @@ -50,6 +52,7 @@ export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
disabled={disabled}
id={id ?? defaultId}
label={label}
labelColor={labelColor}
error={error}
orientation={orientation}
dir={dir}
Expand Down

0 comments on commit 0ef6239

Please sign in to comment.