Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/components/src/components/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useState, useMemo } from 'react';

import { IconArrowDownS16, IconArrowUpS16 } from '@koobiq/react-icons';
import {
IconArrowDownS16,
IconArrowUpS16,
IconArrowUpArrowDown16,
} from '@koobiq/react-icons';
import type { Meta, StoryObj } from '@storybook/react';

import type {
Expand Down Expand Up @@ -727,9 +731,16 @@ export const RenderSortIcon: Story = {
aria-label="Fast animals"
{...(sortDescriptor && { sortDescriptor })}
onSortChange={handleSortChange}
renderSortIcon={({ direction }) =>
direction === 'ascending' ? <IconArrowUpS16 /> : <IconArrowDownS16 />
}
renderSortIcon={({ direction }) => {
if (!direction)
return <IconArrowUpArrowDown16 style={{ opacity: '0.5' }} />;

return direction === 'ascending' ? (
<IconArrowUpS16 />
) : (
<IconArrowDownS16 />
);
}}
selectionMode="multiple"
{...args}
>
Expand Down
58 changes: 58 additions & 0 deletions packages/components/src/components/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,62 @@ describe('Table', () => {

expect(onNavigate).toBeCalled();
});

it('should pass unsorted state to renderSortIcon for sortable column', () => {
const renderSortIcon = vi.fn(() => <span data-testid="sort-icon" />);

render(
<Table aria-label="Sortable table" renderSortIcon={renderSortIcon}>
<Table.Header>
<Table.Column key="name" allowsSorting>
Name
</Table.Column>
</Table.Header>
<Table.Body items={[{ id: 1, name: 'home' }]}>
{(item) => (
<Table.Row>
<Table.Cell>{item.name}</Table.Cell>
</Table.Row>
)}
</Table.Body>
</Table>
);

expect(renderSortIcon).toBeCalledWith({
direction: undefined,
isActive: false,
});

expect(screen.getByTestId('sort-icon')).toBeInTheDocument();
});

it('should pass active sort direction to renderSortIcon', () => {
const renderSortIcon = vi.fn(() => <span data-testid="sort-icon" />);

render(
<Table
aria-label="Sortable table"
sortDescriptor={{ column: 'name', direction: 'ascending' }}
renderSortIcon={renderSortIcon}
>
<Table.Header>
<Table.Column key="name" allowsSorting>
Name
</Table.Column>
</Table.Header>
<Table.Body items={[{ id: 1, name: 'home' }]}>
{(item) => (
<Table.Row>
<Table.Cell>{item.name}</Table.Cell>
</Table.Row>
)}
</Table.Body>
</Table>
);

expect(renderSortIcon).toBeCalledWith({
direction: 'ascending',
isActive: true,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
.sortIcon {
display: flex;
visibility: hidden;
opacity: 1;
align-items: center;
justify-content: center;
inline-size: var(--kbq-size-l);

&.active {
visibility: visible;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useRef } from 'react';

import { useFocusRing, mergeProps, clsx } from '@koobiq/react-core';
import { useFocusRing, mergeProps, clsx, useHover } from '@koobiq/react-core';
import { IconChevronUpS16, IconChevronDownS16 } from '@koobiq/react-icons';
import type {
TableState,
Expand Down Expand Up @@ -58,18 +58,32 @@ export function TableColumnHeader<T>({
}: ColumnProps<T> = column.props;

const { isFocusVisible, focusProps } = useFocusRing();
const { isHovered, hoverProps } = useHover({ isDisabled: !allowsSorting });

const isActive = state.sortDescriptor?.column === column.key;

const direction = isActive ? state.sortDescriptor?.direction : undefined;

const defaultIcon =
direction === 'ascending' ? <IconChevronUpS16 /> : <IconChevronDownS16 />;
const defaultIcon = () => {
if (direction === 'ascending') {
return <IconChevronUpS16 />;
}

const iconToRender = renderSortIcon?.({ direction, isActive }) ?? defaultIcon;
if (direction === 'descending') {
return <IconChevronDownS16 />;
}

return null;
};

const iconToRender =
renderSortIcon?.({ direction, isActive }) ?? defaultIcon();

const columnSortIcon = allowsSorting && (
<span aria-hidden="true" className={clsx(s.sortIcon, isActive && s.active)}>
<span
aria-hidden="true"
className={clsx(s.sortIcon, (isActive || isHovered) && s.active)}
>
{iconToRender}
</span>
);
Expand All @@ -91,7 +105,7 @@ export function TableColumnHeader<T>({
data-valign={valign || undefined}
data-allows-sorting={allowsSorting || undefined}
data-allows-resizing={isResizable || undefined}
{...mergeProps(columnHeaderProps, focusProps)}
{...mergeProps(columnHeaderProps, hoverProps, focusProps)}
style={{
...styleProp,
inlineSize: layoutState?.getColumnWidth(column.key),
Expand Down