Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@clayui/core): updates the sort icon for table #5736

Merged
merged 11 commits into from
Jan 19, 2024
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
25 changes: 17 additions & 8 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import '@clayui/css/lib/css/atlas.css';
const spritemap = require('@clayui/css/lib/images/icons/icons.svg');
import React from 'react';
import React, {useEffect} from 'react';
import svg4everybody from 'svg4everybody';
import {Provider} from '@clayui/provider';

export const decorators = [
(Story) => (
<Provider spritemap={spritemap}>
<div>
<Story />
</div>
</Provider>
),
(Story) => {
useEffect(() => {
svg4everybody({
polyfill: true,
});
}, []);

return (
<Provider spritemap={spritemap}>
<div>
<Story />
</div>
</Provider>
);
},
];

export const parameters = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"size-limit": "^6.0.3",
"source-map-loader": "^0.2.4",
"style-loader": "^0.23.1",
"svg4everybody": "^2.1.9",
"ts-jest": "^28.0.8",
"turbo": "^1.2.6",
"typescript": "4.7.4",
Expand Down
102 changes: 62 additions & 40 deletions packages/clay-core/src/table/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,50 @@ export const Cell = React.forwardRef<HTMLTableCellElement, Props>(
[expandedKeys, onExpandedChange]
);

const doSort = useCallback(
() =>
onSortChange(
{
column: keyValue!,
direction:
sort && keyValue === sort.column
? sort.direction === 'ascending'
? 'descending'
: 'ascending'
: 'ascending',
},
textValue!
),
[onSortChange, keyValue, sort]
);

const isExpandable = (expandable || lazy) && !isLoading;
const isSortable = isHead && sortable;

