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

feat(Datagrid): add ai slug support to col headers #4163

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
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 @@ -155,6 +156,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 @@ -235,6 +246,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
Loading