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

BREAKING CHANGE: Add mandatory id prop for Collapse and Dropdown component #DS-1190 #1402

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { useCollapse } from './useCollapse';
import { useCollapseAriaProps } from './useCollapseAriaProps';

const defaultProps = {
/** @deprecated ID will be made a required user input in the next major version. */
id: Math.random().toString(36).slice(2, 7),
isOpen: false,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@testing-library/jest-dom';
import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest';
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest';
Expand All @@ -13,9 +13,10 @@ describe('UncontrolledCollapse', () => {

restPropsTest(UncontrolledCollapse, 'div');

it('should render text children', () => {
const dom = render(
beforeEach(() => {
literat marked this conversation as resolved.
Show resolved Hide resolved
render(
<UncontrolledCollapse
id="exampleId"
// Normally we want to display state change, not in this test, prop is passed anyway
// eslint-disable-next-line @typescript-eslint/no-unused-vars
renderTrigger={({ isOpen, ...rest }) => (
Expand All @@ -27,27 +28,17 @@ describe('UncontrolledCollapse', () => {
Hello World
</UncontrolledCollapse>,
);
const element = dom.container.querySelector('div') as HTMLElement;
});

expect(element.textContent).toBe('Hello World');
it('should render text children', () => {
const element = screen.getByRole('button').parentElement as HTMLElement;

expect(element).toHaveTextContent('Hello World');
});

it('should toggle a collapse', () => {
const dom = render(
<UncontrolledCollapse
// Normally we want to display state change, not in this test, prop is passed anyway
// eslint-disable-next-line @typescript-eslint/no-unused-vars
renderTrigger={({ isOpen, ...rest }) => (
<button type="button" {...rest}>
trigger
</button>
)}
>
Hello World
</UncontrolledCollapse>,
);
const element = dom.container.querySelector('div') as HTMLElement;
const trigger = dom.container.querySelector('button') as HTMLElement;
const trigger = screen.getByRole('button') as HTMLElement;
const element = trigger.nextElementSibling as HTMLElement;

fireEvent.click(trigger);

Expand All @@ -58,4 +49,10 @@ describe('UncontrolledCollapse', () => {

expect(trigger).toHaveAttribute('aria-expanded', 'false');
});

it('should have correct id', () => {
const element = screen.getByRole('button').nextElementSibling as HTMLElement;

expect(element).toHaveAttribute('id', 'exampleId');
});
});
4 changes: 3 additions & 1 deletion packages/web-react/src/components/Collapse/demo/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const Story: ComponentStory<typeof Collapse> = () => {
<div>
{content}
<CollapseTrigger isOpen={isOpen} onClick={toggleHandler} />
<Collapse isOpen={isOpen}>{content}</Collapse>
<Collapse id="collapseId" isOpen={isOpen}>
{content}
</Collapse>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseDefault = () => {
<Button isOpen={isOpen} onClick={toggleHandler}>
Collapse trigger
</Button>
<Collapse isOpen={isOpen}>
<Collapse id="collapseDefaultId" isOpen={isOpen}>
Aliquam varius, consequat posuere a lacinia mauris eu tellus condimentum ut id ante, accumsan vehicula nulla
neque. Mauris mi orci, in donec nullam odio leo sapien et vehicula nunc a lacinia, fermentum arcu ullamcorper
posuere. Mauris euismod, ac nec ante fermentum praesent nisi commodo neque placerat, vivamus dui et tempus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CollapseHelperClass = () => {
posuere tristique bibendum aliquam. In donec dolor ut, imperdiet quam fermentum molestie vulputate scelerisque
ac nec, tortor mi orci sollicitudin elementum.
</p>
<Collapse isOpen={isOpen}>
<Collapse id="collapseHelperClassId" isOpen={isOpen}>
Aliquam varius, consequat posuere a lacinia mauris eu tellus condimentum ut id ante, accumsan vehicula nulla
neque. Mauris mi orci, in donec nullam odio leo sapien et vehicula nunc a lacinia, fermentum arcu ullamcorper
posuere. Mauris euismod, ac nec ante fermentum praesent nisi commodo neque placerat, vivamus dui et tempus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CollapseHideTrigger = () => {
<a href="#" role="button" onClick={toggleHandler} aria-expanded={isOpen}>
<span className="accessibility-closed">… more</span>
</a>
<Collapse isOpen={isOpen}>
<Collapse id="collapseHideTriggerId" isOpen={isOpen}>
Commodo metus a lorem, a aliquet vestibulum rutrum pharetra sapien sed, ullamcorper quis odio dolor ut aliquam.
Rutrum suspendisse, fermentum tellus metus a lorem cursus volutpat proin bibendum, sed diam a duis id dui et
tempus. Ligula non, sapien augue libero eget aliquam semper varius, posuere urna leo vitae ullamcorper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseMultipleTriggers = () => {
<Button isOpen={isOpen} onClick={toggleHandler}>
Collapse trigger
</Button>
<Collapse isOpen={isOpen}>
<Collapse id="collapseMultipleTriggersId" isOpen={isOpen}>
Aliquam varius, consequat posuere a lacinia mauris eu tellus condimentum ut id ante, accumsan vehicula nulla
neque. Mauris mi orci, in donec nullam odio leo sapien et vehicula nunc a lacinia, fermentum arcu ullamcorper
posuere. Mauris euismod, ac nec ante fermentum praesent nisi commodo neque placerat, vivamus dui et tempus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseOpenOnInit = () => {
<Button isOpen={isOpen} onClick={toggleHandler}>
Collapse trigger
</Button>
<Collapse isOpen={isOpen}>
<Collapse id="collapseOpenOnInitId" isOpen={isOpen}>
Cras dictum ante, mollis ollicitudin proin bibendum nec commodo consequat fusce ante, consequat venenatis
suscipit odio morbi. Dolor sit amet porta, placerat tristique sit amet ligula nisl risus et vehicula, suscipit
accumsan nunc curabitur. Et neque, augue ut nulla a sed porta scelerisque proin, elit sapien lacinia felis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const Story: ComponentStory<typeof UncontrolledCollapse> = () => {
return (
<div>
{content}
<UncontrolledCollapse renderTrigger={CollapseTrigger}>{content}</UncontrolledCollapse>
<UncontrolledCollapse id="uncontrolledCollapseId" renderTrigger={CollapseTrigger}>
{content}
</UncontrolledCollapse>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseVisibilityBreakpointDesktop = () => {
<ButtonLink isOpen={isOpenDesktop} onClick={toggleHandlerDesktop} size="medium" UNSAFE_className="d-desktop-none">
Collapse trigger
</ButtonLink>
<Collapse isOpen={isOpenDesktop} collapsibleToBreakpoint="desktop">
<Collapse id="collapseVisibilityBreakpointDesktopId" isOpen={isOpenDesktop} collapsibleToBreakpoint="desktop">
Elementum luctus ultrices, ultrices quam metus a lorem sollicitudin lorem fringilla curabitur felis a, sed urna
consectetur maximus. Imperdiet donec, elit condimentum dolor ut accumsan congue nulla ut id ante vel arcu, lorem
fringilla interdum pulvinar nullam. Curabitur interdum, semper donec dui et tempus fusce vitae ornare risus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseVisibilityBreakpointTablet = () => {
<ButtonLink isOpen={isOpenTablet} onClick={toggleHandlerTablet} size="medium" UNSAFE_className="d-tablet-none">
Collapse trigger
</ButtonLink>
<Collapse isOpen={isOpenTablet} collapsibleToBreakpoint="tablet">
<Collapse id="collapseVisibilityBreakpointTabletId" isOpen={isOpenTablet} collapsibleToBreakpoint="tablet">
Turpis cursus, urna vehicula sed porttitor nulla non mauris sapien congue, urna dui augue facilisis. Nunc elit,
ipsum porttitor curabitur sapien nulla finibus quis pulvinar, commodo convallis lorem fringilla nec. Quam
libero, vitae massa ornare eget vestibulum et iaculis quisque sapien, turpis maximus maximus vivamus. Nibh
Expand Down
3 changes: 1 addition & 2 deletions packages/web-react/src/types/collapse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export type CollapseRenderProps = {
};

export interface BaseCollapseProps extends ChildrenProps, StyleProps {
/** @deprecated The "id" property will be required instead of optional starting from the next major version. */
id?: string;
id: string;
}

export interface CollapseProps extends BaseCollapseProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
{%- set _fullWidthMode = props.fullWidthMode | default(null) -%}
{%- set _elementType = props.elementType | default('div') -%}
{%- set _placement = props.placement | default('bottom-start') -%}
{%- set _id = props.id -%}

{# Class names #}
{%- set _rootClassName = _spiritClassPrefix ~ 'Dropdown' -%}

{# Attributes #}
{%- set _dataFullWidthModeAttr = _fullWidthMode ? 'data-spirit-fullwidthmode="' ~ _fullWidthMode | escape('html_attr') ~ '"' : null -%}
{%- set _dataPlacementAttr = _placement ? 'data-spirit-placement="' ~ _placement | escape('html_attr') ~ '"' : null -%}
{%- set _idAttr = _id ? 'id="' ~ _id | escape('html_attr') ~ '"' : null -%}

{# Miscellaneous #}
{%- set _styleProps = useStyleProps(props) -%}
{%- set _classNames = [ _rootClassName, _styleProps.className ] -%}
{%- set _mainPropsWithoutReservedAttributes = props | filter((value, prop) => prop not in ['data-spirit-placement']) -%}
{%- set _mainPropsWithoutReservedAttributes = props | filter((value, prop) => prop not in ['data-spirit-placement', 'id']) -%}

<{{ _elementType }}
{{ mainProps(_mainPropsWithoutReservedAttributes) }}
{{ styleProp(_styleProps) }}
{{ classProp(_classNames) }}
{{ _dataFullWidthModeAttr | raw }}
{{ _dataPlacementAttr | raw }}
{{ _idAttr | raw }}
>
{%- block content %}{% endblock -%}
</{{ _elementType }}>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</div>
<!-- Render with all props -->

<aside id="my-dropdown" class="Dropdown" data-spirit-fullwidthmode="mobile-only" data-spirit-placement="top-end">
<aside class="Dropdown" data-spirit-fullwidthmode="mobile-only" data-spirit-placement="top-end" id="my-dropdown">
content
</aside>
</body>
Expand Down
Loading