Skip to content

Commit

Permalink
feat(CountryFlag): add role prop for accessibility customization
Browse files Browse the repository at this point in the history
Co-Authored-By: Jozef Képesi <[email protected]>
  • Loading branch information
2 people authored and mvidalgarcia committed Dec 19, 2024
1 parent 1bf0bb9 commit 96d57b0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const meta: Meta<typeof CountryFlag> = {
name: "Country",
code: CODES.ANYWHERE,
size: SIZES.SMALL,
role: "img",
},

argTypes: {
Expand All @@ -31,6 +32,12 @@ const meta: Meta<typeof CountryFlag> = {
type: "select",
},
},
role: {
options: ["img", "presentation", "none"],
control: {
type: "select",
},
},
},
};

Expand Down
15 changes: 8 additions & 7 deletions packages/orbit-components/src/CountryFlag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ After adding import into your project you can use it simply like:

Table below contains all types of the props available in CountryFlag component.

| Name | Type | Default | Description |
| :------- | :-------------- | :------------ | :----------------------------------- |
| code | [`enum`](#enum) | `"undefined"` | Code for the displayed country flag. |
| dataTest | `string` | | Optional prop for testing purposes. |
| id | `string` | | Set `id` for `CountryFlag` |
| name | `string` | | The name for the flag. |
| size | [`enum`](#enum) | `"medium"` | The size of the CountryFlag. |
| Name | Type | Default | Description |
| :------- | :----------------------------------------------- | :------------ | :----------------------------------- |
| code | [`enum`](#enum) | `"undefined"` | Code for the displayed country flag. |
| dataTest | `string` | | Optional prop for testing purposes. |
| id | `string` | | Set `id` for `CountryFlag` |
| name | `string` | | The name for the flag. |
| role | `React.HTMLAttributes<HTMLImageElement>["role"]` | `"img"` | ARIA role for the flag image. |
| size | [`enum`](#enum) | `"medium"` | The size of the CountryFlag. |

### enum

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ describe("CountryFlag", () => {
);
expect(wrapper).toHaveStyle({ width: "16px", height: "9px" });
});

it("should support custom role", () => {
render(<CountryFlag code="us" name="United States" role="presentation" />);
expect(screen.getByRole("presentation")).toBeInTheDocument();
});
});
3 changes: 2 additions & 1 deletion packages/orbit-components/src/CountryFlag/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getCountryProps(code?: string, name?: string) {
return { code: countryCode, name: countryName };
}

const CountryFlag = ({ dataTest, size = SIZES.MEDIUM, id, ...props }: Props) => {
const CountryFlag = ({ dataTest, size = SIZES.MEDIUM, id, role = "img", ...props }: Props) => {
const { code, name } = getCountryProps(props.code, props.name);

const width = SIZE_WIDTHS[size];
Expand All @@ -40,6 +40,7 @@ const CountryFlag = ({ dataTest, size = SIZES.MEDIUM, id, ...props }: Props) =>
data-test={dataTest}
src={src}
srcSet={srcSet}
role={role}
/>
<div className="rounded-50 shadow-country-flag absolute inset-0 block size-full" />
</div>
Expand Down
3 changes: 3 additions & 0 deletions packages/orbit-components/src/CountryFlag/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Type definitions for @kiwicom/orbit-components
// Project: http://github.com/kiwicom/orbit

import type * as React from "react";

import type * as Common from "../common/types";

export type Size = "small" | "medium";
Expand All @@ -9,4 +11,5 @@ export interface Props extends Common.Globals {
readonly code?: string;
readonly name?: string;
readonly size?: Size;
readonly role?: React.HTMLAttributes<HTMLImageElement>["role"];
}

0 comments on commit 96d57b0

Please sign in to comment.