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(forms): migrate to DS Tabs + CollapsibleFieldset #4928

Merged
merged 18 commits into from
Nov 28, 2023
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
5 changes: 5 additions & 0 deletions .changeset/beige-terms-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@talend/react-forms': minor
---

Use DS tabs in UIForm
5 changes: 5 additions & 0 deletions .changeset/few-months-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@talend/design-system': minor
---

Error state for tabs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,24 @@
margin-right: tokens.$coral-spacing-s;
}
}

&_error {
color: tokens.$coral-color-danger-text;

&[aria-selected='true'] {
color: tokens.$coral-color-danger-text;
}
}

&_error::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
border-top: tokens.$coral-border-m-solid tokens.$coral-color-danger-text;
opacity: 0;
transition: tokens.$coral-transition-fast;
transform: translateY(100%);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useContext } from 'react';
import { SizedIcon } from '../../Icon';

Check warning on line 2 in packages/design-system/src/components/Tabs/Primitive/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/Primitive/Tabs.tsx#L2

[prettier/prettier] Replace `import·{·SizedIcon·}·from·'../../Icon';` with `⏎import·classNames·from·'classnames';⏎⏎import·{·IconNameWithSize·}·from·'@talend/icons';⏎`
import { TagDefault } from '../../Tag';

Check warning on line 3 in packages/design-system/src/components/Tabs/Primitive/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/Primitive/Tabs.tsx#L3

[prettier/prettier] Replace `TagDefault·}·from·'../../Tag` with `SizedIcon·}·from·'../../Icon`
import { StackHorizontal } from '../../Stack';
import { TabsInternalContext } from './TabsProvider';

Check warning on line 5 in packages/design-system/src/components/Tabs/Primitive/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/Primitive/Tabs.tsx#L5

[prettier/prettier] Replace `bsInternalContext·}·from·'./TabsProvider` with `gDefault·}·from·'../../Tag`
import { Tooltip } from '../../Tooltip';
import style from './TabStyles.module.scss';

Check warning on line 7 in packages/design-system/src/components/Tabs/Primitive/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/Primitive/Tabs.tsx#L7

[prettier/prettier] Replace `style·from·'./TabStyles.module.scss';` with `{·TabsInternalContext·}·from·'./TabsProvider';⏎`
import { IconNameWithSize } from '@talend/icons';
import classNames from 'classnames';

Check warning on line 9 in packages/design-system/src/components/Tabs/Primitive/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/Primitive/Tabs.tsx#L8-L9

[prettier/prettier] Replace `{·IconNameWithSize·}·from·'@talend/icons';⏎import·classNames·from·'classname` with `style·from·'./TabStyles.module.scs`

export type TabsPropTypes = {
children: React.ReactNode[];
Expand All @@ -28,6 +28,7 @@
icon?: IconNameWithSize<'S'>;
tag?: string | number;
tooltip?: string;
error?: boolean;
};

export function Tab(props: TabPropTypes) {
Expand All @@ -36,7 +37,10 @@
<button
role="tab"
aria-selected={props['aria-controls'] === context?.value}
className={classNames(style.tab, { [style.tab_large]: context?.size === 'L' })}
className={classNames(style.tab, {
[style.tab_large]: context?.size === 'L',
[style.tab_error]: props.error === true,
})}
onClick={e => context?.onChange(e, props['aria-controls'])}
disabled={props.disabled}
type="button"
Expand Down
9 changes: 3 additions & 6 deletions packages/design-system/src/components/Tabs/variants/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TabsProvider, TabsProviderPropTypes } from '../Primitive/TabsProvider';

Check warning on line 1 in packages/design-system/src/components/Tabs/variants/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/variants/Tabs.tsx#L1

[prettier/prettier] Replace `TabsProvider,·TabsProviderPropTypes·}·from·'../Primitive/TabsProvider` with `useEffect,·useState·}·from·'react';⏎⏎import·{·randomUUID·}·from·'@talend/utils';⏎⏎import·{·TabPanel,·TabPanelPropTypes·}·from·'../Primitive/TabPanel`
import { Tabs as TabList, Tab, TabPropTypes } from '../Primitive/Tabs';

Check warning on line 2 in packages/design-system/src/components/Tabs/variants/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/variants/Tabs.tsx#L2

