-
-
Notifications
You must be signed in to change notification settings - Fork 486
fix: improve category select accessibility #4836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Atmuri-SatyaPrakash
wants to merge
5
commits into
ONEARMY:master
from
Atmuri-SatyaPrakash:fix/4827-category-select-accessibility
+114
−9
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af0ff39
fix: improve category select accessibility
Atmuri-SatyaPrakash 374572b
fix: preserve category list test selector
Atmuri-SatyaPrakash 2049969
fix: preserve category item test selector
Atmuri-SatyaPrakash 9453d6b
Merge branch 'master' into fix/4827-category-select-accessibility
mariojsnunes 1946c04
Merge branch 'master' into fix/4827-category-select-accessibility
mariojsnunes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { useState } from "react"; | ||
| import type { Category, ContentType } from "oa-shared"; | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { CategoryHorizontalList } from "./category-horizontal-list"; | ||
|
|
||
| const categories: Category[] = [ | ||
| { | ||
| id: 1, | ||
| createdAt: new Date("2022-12-01T18:03:51.313Z"), | ||
| modifiedAt: null, | ||
| name: "Machines", | ||
| type: "questions" as ContentType, | ||
| imageUrl: null, | ||
| description: "Machine projects", | ||
| }, | ||
| { | ||
| id: 2, | ||
| createdAt: new Date("2022-12-03T18:03:51.313Z"), | ||
| modifiedAt: null, | ||
| name: "Moulds", | ||
| type: "questions" as ContentType, | ||
| imageUrl: null, | ||
| description: null, | ||
| }, | ||
| { | ||
| id: 3, | ||
| createdAt: new Date("2022-12-04T18:03:51.313Z"), | ||
| modifiedAt: null, | ||
| name: "Recycling", | ||
| type: "questions" as ContentType, | ||
| imageUrl: null, | ||
| description: null, | ||
| }, | ||
| ]; | ||
|
|
||
| const meta: Meta<typeof CategoryHorizontalList> = { | ||
| title: "ui/CategoryHorizontalList", | ||
| component: CategoryHorizontalList, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof CategoryHorizontalList>; | ||
|
|
||
| export const Default: Story = { | ||
| render: () => { | ||
| const [activeCategory, setActiveCategory] = useState<Category | null>(null); | ||
|
|
||
| return ( | ||
| <CategoryHorizontalList | ||
| activeCategory={activeCategory} | ||
| allCategories={categories} | ||
| setActiveCategory={setActiveCategory} | ||
| /> | ||
| ); | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import type { Category } from 'oa-shared'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| interface CategoryHorizontalListProps { | ||
| activeCategory: Category | null; | ||
| allCategories: Category[]; | ||
| setActiveCategory: (category: Category | null) => void; | ||
| } | ||
|
|
||
| export function CategoryHorizontalList({ | ||
| activeCategory, | ||
| allCategories, | ||
| setActiveCategory, | ||
| }: CategoryHorizontalListProps) { | ||
| if (!allCategories?.length) { | ||
| return null; | ||
| } | ||
|
|
||
| const orderedCategories = allCategories | ||
| .slice() | ||
| .sort((a, b) => (a.createdAt > b.createdAt ? 1 : -1)); | ||
|
|
||
| return ( | ||
| <div data-cy="CategoryHorizonalList" className="flex overflow-x-auto"> | ||
| {orderedCategories.map((category) => { | ||
| const isSelected = category.id === activeCategory?.id; | ||
|
|
||
| return ( | ||
| <Button | ||
| key={category.id} | ||
| type="button" | ||
| variant="ghost" | ||
| aria-pressed={isSelected} | ||
| data-cy={`CategoryHorizonalList-Item${isSelected ? '-active' : ''}`} | ||
| data-testid="CategoryHorizonalList-Item" | ||
| title={category.name} | ||
| onClick={() => setActiveCategory(isSelected ? null : category)} | ||
| className={cn( | ||
| 'mx-1 flex h-auto min-w-20 flex-col items-center justify-center rounded-lg border-2 py-2 text-center md:min-w-[100px] lg:min-w-[130px]', | ||
| isSelected ? 'border-foreground' : 'border-transparent', | ||
| )} | ||
| > | ||
| {category.imageUrl && ( | ||
| <img src={category.imageUrl} alt="" className="size-10 object-contain" /> | ||
| )} | ||
|
|
||
| <span className="text-sm">{category.name}</span> | ||
| </Button> | ||
| ); | ||
| })} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.