Skip to content
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

feat(Chip): accessibility improvements #1471

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions collections/forms/i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-07-17T14:10:05.852Z\n"
"PO-Revision-Date: 2024-07-17T14:10:05.853Z\n"
"POT-Creation-Date: 2024-09-02T18:17:51.871Z\n"
"PO-Revision-Date: 2024-09-02T18:17:51.873Z\n"

msgid "Upload file"
msgstr "Upload file"
Expand Down
18 changes: 18 additions & 0 deletions components/chip/API.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
### ChipGroup

#### Usage

**Note**: If possible, import the component from the main UI (`@dhis2/ui`) package.

```js
import { ChipGroup } from '@dhis2-ui/chip'
```

#### Props

|Name|Type|Default|Required|Description|
|---|---|---|---|---|
|children|node|||The Chip components to be rendered within the group|
|className|string|||Additional CSS class for the chip group|
|dataTest|string|`'dhis2-uicore-chipgroup'`||Data test id for testing purposes|

### Chip

#### Usage
Expand Down
106 changes: 106 additions & 0 deletions components/chip/src/chip-group/chip-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { theme } from '@dhis2/ui-constants'
import PropTypes from 'prop-types'
import React, { useRef, useEffect, useState } from 'react'

const ChipGroup = ({ className, dataTest, children }) => {
const chipContainer = useRef(null)
const [childrenToFocus, setChildrenToFocus] = useState([])

useEffect(() => {
if (chipContainer.current) {
const chips =
chipContainer.current.querySelectorAll('[role="option"]')
if (chips.length > 0) {
const childrenToFocus = Array.from(chips)
setChildrenToFocus(childrenToFocus)
}
}
}, [children])

const handleKeyDown = (event) => {
if (!childrenToFocus.length) {
return
}

const currentFocus = document.activeElement

const currentIndex = childrenToFocus.findIndex(
(element) => element === currentFocus
)

if (currentIndex === -1) {
return
}

if (event.key === 'ArrowRight') {
event.preventDefault()
const nextIndex = (currentIndex + 1) % childrenToFocus.length
childrenToFocus[nextIndex].focus()
}

if (event.key === 'ArrowLeft') {
event.preventDefault()
const prevIndex =
(currentIndex - 1 + childrenToFocus.length) %
childrenToFocus.length
childrenToFocus[prevIndex].focus()
}

if (event.key === 'Backspace' || event.key === 'Delete') {
event.preventDefault()
event.stopPropagation()
const nextIndex = (currentIndex + 1) % childrenToFocus.length
childrenToFocus[nextIndex].focus()
}
}

const handleFocus = (event) => {
if (!childrenToFocus.length) {
return
}
if (event.target === chipContainer.current) {
const selectedChild = childrenToFocus.find(
(child) => child.getAttribute('aria-selected') === 'true'
)
if (!selectedChild && childrenToFocus[0]) {
childrenToFocus[0].focus()
}
selectedChild.focus()
}
}

return (
<div
className={className}
data-test={dataTest}
onKeyDown={handleKeyDown}
onFocus={handleFocus}
ref={chipContainer}
tabIndex={0}
role="listbox"
>
{children}
<style jsx>{`
div {
display: flex;
gap: 4px;
}
div:focus {
outline: 1px solid ${theme.focus};
}
`}</style>
</div>
)
}

ChipGroup.defaultProps = {
dataTest: 'dhis2-uicore-chipgroup',
}

ChipGroup.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
dataTest: PropTypes.string,
}

export { ChipGroup }
1 change: 1 addition & 0 deletions components/chip/src/chip-group/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ChipGroup } from './chip-group.js'
149 changes: 0 additions & 149 deletions components/chip/src/chip.js

This file was deleted.

Loading
Loading