Drop down without managed focus

managedFocus is disabled. This means the focus remains in the combox box element and does not follow the selected option. Instead aria-activedescendant sets the focused option.

The default is to use managedFocus as this has the best browser and screen-reader compatibility.

import { useRef, useState } from 'react';
import { DropDown } from '@citizensadvice/react-combo-boxes';
import countries from '../../data/countries.json';

function mapOption({ name, code }) {
  return `${name} (${code})`;
}

export function Example() {
  const [value, setValue] = useState(null);
  const ref = useRef();
  const [managedFocus, setManagedFocus] = useState(false);

  return (
    <>
      <div
        className="label"
        onClick={() => ref.current.focus()}
        id="drop-down-label"
      >
        Drop down
      </div>
      <DropDown
        ref={ref}
        id="drop-down"
        aria-labelledby="drop-down-label"
        value={value}
        onValue={setValue}
        options={countries}
        mapOption={mapOption}
        managedFocus={managedFocus}
      />

      <label htmlFor="output">Current value</label>
      <output
        htmlFor="drop-down"
        id="output"
      >
        {JSON.stringify(value, undefined, ' ')}
      </output>

      <label>
        <input
          type="checkbox"
          onChange={({ target: { checked } }) => setManagedFocus(checked)}
          checked={managedFocus}
        />{' '}
        Toggle managed focus
      </label>
    </>
  );
}