Skip to content

Commit

Permalink
feat(search): add searchButtonAriaLabel prop to component
Browse files Browse the repository at this point in the history
New prop `searchButtonAriaLabel` prop has created so that consumers can add their own aria-label to
the Search component when rendered with a button.
  • Loading branch information
DipperTheDan committed Jun 15, 2023
1 parent 70bcb35 commit 5de15f0
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 32 deletions.
17 changes: 17 additions & 0 deletions cypress/components/search/search.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ context("Test for Search component", () => {
searchDefaultInput().should("have.attr", "aria-label", testCypress);
});

it("should render Search button with aria-label prop", () => {
CypressMountWithProviders(
<SearchComponent searchButton searchButtonAriaLabel={testCypress} />
);

searchDefaultInput().clear().type(testCypress);
searchButton().should("have.attr", "aria-label", testCypress);
});

it.each([
[true, "be.visible"],
[false, "not.exist"],
Expand Down Expand Up @@ -434,6 +443,14 @@ context("Test for Search component", () => {
cy.checkAccessibility();
});

it("should check accessibility for searchButton with searchButtonAriaLabel", () => {
CypressMountWithProviders(
<SearchComponent searchButton searchButtonAriaLabel={testCypress} />
);

cy.checkAccessibility();
});

it.each(["34%", "70%"])(
"should check accessibility for Search with searchWidth prop set to %s",
(widthInPercentage) => {
Expand Down
10 changes: 9 additions & 1 deletion src/components/search/search.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface SearchEvent {
export interface SearchProps extends ValidationProps, MarginProps {
/** Prop to specify the aria-label of the search component */
"aria-label"?: string;
/** Prop to specify the aria-label of the search button */
searchButtonAriaLabel?: string;
/** Prop for `uncontrolled` use */
defaultValue?: string;
/** Prop for `id` */
Expand Down Expand Up @@ -87,6 +89,7 @@ export const Search = React.forwardRef(
searchWidth,
maxWidth,
searchButton,
searchButtonAriaLabel = "search button",
placeholder,
variant = "default",
"aria-label": ariaLabel = "search",
Expand Down Expand Up @@ -286,7 +289,12 @@ export const Search = React.forwardRef(
{searchButton && (
<StyledSearchButton>
{isSearchTyped && (
<Button size="medium" px="16px" {...buttonProps}>
<Button
aria-label={searchButtonAriaLabel}
size="medium"
px="16px"
{...buttonProps}
>
<StyledButtonIcon>
<Icon type="search" />
</StyledButtonIcon>
Expand Down
90 changes: 60 additions & 30 deletions src/components/search/search.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,39 @@ describe("Search", () => {
{ modifier: `${StyledIcon}:hover` }
);
});

it("renders with the expected border radius styling", () => {
wrapper = renderSearch({ value: "" });
assertStyleMatch(
{
borderRadius: "var(--borderRadius050)",
},
wrapper.find(StyledInput)
);
});

it("renders with the expected border radius styling when searchButton is enabled and input has value", () => {
wrapper = renderSearch({ value: "foo", searchButton: true });
assertStyleMatch(
{
borderTopRightRadius: "var(--borderRadius000)",
borderBottomRightRadius: "var(--borderRadius000)",
},
wrapper,
{ modifier: `${StyledTextInput}` }
);

assertStyleMatch(
{
borderTopLeftRadius: "var(--borderRadius000)",
borderBottomLeftRadius: "var(--borderRadius000)",
borderTopRightRadius: "var(--borderRadius050)",
borderBottomRightRadius: "var(--borderRadius050)",
},
wrapper.find(StyledSearchButton),
{ modifier: `& ${StyledButton}` }
);
});
});

describe("When button is true and textbox is active", () => {
Expand Down Expand Up @@ -677,36 +710,33 @@ describe("Search", () => {
});
});

it("renders with the expected border radius styling", () => {
wrapper = renderSearch({ value: "" });
assertStyleMatch(
{
borderRadius: "var(--borderRadius050)",
},
wrapper.find(StyledInput)
);
});
describe("aria-label", () => {
it("has a default aria-label passed to Search", () => {
wrapper = renderSearch({ defaultValue: "foo" });
const search = wrapper.find(TextBox);
expect(search.prop("aria-label")).toEqual("search");
});

it("renders with the expected border radius styling when searchButton is enabled and input has value", () => {
wrapper = renderSearch({ value: "foo", searchButton: true });
assertStyleMatch(
{
borderTopRightRadius: "var(--borderRadius000)",
borderBottomRightRadius: "var(--borderRadius000)",
},
wrapper,
{ modifier: `${StyledTextInput}` }
);

assertStyleMatch(
{
borderTopLeftRadius: "var(--borderRadius000)",
borderBottomLeftRadius: "var(--borderRadius000)",
borderTopRightRadius: "var(--borderRadius050)",
borderBottomRightRadius: "var(--borderRadius050)",
},
wrapper.find(StyledSearchButton),
{ modifier: `& ${StyledButton}` }
);
it("has a default aria-label passed to Search button", () => {
wrapper = renderSearch({ defaultValue: "foo", searchButton: true });
const searchButton = wrapper.find(Button);
expect(searchButton.prop("aria-label")).toEqual("search button");
});

it("supports a custom aria-label passed to Search", () => {
wrapper = renderSearch({ defaultValue: "foo", "aria-label": "foobar" });
const search = wrapper.find(Search);
expect(search.prop("aria-label")).toEqual("foobar");
});

it("supports a custom aria-label passed to Search button", () => {
wrapper = renderSearch({
defaultValue: "foo",
searchButtonAriaLabel: "foobar button",
searchButton: true,
});
const searchButton = wrapper.find(Button);
expect(searchButton.prop("aria-label")).toEqual("foobar button");
});
});
});
2 changes: 2 additions & 0 deletions src/components/search/search.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import Search from "carbon-react/lib/components/search";

### Search with Search Button and default width

Please note, if you need to apply your own custom aria-label for the search button, this can be done by using the `searchButtonAriaLabel` prop. By default the aria-label is `search button`.

<Canvas>
<Story name="with SearchButton" story={stories.WithSearchButton} />
</Canvas>
Expand Down
8 changes: 7 additions & 1 deletion src/components/search/search.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export const Controlled: ComponentStory<typeof Search> = () => {
};

export const WithSearchButton: ComponentStory<typeof Search> = () => {
return <Search defaultValue="Here is some text" searchButton />;
return (
<Search
defaultValue="Here is some text"
searchButton
searchButtonAriaLabel="search button aria label"
/>
);
};

export const DefaultWithColourBackground: ComponentStory<
Expand Down

0 comments on commit 5de15f0

Please sign in to comment.