Skip to content

Commit

Permalink
feat(Datagrid): add ai slug support to col headers (#4163)
Browse files Browse the repository at this point in the history
* feat(Datagrid): start slug addition in col headers

* feat(Datagrid): add column header slug support

* chore: update initial widths

* chore: revert copyright header change

* chore: small refactor
  • Loading branch information
matthewgallo authored Jan 30, 2024
1 parent cc3061b commit f0b17e2
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/core/story-structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const s = [
'c/Datagrid/Extensions/EditableCell',
'c/Datagrid/Extensions/ColumnCustomization',
'c/Datagrid/Extensions/Skeleton',
'c/Datagrid/Extensions/Slug',
],
},
{ n: 'DelimitedList', s: ['c/DelimitedList'] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

@use '@carbon/styles/scss/theme' as *;
@use '@carbon/styles/scss/utilities' as *;
@use '@carbon/layout/scss/convert' as *;
@use '@carbon/styles/scss/spacing' as *;
@use '@carbon/react/scss/components/button/tokens' as *;
Expand Down Expand Up @@ -157,6 +158,16 @@
background-color: $background;
}

.#{$block-class} th.#{$block-class}__with-slug {
@include ai-gradient('top', 100%);
}

.#{$block-class}
th.#{$block-class}__with-slug
.#{c4p-settings.$carbon-prefix}--slug {
margin-left: $spacing-03;
}

.#{$block-class}__grid-container {
display: block;
width: 100%;
Expand Down Expand Up @@ -243,6 +254,10 @@
white-space: initial;
}

.#{$block-class}__defaultStringRenderer.#{$block-class}__defaultStringRenderer--slug {
width: fit-content;
}

.#{$block-class}__expanded-row {
display: flex;
overflow: hidden;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/prop-types */
/**
* Copyright IBM Corp. 2020, 2023
* Copyright IBM Corp. 2020, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -17,6 +17,7 @@ import {
handleColumnResizingEvent,
} from './addons/stateReducer';
import { getNodeTextContent } from '../../../global/js/utils/getNodeTextContent';
import { ColumnHeaderSlug } from './addons/Slug/ColumnHeaderSlug';

const blockClass = `${pkg.prefix}--datagrid`;

Expand Down Expand Up @@ -92,7 +93,7 @@ const ResizeHeader = ({
};

const HeaderRow = (datagridState, headRef, headerGroup) => {
const { resizerAriaLabel } = datagridState;
const { resizerAriaLabel, isTableSortable } = datagridState;
// Used to measure the height of the table and uses that value
// to display a vertical line to indicate the column you are resizing
useEffect(() => {
Expand Down Expand Up @@ -149,6 +150,13 @@ const HeaderRow = (datagridState, headRef, headerGroup) => {
...headerGroupProps
} = headerGroup.getHeaderGroupProps();

const renderSlug = (slug) => {
if (isTableSortable) {
return;
}
return <ColumnHeaderSlug slug={slug} />;
};

return (
<TableRow
{...headerGroupProps}
Expand Down Expand Up @@ -185,12 +193,15 @@ const HeaderRow = (datagridState, headRef, headerGroup) => {
datagridState.isTableSortable && header.id !== 'spacer',
[`${blockClass}__isSorted`]: header.isSorted,
[`${blockClass}__header-actions-column`]: header.isAction,
[`${blockClass}__with-slug`]:
header.slug && React.isValidElement(header.slug),
})}
key={header.id}
aria-hidden={header.id === 'spacer' && 'true'}
{...getAccessibilityProps(header)}
>
{header.render('Header')}
{renderSlug(header.slug)}
{resizerProps && !header.isAction && (
<ResizeHeader
{...{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright IBM Corp. 2024, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, { forwardRef, isValidElement } from 'react';
import PropTypes from 'prop-types';

export const ColumnHeaderSlug = forwardRef(({ slug }, ref) => {
if (slug && isValidElement(slug)) {
const normalizedSlug = React.cloneElement(slug, {
size: 'mini',
ref,
});
return normalizedSlug;
}
return;
});

ColumnHeaderSlug.propTypes = {
/**
* Specify the AI slug to be displayed
*/
slug: PropTypes.node,
};
Loading

0 comments on commit f0b17e2

Please sign in to comment.