Skip to content

Commit

Permalink
Add ClassName prop to SearchField and TextField + add SearchField arg…
Browse files Browse the repository at this point in the history
…types
  • Loading branch information
nishasy committed Jan 9, 2024
1 parent 163cfca commit 3659538
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __docs__/wonder-blocks-form/text-field.argtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ export default {
},
},

className: {
description: "Custom class name for the input.",
table: {
type: {
summary: "string",
},
},
control: {
type: "text",
},
},

testId: {
description: "Optional test ID for e2e testing.",
table: {
Expand Down
177 changes: 177 additions & 0 deletions __docs__/wonder-blocks-search-field/search-field.argtypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
export default {
clearAriaLabel: {
description: `ARIA label for the clear button. Defaults to "Clear search".`,
table: {
type: {
summary: "string",
},
category: "Accessibility",
},
control: {
type: "text",
},
},
id: {
description: `The unique identifier for the input. If one is not
provided, a unique id will be generated.`,
type: {name: "string", required: true},
table: {
type: {
summary: "string",
},
},
control: {
type: "text",
},
},
value: {
description: "The text input value.",
type: {name: "string", required: true},
table: {
type: {
summary: "string",
},
},
control: {type: "text"},
},
placeholder: {
description: `Provide hints or examples of what to enter.
This shows up as a grayed out text in the field before
a value is entered.`,
table: {
type: {
summary: "string",
},
},
control: {
type: "text",
},
},
autoFocus: {
description: "Whether this field should autofocus on page load.",
table: {
type: {
summary: "boolean",
},
defaultValue: {
summary: "false",
},
},
control: {
type: "boolean",
},
},
disabled: {
description: `Makes a read-only input field that cannot be focused.
Defaults to false.`,
table: {
type: {
summary: "boolean",
},
defaultValue: {
summary: "false",
},
},
control: {
type: "boolean",
},
},
light: {
description:
"Change the default focus ring color to fit a dark background.",
table: {
type: {
summary: "boolean",
},
defaultValue: {
summary: "false",
},
},
control: {
type: "boolean",
},
},
style: {
description: "Custom styles for the main wrapper.",
table: {
type: {
summary: "Style",
},
defaultValue: {
summary: "StyleType",
},
},
control: {
type: "select",
options: ["box", "underline"],
},
},
className: {
description: "Optional CSS classes for the input field.",
table: {
type: {
summary: "string",
},
},
control: {
type: "text",
},
},
testId: {
description: "Test ID used for e2e testing.",
table: {
type: {
summary: "string",
},
},
control: {
type: "text",
},
},
onChange: {
description: "Called when the value has changed.",
table: {
type: {
summary: "function",
},
category: "Events",
},
},
onClick: {
description: `Handler that is triggered when this component is clicked.
For example, use this to adjust focus in parent component.`,
table: {
type: {
summary: "function",
},
category: "Events",
},
},
onKeyDown: {
description: "Called when a key is pressed.",
table: {
type: {
summary: "function",
},
category: "Events",
},
},
onFocus: {
description: "Called when the element has been focused.",
table: {
type: {
summary: "function",
},
category: "Events",
},
},
onBlur: {
description: "Called when the element has been blurred.",
table: {
type: {
summary: "function",
},
category: "Events",
},
},
};
3 changes: 2 additions & 1 deletion __docs__/wonder-blocks-search-field/search-field.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import SearchField from "@khanacademy/wonder-blocks-search-field";

import ComponentInfo from "../../.storybook/components/component-info";
import packageConfig from "../../packages/wonder-blocks-search-field/package.json";
import SearchFieldArgtypes from "./search-field.argtypes";

export default {
component: SearchField,
title: "SearchField",
args: {disabled: false, placeholder: "Placeholder"},
parameters: {
componentSubtitle: (
<ComponentInfo
Expand All @@ -26,6 +26,7 @@ export default {
/>
),
},
argTypes: SearchFieldArgtypes,
} as Meta<typeof SearchField>;

type StoryComponentType = StoryObj<typeof SearchField>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ describe("TextField", () => {
expect(input).toBeInTheDocument();
});

it("className prop is passed to the input element", () => {
// Arrange
const className = "some-class";

// Act
render(
<TextField
id={"tf-1"}
value=""
onChange={() => {}}
className={className}
/>,
);

// Assert
const input = screen.getByRole("textbox");
expect(input).toHaveClass(className);
});

it("disabled prop disables the input element", () => {
// Arrange
render(
Expand Down
4 changes: 4 additions & 0 deletions packages/wonder-blocks-form/src/components/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ type Props = AriaProps & {
* Custom styles for the input.
*/
style?: StyleType;
/**
* Optional CSS classes for the entire dropdown component.
*/
className?: string;
/**
* Optional test ID for e2e testing.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ describe("SearchField", () => {
expect(input).toHaveAttribute("aria-label", "some-aria-label");
});

test("className is passed down", () => {
// Arrange

// Act
render(
<SearchField
value=""
onChange={() => {}}
testId="search-field-test"
className="some-class-name"
/>,
);

// Assert
const input = screen.getByRole("textbox");
expect(input).toHaveClass("some-class-name");
});

test("receives focus on click", () => {
// Arrange
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type Props = AriaProps & {
* Custom styles for the main wrapper.
*/
style?: StyleType;
/**
* Optional CSS classes for the input field.
*/
className?: string;
/**
* Test ID used for e2e testing.
*/
Expand Down

0 comments on commit 3659538

Please sign in to comment.