Ott 833 user acc select country (#304)

* fix(817): a11y fix

* fix(833): country selection in user account
keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
Mirlan 5 years ago committed by GitHub
parent b33773cd94
commit ff4ab7866f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      src/features/Combobox/hooks/index.tsx
  2. 2
      src/features/Combobox/index.tsx
  3. 5
      src/features/Combobox/styled.tsx
  4. 10
      src/features/UserAccount/components/UserSubscriptionsList/styled.tsx

@ -1,4 +1,9 @@
import type { ChangeEvent, KeyboardEvent } from 'react' import type {
FocusEvent,
ChangeEvent,
KeyboardEvent,
BaseSyntheticEvent,
} from 'react'
import { import {
useState, useState,
useCallback, useCallback,
@ -10,7 +15,6 @@ import toLower from 'lodash/toLower'
import find from 'lodash/find' import find from 'lodash/find'
import trim from 'lodash/trim' import trim from 'lodash/trim'
import size from 'lodash/size' import size from 'lodash/size'
import isEmpty from 'lodash/isEmpty'
import { useToggle } from 'hooks' import { useToggle } from 'hooks'
@ -18,6 +22,10 @@ import type { Props, Option } from '../types'
import { matchSort } from '../helpers' import { matchSort } from '../helpers'
import { useKeyboardScroll } from './useKeyboardScroll' import { useKeyboardScroll } from './useKeyboardScroll'
const isOptionClicked = (target: HTMLElement | null) => (
target?.getAttribute('role') === 'option'
)
const useQuery = <T extends Option>({ onChange, value }: Props<T>) => { const useQuery = <T extends Option>({ onChange, value }: Props<T>) => {
const [query, setQuery] = useState('') const [query, setQuery] = useState('')
@ -72,7 +80,8 @@ export const useCombobox = <T extends Option>(props: Props<T>) => {
) || null ) || null
), [options]) ), [options])
const onOptionSelect = useCallback((option: string) => { const onOptionSelect = useCallback((option: string, e?: BaseSyntheticEvent) => {
e?.stopPropagation()
const selectedOption = findOptionByName(option) const selectedOption = findOptionByName(option)
setQuery(selectedOption?.name || '') setQuery(selectedOption?.name || '')
onSelect?.(selectedOption) onSelect?.(selectedOption)
@ -85,20 +94,18 @@ export const useCombobox = <T extends Option>(props: Props<T>) => {
]) ])
const onOutsideClick = (event: MouseEvent) => { const onOutsideClick = (event: MouseEvent) => {
if (event.target !== inputFieldRef.current as HTMLInputElement) { if (event.target !== inputFieldRef.current) {
close() onOptionSelect(query)
if (results[0]?.name.includes(query) && query) {
onOptionSelect(results[0].name)
} else {
onOptionSelect(query)
}
} }
} }
const onInputBlur = () => { const onInputBlur = (event: FocusEvent<HTMLInputElement>) => {
if (isEmpty(results)) { const target = event.relatedTarget as HTMLElement | null
onOptionSelect('') // клик по элементу списка тоже вызывает onBlur
} // если кликали элемент списка то событие обрабатывает onOptionSelect
if (isOptionClicked(target)) return
onOptionSelect(query)
} }
useEffect(() => { useEffect(() => {

@ -94,7 +94,7 @@ export const Combobox = <T extends Option>(props: Props<T>) => {
> >
{map(options, (option, i) => ( {map(options, (option, i) => (
<ListOption <ListOption
onClick={() => onOptionSelect(option.name)} onClick={(e) => onOptionSelect(option.name, e)}
aria-selected={index === i} aria-selected={index === i}
isHighlighted={index === i} isHighlighted={index === i}
key={option.id} key={option.id}

@ -22,7 +22,10 @@ export const PopOver = styled.ul`
${customStylesMixin}; ${customStylesMixin};
` `
export const ListOption = styled.li<{isHighlighted?: boolean}>` export const ListOption = styled.li.attrs(() => ({
role: 'option',
tabIndex: 0,
}))<{isHighlighted?: boolean}>`
width: 100%; width: 100%;
height: 48px; height: 48px;
font-size: 16px; font-size: 16px;

@ -58,7 +58,9 @@ export const ActionsWrapper = styled.div`
} }
` `
export const Subscription = styled.div` export const Subscription = styled.div.attrs(() => ({
tabIndex: 0,
}))`
width: 800px; width: 800px;
height: 70px; height: 70px;
display: flex; display: flex;
@ -70,8 +72,10 @@ export const Subscription = styled.div`
border-radius: 2px; border-radius: 2px;
overflow: hidden; overflow: hidden;
:hover ${ActionsWrapper} { :focus-within, :hover {
transform: translateX(0); ${ActionsWrapper} {
transform: translateX(0);
}
} }
` `

Loading…
Cancel
Save