diff --git a/components/select/src/single-select-a11y/menu/index.js b/components/select/src/single-select-a11y/menu/index.js new file mode 100644 index 0000000000..bf206853e6 --- /dev/null +++ b/components/select/src/single-select-a11y/menu/index.js @@ -0,0 +1 @@ +export { Menu } from './menu.js' diff --git a/components/select/src/single-select-a11y/menu-filter.js b/components/select/src/single-select-a11y/menu/menu-filter.js similarity index 97% rename from components/select/src/single-select-a11y/menu-filter.js rename to components/select/src/single-select-a11y/menu/menu-filter.js index 4cc2181882..a4ec8102bc 100644 --- a/components/select/src/single-select-a11y/menu-filter.js +++ b/components/select/src/single-select-a11y/menu/menu-filter.js @@ -2,7 +2,7 @@ import { colors, spacers } from '@dhis2/ui-constants' import { Input } from '@dhis2-ui/input' import PropTypes from 'prop-types' import React from 'react' -import i18n from '../locales/index.js' +import i18n from '../../locales/index.js' export function MenuFilter({ value, onChange, dataTest, placeholder, label }) { return ( diff --git a/components/select/src/single-select-a11y/menu-loading.js b/components/select/src/single-select-a11y/menu/menu-loading.js similarity index 100% rename from components/select/src/single-select-a11y/menu-loading.js rename to components/select/src/single-select-a11y/menu/menu-loading.js diff --git a/components/select/src/single-select-a11y/menu-options-list.js b/components/select/src/single-select-a11y/menu/menu-options-list.js similarity index 78% rename from components/select/src/single-select-a11y/menu-options-list.js rename to components/select/src/single-select-a11y/menu/menu-options-list.js index 5dae04aca3..e690721469 100644 --- a/components/select/src/single-select-a11y/menu-options-list.js +++ b/components/select/src/single-select-a11y/menu/menu-options-list.js @@ -1,31 +1,32 @@ import PropTypes from 'prop-types' -import React, { useEffect, useRef } from 'react' -import { isOptionHidden } from './is-option-hidden.js' +import React, { forwardRef, useEffect } from 'react' +import { isOptionHidden } from '../is-option-hidden.js' +import { optionProp } from '../shared-prop-types.js' import { Option } from './option.js' -import { optionsProp } from './shared-prop-types.js' - -export function MenuOptionsList({ - comboBoxId, - expanded, - focussedOptionIndex, - idPrefix, - labelledBy, - options, - selected, - dataTest, - disabled, - loading, - onChange, - onBlur, - onKeyDown, -}) { - const listBoxRef = useRef() +export const MenuOptionsList = forwardRef(function MenuOptionsList( + { + comboBoxId, + expanded, + focussedOptionIndex, + idPrefix, + labelledBy, + options, + selected, + dataTest, + disabled, + loading, + onChange, + onBlur, + onKeyDown, + }, + ref +) { // scrolls the highlighted option into view when: // * the highlighted option changes // * the menu opens useEffect(() => { - const { current: listBox } = listBoxRef + const { current: listBox } = ref const highlightedOption = expanded ? listBox.childNodes[focussedOptionIndex] : null @@ -41,11 +42,11 @@ export function MenuOptionsList({ highlightedOption.scrollIntoView() } } - }, [expanded, focussedOptionIndex]) + }, [expanded, focussedOptionIndex, ref]) return (
) -} +}) MenuOptionsList.propTypes = { comboBoxId: PropTypes.string.isRequired, expanded: PropTypes.bool.isRequired, focussedOptionIndex: PropTypes.number.isRequired, idPrefix: PropTypes.string.isRequired, - options: optionsProp.isRequired, + options: PropTypes.arrayOf(optionProp).isRequired, onChange: PropTypes.func.isRequired, dataTest: PropTypes.string, disabled: PropTypes.bool, diff --git a/components/select/src/single-select-a11y/menu.js b/components/select/src/single-select-a11y/menu/menu.js similarity index 91% rename from components/select/src/single-select-a11y/menu.js rename to components/select/src/single-select-a11y/menu/menu.js index 5a4b49064e..aa52c51293 100644 --- a/components/select/src/single-select-a11y/menu.js +++ b/components/select/src/single-select-a11y/menu/menu.js @@ -4,10 +4,10 @@ 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 { MenuFilter } from './menu-filter.js' import { MenuLoading } from './menu-loading.js' import { MenuOptionsList } from './menu-options-list.js' -import { optionsProp } from './shared-prop-types.js' export function Menu({ comboBoxId, @@ -24,6 +24,7 @@ export function Menu({ filterable, hidden, labelledBy, + listBoxRef, loading, loadingText, maxHeight, @@ -66,6 +67,7 @@ export function Menu({ {!options.length &&
{empty}
} (