[prettier/prettier] Replace `s·as·TabList,·Tab` with `,·Tabs·as·TabList`
import { TabPanel, TabPanelPropTypes } from '../Primitive/TabPanel';
import { useEffect, useState } from 'react';
import { randomUUID } from '@talend/utils';

Check warning on line 5 in packages/design-system/src/components/Tabs/variants/Tabs.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/components/Tabs/variants/Tabs.tsx#L3-L5

[prettier/prettier] Replace `Panel,·TabPanelPropTypes·}·from·'../Primitive/TabPanel';⏎import·{·useEffect,·useState·}·from·'react';⏎import·{·randomUUID·}·from·'@talend/utils` with `sProvider,·TabsProviderPropTypes·}·from·'../Primitive/TabsProvider`

type TabTitlePropTypes = Omit<TabPropTypes, 'aria-controls'> & {
id?: string;
Expand Down Expand Up @@ -50,12 +50,9 @@
tabProps['aria-controls'] = ids[index];
tabProps.title = tab.tabTitle;
} else if (typeof tab.tabTitle === 'object') {
tabProps['aria-controls'] = tab.tabTitle.id || ids[index];
tabProps.title = tab.tabTitle.title;
tabProps.icon = tab.tabTitle.icon;
tabProps.tag = tab.tabTitle.tag;
tabProps.tooltip = tab.tabTitle.tooltip;
tabProps.disabled = tab.tabTitle.disabled;
const { id, ...rest } = tab.tabTitle;
tabProps['aria-controls'] = id || ids[index];
Object.assign(tabProps, rest);
}
return <Tab key={index} {...(tabProps as TabPropTypes)} />;
})}
Expand Down
22 changes: 22 additions & 0 deletions packages/design-system/src/stories/navigation/Tabs.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react';
import { StackHorizontal, StackVertical, Tabs } from '../../';

Check warning on line 2 in packages/design-system/src/stories/navigation/Tabs.stories.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/design-system/src/stories/navigation/Tabs.stories.tsx#L2

[prettier/prettier] Insert `⏎`

export default { component: Tabs, title: 'Navigation/Tabs' };

Expand Down Expand Up @@ -34,6 +34,28 @@
</StackHorizontal>
);

export const TabsWithError = () => (
<Tabs
tabs={[
{
tabTitle: {
icon: 'user',
title: 'User',
error: true,
},
tabContent: <h2>Users tab content</h2>,
},
{
tabTitle: {
icon: 'calendar',
title: 'Calendar',
},
tabContent: <h2>Calendar tab content</h2>,
},
]}
/>
);