return (
<As
{...otherProps}
{...focusWithinProps}
aria-colindex={isHead && !sortable ? undefined : index! + 1}
aria-describedby={
isHead && sortable ? sortDescriptionId : undefined
}
aria-describedby={isSortable ? sortDescriptionId : undefined}
aria-sort={
isHead && sortable
isSortable
? sort && keyValue === sort.column
? sort.direction
: 'none'
: undefined
}
className={classNames(className, {
'order-arrow-down-active': isSortable
? sort &&
keyValue === sort.column &&
sort.direction === 'descending'
: undefined,
'order-arrow-up-active': isSortable
? sort &&
keyValue === sort.column &&
sort.direction === 'ascending'
: undefined,
'table-cell-expand': truncate || expanded,
[`table-cell-${delimiter}`]: delimiter,
[`table-column-text-${textAlign}`]: textAlign,
Expand All @@ -180,57 +208,51 @@ export const Cell = React.forwardRef<HTMLTableCellElement, Props>(
: `string,${keyValue}`
}
onClick={(event) => {
if (!(isHead && sortable)) {
if (!isSortable) {
return;
}

event.preventDefault();
onSortChange(
{
column: keyValue!,
direction:
sort && keyValue === sort.column
? sort.direction === 'ascending'
? 'descending'
: 'ascending'
: 'ascending',
},
textValue!
);
doSort();
}}
onKeyDown={(event) => {
if (event.key === Keys.Enter) {
toggle(key!);
if (isSortable) {
event.preventDefault();
doSort();
}

if (treegrid && isExpandable) {
toggle(key!);
}
}
}}
ref={ref}
role={treegrid ? 'gridcell' : undefined}
style={{
width,
}}
tabIndex={focusWithinProps.tabIndex}
>
{isHead && sortable ? (
<a
className="inline-item text-truncate-inline"
href="#"
onClick={(event) => event.preventDefault()}
role="presentation"
tabIndex={treegrid ? -1 : undefined}
>
<span className="text-truncate">{children}</span>

{sort && keyValue === sort.column && (
<span className="inline-item inline-item-after">
<Icon
symbol={
sort.direction === 'ascending'
? 'order-arrow-up'
: 'order-arrow-down'
}
/>
{isSortable ? (
<Layout.ContentRow>
<Layout.ContentCol expand>
<span className="text-truncate-inline">
<span className="text-truncate">
{children}
</span>
</span>
)}
</a>
</Layout.ContentCol>
<Layout.ContentCol>
<button
aria-label={messages['sortDescription']}
className="component-action"
type="button"
>
<Icon symbol="order-arrow" />
</button>
</Layout.ContentCol>
</Layout.ContentRow>
) : truncate ? (
<span className="text-truncate-inline">
<span className="text-truncate">{children}</span>
Expand All @@ -243,7 +265,7 @@ export const Cell = React.forwardRef<HTMLTableCellElement, Props>(
(expandable || lazy ? 4 : 0),
}}
>
{(expandable || lazy) && !isLoading && (
{isExpandable && (
<Layout.ContentCol className="autofit-col-toggle">
<Button
aria-label={messages['expandable']}
Expand Down
1 change: 1 addition & 0 deletions packages/clay-core/src/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export const Table = React.forwardRef<HTMLDivElement, Props>(
className={classNames(className, {
'table-nested-rows': nestedKey,
[`table-${size}`]: size,
'table-sort': sort || sort === null,
})}
ref={ref}
role={nestedKey ? 'treegrid' : undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,41 @@ exports[`Table basic rendering render with sort column 1`] = `
style="width: auto;"
tabindex="-1"
>
<a
class="inline-item text-truncate-inline"
href="#"
role="presentation"
<div
class="autofit-row"
>
<span
class="text-truncate"
<div
class="autofit-col autofit-col-expand"
>
Name
</span>
</a>
<span
class="text-truncate-inline"
>
<span
class="text-truncate"
>
Name
</span>
</span>
</div>
<div
class="autofit-col"
>
<button
aria-label="sortable column"
class="component-action"
type="button"
>
<svg
class="lexicon-icon lexicon-icon-order-arrow"
role="presentation"
>
<use
href="#order-arrow"
/>
</svg>
</button>
</div>
</div>
</th>
<th
aria-colindex="2"
Expand All @@ -353,17 +377,41 @@ exports[`Table basic rendering render with sort column 1`] = `
style="width: auto;"
tabindex="-1"
>
<a
class="inline-item text-truncate-inline"
href="#"
role="presentation"
<div
class="autofit-row"
>
<span
class="text-truncate"
<div
class="autofit-col autofit-col-expand"
>
Type
</span>
</a>
<span
class="text-truncate-inline"
>
<span
class="text-truncate"
>
Type
</span>
</span>
</div>
<div
class="autofit-col"
>
<button
aria-label="sortable column"
class="component-action"
type="button"
>
<svg
class="lexicon-icon lexicon-icon-order-arrow"
role="presentation"
>
<use
href="#order-arrow"
/>
</svg>
</button>
</div>
</div>
</th>
<th
class="table-head-title"
Expand Down
2 changes: 1 addition & 1 deletion packages/clay-core/src/table/useTreeNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function useTreeNavigation<T extends HTMLElement>({
Keys.Down,
Keys.Home,
Keys.End,
].includes(event.key) &&
].includes(event.key) ||
disabled
) {
return;
Expand Down
3 changes: 3 additions & 0 deletions packages/clay-css/src/scss/cadmin/variables/_tables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ $cadmin-c-table: map-deep-merge(
color: $cadmin-table-disabled-color,
),
),
table-sort: (
color: $cadmin-gray-900,
),
autofit-col: (
justify-content: center,
padding-left: nth($cadmin-table-cell-padding, 2),
Expand Down
7 changes: 7 additions & 0 deletions packages/clay-css/src/scss/components/_tables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ caption {
tr.table-focus {
@include clay-css($c-tr-table-focus);

th,
td {
$_td: setter(map-get($c-tr-table-focus, td), ());

Expand Down Expand Up @@ -527,6 +528,12 @@ td.table-focus {
}
}

// .table-sort

.table-sort {
@include clay-table-variant($c-table-sort);
}

// Show Quick Action

.show-quick-actions-on-hover {
Expand Down
10 changes: 10 additions & 0 deletions packages/clay-css/src/scss/mixins/_tables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@

@include clay-css($_thead-table-cell);

&:hover {
@include clay-css(map-get($_thead-table-cell, hover));
}

&:first-child {
@include clay-css(
map-get($_thead-table-cell, table-column-start)
Expand All @@ -82,6 +86,12 @@
map-get($_thead-table-cell, table-column-end)
);
}

.component-action {
@include clay-css(
map-get($_thead-table-cell, component-action)
);
}
}

th {
Expand Down
24 changes: 24 additions & 0 deletions packages/clay-css/src/scss/variables/_tables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,30 @@ $c-table-nested-rows: map-deep-merge(
$c-table-nested-rows
);

// .table-sort

$c-table-sort: () !default;
$c-table-sort: map-deep-merge(
(
thead: (
table-cell: (
cursor: pointer,
transition: clay-enable-transitions($component-transition),
hover: (
background-color: $primary-l3,
color: $gray-900,
),
component-action: (
font-size: 0.75rem,
height: 1.5rem,
width: 1.5rem,
),
),
),
),
$c-table-sort
);

// Table Dark Variant

$table-dark-bg: $gray-800 !default;
Expand Down
Loading
Loading