From 4ef7f997c505e39d56bfca84f663c2a58349616e 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 + .../single-select-a11y/__stories__/options.js | 2 +- .../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 +++---- ...-value-clear-button.js => clear-button.js} | 8 +++-- ...lected-value-container.js => container.js} | 4 +-- ...ed-value-placeholder.js => placeholder.js} | 8 ++--- .../{selected-value-prefix.js => prefix.js} | 4 +-- .../selected-value/selected-value.js | 25 ++++++------- .../single-select-a11y/single-select-a11y.js | 7 ++++ .../single-select-a11y.prod.stories.js | 1 + 14 files changed, 78 insertions(+), 65 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%) rename components/select/src/single-select-a11y/selected-value/{selected-value-clear-button.js => clear-button.js} (91%) 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} (80%) rename components/select/src/single-select-a11y/selected-value/{selected-value-prefix.js => prefix.js} (85%) 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/__stories__/options.js b/components/select/src/single-select-a11y/__stories__/options.js index bcbde0f18..20b185602 100644 --- a/components/select/src/single-select-a11y/__stories__/options.js +++ b/components/select/src/single-select-a11y/__stories__/options.js @@ -1,6 +1,6 @@ export const options = [ { - label: 'None', + label: 'None foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz None foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz', value: '', }, { 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 > -