From f7146dd504a7519b60d7d31ee1aadc5efaebc0f5 Mon Sep 17 00:00:00 2001 From: Andrey Morozov Date: Fri, 22 Dec 2023 18:13:14 +0300 Subject: [PATCH] feat(Table)!: update SortIndicator view --- .../SortIndicator/SortIndicator.scss | 8 ++---- .../SortIndicator/SortIndicator.tsx | 19 +++----------- .../withTableSorting/withTableSorting.scss | 9 +++---- .../hoc/withTableSorting/withTableSorting.tsx | 10 ++++++-- .../useActionHandlers/useActionHandlers.ts | 25 ++++++++++--------- 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.scss b/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.scss index 35f0a09fd3..f6833f1cf6 100644 --- a/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.scss +++ b/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.scss @@ -3,11 +3,7 @@ $block: '.#{variables.$ns}sort-indicator'; #{$block} { - &__caret { - padding: 1px 0; - - & > svg { - display: block; - } + &__icon { + vertical-align: -2px; } } diff --git a/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.tsx b/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.tsx index 31414cea5a..8da3bc9942 100644 --- a/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.tsx +++ b/src/components/Table/hoc/withTableSorting/SortIndicator/SortIndicator.tsx @@ -1,7 +1,9 @@ import React from 'react'; +import {ArrowDown, ArrowUp} from '@gravity-ui/icons'; + +import {Icon} from '../../../../Icon'; import {block} from '../../../../utils/cn'; -import {a11yHiddenSvgProps} from '../../../../utils/svg'; import './SortIndicator.scss'; @@ -14,20 +16,7 @@ const b = block('sort-indicator'); export function SortIndicator({order = 'asc'}: SortIndicatorProps) { return (
-
- - - -
+
); } diff --git a/src/components/Table/hoc/withTableSorting/withTableSorting.scss b/src/components/Table/hoc/withTableSorting/withTableSorting.scss index 2490c29c23..2180ac304b 100644 --- a/src/components/Table/hoc/withTableSorting/withTableSorting.scss +++ b/src/components/Table/hoc/withTableSorting/withTableSorting.scss @@ -5,11 +5,12 @@ $block: '.#{variables.$ns}table'; #{$block} { &__sort { display: inline-flex; - align-items: center; + align-items: baseline; // `top` to avoid redundant height to appear vertical-align: top; cursor: pointer; user-select: none; + border-radius: var(--g-border-radius-xs); &-spacer { width: 5px; @@ -17,16 +18,14 @@ $block: '.#{variables.$ns}table'; &-indicator { color: var(--g-color-text-hint); - opacity: 0; } &_active &-indicator { color: var(--g-color-text-primary); - opacity: 1; } - &:hover &-indicator { - opacity: 1; + &:focus-visible { + outline: 2px solid var(--g-color-line-focus); } } } diff --git a/src/components/Table/hoc/withTableSorting/withTableSorting.tsx b/src/components/Table/hoc/withTableSorting/withTableSorting.tsx index 51186974ca..45ede3454a 100644 --- a/src/components/Table/hoc/withTableSorting/withTableSorting.tsx +++ b/src/components/Table/hoc/withTableSorting/withTableSorting.tsx @@ -2,6 +2,7 @@ import React from 'react'; import _memoize from 'lodash/memoize'; +import {createOnKeyDownHandler} from '../../../../hooks/useActionHandlers/useActionHandlers'; import {block} from '../../../utils/cn'; import {getComponentName} from '../../../utils/getComponentName'; import {Table} from '../../Table'; @@ -147,11 +148,16 @@ export function withTableSorting( content.reverse(); } + const onClick = this.handleColumnSortClick.bind(this, column); + const onKeyDown = createOnKeyDownHandler(onClick); + return ( - // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
{content}
diff --git a/src/hooks/useActionHandlers/useActionHandlers.ts b/src/hooks/useActionHandlers/useActionHandlers.ts index 73309bd540..9a6e37c14e 100644 --- a/src/hooks/useActionHandlers/useActionHandlers.ts +++ b/src/hooks/useActionHandlers/useActionHandlers.ts @@ -11,6 +11,18 @@ export interface UseActionHandlersResult { onKeyDown: React.KeyboardEventHandler; } +export function createOnKeyDownHandler(callback?: AnyFunction) { + return (event: React.KeyboardEvent) => { + if ( + callback && + [KeyCode.ENTER, KeyCode.SPACEBAR, KeyCode.SPACEBAR_OLD].includes(event.key) + ) { + // eslint-disable-next-line callback-return + callback(event); + } + }; +} + /** * Emulates behaviour of system controls, that respond to Enter and Spacebar * @param callback @@ -19,18 +31,7 @@ export interface UseActionHandlersResult { export function useActionHandlers( callback?: UseActionHandlersProps, ): UseActionHandlersResult { - const onKeyDown = React.useCallback( - (event: React.KeyboardEvent) => { - if ( - callback && - [KeyCode.ENTER, KeyCode.SPACEBAR, KeyCode.SPACEBAR_OLD].includes(event.key) - ) { - // eslint-disable-next-line callback-return - callback(event); - } - }, - [callback], - ); + const onKeyDown = React.useMemo(() => createOnKeyDownHandler(callback), [callback]); return {onKeyDown}; }