export const TabsWithIcon = () => (
<Tabs.Container defaultActiveKey="profile">
<Tabs.List>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import PropTypes from 'prop-types';
import { Component } from 'react';

Check warning on line 2 in packages/forms/src/UIForm/fieldsets/Array/Array.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/Array/Array.component.js#L1-L2

[prettier/prettier] Delete `import·PropTypes·from·'prop-types';⏎`
import { head, get } from 'lodash';

Check warning on line 3 in packages/forms/src/UIForm/fieldsets/Array/Array.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/Array/Array.component.js#L3

[prettier/prettier] Replace `import·{·head,·get` with `⏎import·{·get,·head`
import Widget from '../../Widget';

Check warning on line 4 in packages/forms/src/UIForm/fieldsets/Array/Array.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/Array/Array.component.js#L4

[prettier/prettier] Replace `Widget·from·'../../Widget';` with `PropTypes·from·'prop-types';⏎`
import { shiftArrayErrorsKeys } from '../../utils/validation';

Check warning on line 5 in packages/forms/src/UIForm/fieldsets/Array/Array.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/Array/Array.component.js#L5

[prettier/prettier] Replace `shiftArrayErrorsKeys·}·from·'../../utils/validation` with `getArrayElementSchema·}·from·'../../utils/array`
import defaultTemplates from '../../utils/templates';
import { getArrayElementSchema } from '../../utils/array';

Check warning on line 7 in packages/forms/src/UIForm/fieldsets/Array/Array.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/Array/Array.component.js#L7

[prettier/prettier] Replace `getArrayElementSchema·}·from·'../../utils/array` with `shiftArrayErrorsKeys·}·from·'../../utils/validation';⏎import·Widget·from·'../../Widget`

function getRange(previousIndex, nextIndex) {
if (previousIndex < nextIndex) {
Expand Down Expand Up @@ -124,6 +124,7 @@
<Widget
{...this.props}
{...extraProps}
index={index}
disabled={this.props.schema.disabled}
id={this.props.id && `${this.props.id}-${index}`}
schema={getArrayElementSchema(this.props.schema, index)}
Expand Down Expand Up @@ -155,7 +156,7 @@
widgets: {},
};

if (process.env.NODE_ENV !== 'production') {

Check warning on line 159 in packages/forms/src/UIForm/fieldsets/Array/Array.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/Array/Array.component.js#L159

[no-undef] 'process' is not defined.
ArrayWidget.propTypes = {
displayMode: PropTypes.string,
id: PropTypes.string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useCallback } from 'react';

import PropTypes from 'prop-types';

Check warning on line 3 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js#L3

[prettier/prettier] Replace `PropTypes·from·'prop-typ` with `classNames·from·'classnam`
import classNames from 'classnames';

Check warning on line 4 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js#L4

[prettier/prettier] Replace `classNames·from·'classnames';` with `get·from·'lodash/get';⏎import·PropTypes·from·'prop-types';⏎`
import { InlineMessageInformation } from '@talend/design-system';
import CollapsiblePanel from '@talend/react-components/lib/CollapsiblePanel';
import { InlineMessageInformation, CollapsiblePanel } from '@talend/design-system';

Check warning on line 5 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js#L5

[prettier/prettier] Replace `InlineMessageInformation,·CollapsiblePanel` with `CollapsiblePanel,·InlineMessageInformation`
import get from 'lodash/get';
import Widget from '../../Widget';

Check warning on line 7 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js#L6-L7

[prettier/prettier] Delete `import·get·from·'lodash/get';⏎import·Widget·from·'../../Widget';`
import { generateDescriptionId } from '../../Message/generateId';

Check warning on line 8 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js#L8

[prettier/prettier] Insert `';⏎import·Widget·from·'../../Widget`

import theme from './CollapsibleFieldset.module.scss';

Expand Down Expand Up @@ -72,11 +73,18 @@
* @param {function} title the function called by the component to compute the title
* @return {function} CollapsibleFieldset react component
*/
// eslint-disable-next-line @typescript-eslint/default-param-last
export default function createCollapsibleFieldset(title = defaultTitle) {
function CollapsibleFieldset(props) {
function toggle(event) {
event.stopPropagation();
event.preventDefault();
const { id, schema, value, actions, index, ...restProps } = props;
const { items, managed } = schema;

function onToggleClick(event) {
if (event) {
event.stopPropagation();
event.preventDefault();
}

const payload = {
schema: props.schema,
value: {
Expand All @@ -87,22 +95,31 @@
props.onChange(event, payload);
}

const { id, schema, value, actions, ...restProps } = props;
const { items } = schema;
const displayAction = actions.map(action => ({
...action,
displayMode: CollapsiblePanel.displayModes.TYPE_ACTION,
}));
const getAction = useCallback(() => {
if (!actions || actions.length === 0 || actions[0] === undefined) {
return undefined;
}

const action = actions[0];

return {
...action,
tooltip: action.tooltip || action.label,
callback: action.onClick,
};
}, [actions]);

return (
<fieldset
className={classNames('form-group', theme['collapsible-panel'], 'collapsible-panel')}
>
<CollapsiblePanel
id={`${id}`}
header={[{ label: title(value, schema) }, displayAction]}
onToggle={toggle}
title={title(value, schema)}
onToggleExpanded={onToggleClick}
index={index}
managed={!!managed}
expanded={!value.isClosed}
action={getAction()}
>
{schema.description ? (
<InlineMessageInformation
Expand All @@ -115,8 +132,8 @@
) : (
''
)}
{items.map((itemSchema, index) => (
<Widget {...restProps} id={id} key={index} schema={itemSchema} value={value} />
{items.map((itemSchema, idx) => (
<Widget {...restProps} id={id} key={`${id}-${idx}`} schema={itemSchema} value={value} />
))}
</CollapsiblePanel>
</fieldset>
Expand All @@ -129,12 +146,14 @@
};
CollapsibleFieldset.isCloseable = true;

if (process.env.NODE_ENV !== 'production') {

Check warning on line 149 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.js#L149

[no-undef] 'process' is not defined.
CollapsibleFieldset.propTypes = {
id: PropTypes.string,
index: PropTypes.number,
onChange: PropTypes.func.isRequired,
schema: PropTypes.shape({
items: PropTypes.array.isRequired,
managed: PropTypes.bool,
description: PropTypes.string,
}).isRequired,
value: PropTypes.object,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import set from 'lodash/set';
import cloneDeep from 'lodash/cloneDeep';
import { screen, render } from '@testing-library/react';

Check warning on line 3 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.test.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.test.js#L1-L3

[prettier/prettier] Replace `set·from·'lodash/set';⏎import·cloneDeep·from·'lodash/cloneDeep';⏎import·{·screen,·render` with `{·render,·screen`
import userEvent from '@testing-library/user-event';
import createCollapsibleFieldset, { defaultTitle } from './CollapsibleFieldset.component';

Check warning on line 5 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.test.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.test.js#L5

[prettier/prettier] Replace `reateCollapsibleFieldset,·{·defaultTitle·}·from·'./CollapsibleFieldset.component';` with `loneDeep·from·'lodash/cloneDeep';⏎import·set·from·'lodash/set';⏎`
import { WidgetContext } from '../../context';
import widgets from '../../utils/widgets';

Check warning on line 7 in packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.test.js

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/forms/src/UIForm/fieldsets/CollapsibleFieldset/CollapsibleFieldset.component.test.js#L7

[prettier/prettier] Insert `';⏎import·createCollapsibleFieldset,·{·defaultTitle·}·from·'./CollapsibleFieldset.component`

jest.unmock('@talend/design-system');

Expand Down Expand Up @@ -90,7 +90,7 @@
<CollapsibleFieldset {...props} value={{ ...value, isClosed: true }} />
</WidgetContext.Provider>,
);
expect(screen.getByRole('tab')).toHaveTextContent('Jimmy, Somsanith');
expect(screen.getByText('Jimmy, Somsanith')).toBeInTheDocument();
expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'false');
});
it('should render a custom title', () => {
Expand All @@ -101,7 +101,7 @@
<CollapsibleFieldset {...props} />
</WidgetContext.Provider>,
);
expect(screen.getByRole('tab')).toHaveTextContent('Basic: Jimmy Somsanith');
expect(screen.getByText('Basic: Jimmy Somsanith')).toBeInTheDocument();
});
it('should render without value', () => {
const CollapsibleFieldset = createCollapsibleFieldset();
Expand All @@ -110,42 +110,48 @@
<CollapsibleFieldset {...props} value={{}} />
</WidgetContext.Provider>,
);
expect(screen.getByRole('tab')).toHaveTextContent('Basic');
expect(screen.getByText('Basic')).toBeInTheDocument();
});

it('should toggle', async () => {
// given
const CollapsibleFieldset = createCollapsibleFieldset();

const extendedSchema = {
...props.schema,
managed: true,
};

render(
<WidgetContext.Provider value={widgets}>
<CollapsibleFieldset {...props} value={{ ...value, isClosed: true }} />
<CollapsibleFieldset
{...props}
value={{ ...value, isClosed: true }}
schema={extendedSchema}
index={0}
/>
</WidgetContext.Provider>,
);
// when
await userEvent.click(screen.getByRole('button'));

// then
expect(props.onChange).toBeCalledWith(expect.anything(), {
schema,
schema: extendedSchema,
value: { ...value, isClosed: false },
});
});

it('should render Actions component if actions are provided', () => {
const CollapsibleFieldset = createCollapsibleFieldset();
const actions = [
{ id: 'action1', label: 'Action1', onClick: jest.fn() },
{ id: 'action2', label: 'Action 2', onClick: jest.fn() },
];
const actions = [{ id: 'action1', label: 'Action1', onClick: jest.fn(), icon: 'talend-trash' }];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove one action ?


render(
<WidgetContext.Provider value={widgets}>
<CollapsibleFieldset {...props} actions={actions} />
</WidgetContext.Provider>,
);
expect(screen.getByRole('button', { name: 'Action1' })).toBeVisible();
expect(screen.getByRole('button', { name: 'Action 2' })).toBeVisible();
});

it('should not render Actions component if actions are not provided', () => {
Expand Down
Loading
Loading