From 76194469fc5a34c18489f0c4087ebcf78f6fb433 Mon Sep 17 00:00:00 2001 From: Mohammer5 Date: Mon, 25 Nov 2024 21:42:44 +0800 Subject: [PATCH] fix(select a11y): handle remaining TODO comments & rename internal components --- .../__stories__/WithoutOptionsAndLoading.js | 28 ++++++ .../__stories__/WithoutSelection.js | 1 + .../menu/{menu-filter.js => filter.js} | 4 +- .../menu/{menu-loading.js => loading.js} | 4 +- .../src/single-select-a11y/menu/menu.js | 35 +++----- .../{menu-options-list.js => options-list.js} | 12 +-- .../selected-value/clear-button.js | 90 +++++++++++++++++++ ...lected-value-container.js => container.js} | 4 +- ...ed-value-placeholder.js => placeholder.js} | 12 +-- .../{selected-value-prefix.js => prefix.js} | 4 +- .../selected-value-clear-button.js | 86 ------------------ .../selected-value/selected-value.js | 27 +++--- .../single-select-a11y/single-select-a11y.js | 11 ++- .../single-select-a11y.prod.stories.js | 1 + .../use-handle-key-press/index.js | 2 +- ...js => use-handle-key-press-on-combobox.js} | 2 +- 16 files changed, 167 insertions(+), 156 deletions(-) create mode 100644 components/select/src/single-select-a11y/__stories__/WithoutOptionsAndLoading.js rename components/select/src/single-select-a11y/menu/{menu-filter.js => filter.js} (96%) rename components/select/src/single-select-a11y/menu/{menu-loading.js => loading.js} (93%) rename components/select/src/single-select-a11y/menu/{menu-options-list.js => options-list.js} (90%) create mode 100644 components/select/src/single-select-a11y/selected-value/clear-button.js rename components/select/src/single-select-a11y/selected-value/{selected-value-container.js => container.js} (97%) rename components/select/src/single-select-a11y/selected-value/{selected-value-placeholder.js => placeholder.js} (73%) rename components/select/src/single-select-a11y/selected-value/{selected-value-prefix.js => prefix.js} (85%) delete mode 100644 components/select/src/single-select-a11y/selected-value/selected-value-clear-button.js rename components/select/src/single-select-a11y/use-handle-key-press/{use-handle-key-press.js => use-handle-key-press-on-combobox.js} (99%) diff --git a/components/select/src/single-select-a11y/__stories__/WithoutOptionsAndLoading.js b/components/select/src/single-select-a11y/__stories__/WithoutOptionsAndLoading.js new file mode 100644 index 000000000..1b8236cdf --- /dev/null +++ b/components/select/src/single-select-a11y/__stories__/WithoutOptionsAndLoading.js @@ -0,0 +1,28 @@ +import React, { useState } from 'react' +import { SingleSelectA11y } from '../single-select-a11y.js' + +const options = [ + { label: 'None', value: '' }, + { value: '1', label: 'Option 1' }, + { value: '2', label: 'Option 2' }, + { value: '3', label: 'Option 3' }, +] + +export const WithoutOptionsAndLoading = () => { + const [value, setValue] = useState('') + + return ( + option.value === value).label + : '' + } + onChange={(nextValue) => setValue(nextValue)} + options={[]} + /> + ) +} diff --git a/components/select/src/single-select-a11y/__stories__/WithoutSelection.js b/components/select/src/single-select-a11y/__stories__/WithoutSelection.js index aa17177f9..abcf0fe83 100644 --- a/components/select/src/single-select-a11y/__stories__/WithoutSelection.js +++ b/components/select/src/single-select-a11y/__stories__/WithoutSelection.js @@ -7,6 +7,7 @@ export const WithoutSelection = () => { return ( setValue(nextValue)} diff --git a/components/select/src/single-select-a11y/menu/menu-filter.js b/components/select/src/single-select-a11y/menu/filter.js similarity index 96% rename from components/select/src/single-select-a11y/menu/menu-filter.js rename to components/select/src/single-select-a11y/menu/filter.js index 828ed0ec5..a16ba3c6b 100644 --- a/components/select/src/single-select-a11y/menu/menu-filter.js +++ b/components/select/src/single-select-a11y/menu/filter.js @@ -4,7 +4,7 @@ import PropTypes from 'prop-types' import React from 'react' import i18n from '../../locales/index.js' -export function MenuFilter({ +export function Filter({ dataTest, idPrefix, label, @@ -46,7 +46,7 @@ export function MenuFilter({ ) } -MenuFilter.propTypes = { +Filter.propTypes = { idPrefix: PropTypes.string.isRequired, value: PropTypes.string.isRequired, onChange: PropTypes.func.isRequired, diff --git a/components/select/src/single-select-a11y/menu/menu-loading.js b/components/select/src/single-select-a11y/menu/loading.js similarity index 93% rename from components/select/src/single-select-a11y/menu/menu-loading.js rename to components/select/src/single-select-a11y/menu/loading.js index 936db6391..fc57e0a0f 100644 --- a/components/select/src/single-select-a11y/menu/menu-loading.js +++ b/components/select/src/single-select-a11y/menu/loading.js @@ -3,7 +3,7 @@ import { CircularLoader } from '@dhis2-ui/loader' import PropTypes from 'prop-types' import React from 'react' -export function MenuLoading({ message }) { +export function Loading({ message }) { return (
@@ -33,6 +33,6 @@ export function MenuLoading({ message }) { ) } -MenuLoading.propTypes = { +Loading.propTypes = { message: PropTypes.string, } diff --git a/components/select/src/single-select-a11y/menu/menu.js b/components/select/src/single-select-a11y/menu/menu.js index 9d2506b28..26b751f23 100644 --- a/components/select/src/single-select-a11y/menu/menu.js +++ b/components/select/src/single-select-a11y/menu/menu.js @@ -1,15 +1,14 @@ import { colors, elevations } from '@dhis2/ui-constants' import { Layer } from '@dhis2-ui/layer' import { Popper } from '@dhis2-ui/popper' -import cx from 'classnames' import PropTypes from 'prop-types' import React, { useEffect, useState } from 'react' import { optionProp } from '../shared-prop-types.js' import { Empty } from './empty.js' -import { MenuFilter } from './menu-filter.js' -import { MenuLoading } from './menu-loading.js' -import { MenuOptionsList } from './menu-options-list.js' +import { Filter } from './filter.js' +import { Loading } from './loading.js' import { NoMatch } from './no-match.js' +import { OptionsList } from './options-list.js' export function Menu({ comboBoxId, @@ -42,12 +41,12 @@ export function Menu({ onFilterChange, onFilterInputKeyDown, }) { - const [menuWidth, setMenuWidth] = useState('auto') + const [menuWidth, setWidth] = useState('auto') const dataTestPrefix = `${dataTest}-menu` useEffect(() => { if (selectRef) { - const callback = () => setMenuWidth(`${selectRef.offsetWidth}px`) + const callback = () => setWidth(`${selectRef.offsetWidth}px`) callback() // We want to know the width as soon as the selectRef.addEventListener('resize', callback) @@ -75,13 +74,10 @@ export function Menu({ placement="bottom-start" observeReferenceResize > -