Skip to content

Commit

Permalink
Fix: Open all sections when searching (#1537)
Browse files Browse the repository at this point in the history
When searching with the option tocmode: 'collapse' the sections do not open automatically (because they keep their state). The found section or page is therefore not visible nor clickable.
  • Loading branch information
Barthélémy Ledoux committed Feb 13, 2020
1 parent a63e7ec commit 89420ca
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/client/rsg-components/ComponentsList/ComponentsList.spec.tsx
Expand Up @@ -229,3 +229,40 @@ it('should show content of initialOpen items even if they are not active', () =>
Array.from(getAllByTestId('content')).map(node => (node as HTMLDivElement).innerHTML)
).toEqual(['Content for Button', 'Content for Input']);
});

it('should show content of forcedOpen items even if they are initially collapsed', () => {
const components = [
{
visibleName: 'Button',
name: 'Button',
slug: 'button',
content: <div data-testid="content">Content for Button</div>,
initialOpen: true,
},
{
visibleName: 'Input',
name: 'Input',
slug: 'input',
content: <div data-testid="content">Content for Input</div>,
initialOpen: true,
forcedOpen: true,
},
];

const { getAllByTestId, getByText } = render(
<Provider>
<ComponentsList
items={components}
useRouterLinks
hashPath={['Components']}
useHashId={false}
/>
</Provider>
);

getByText('Input').click();

expect(
Array.from(getAllByTestId('content')).map(node => (node as HTMLDivElement).innerHTML)
).toEqual(['Content for Button', 'Content for Input']);
});
Expand Up @@ -63,6 +63,7 @@ const ComponentsListSectionRenderer: React.FunctionComponent<Rsg.TOCItem & JssIn
shouldOpenInNewTab,
selected,
initialOpen,
forcedOpen,
}) => {
const {
config: { tocMode },
Expand All @@ -85,7 +86,7 @@ const ComponentsListSectionRenderer: React.FunctionComponent<Rsg.TOCItem & JssIn
>
{visibleName}
</Link>
{open ? content : null}
{open || forcedOpen ? content : null}
</li>
);
};
Expand Down
Expand Up @@ -153,6 +153,7 @@ it('should render content of subsections of a section that has no components', (
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"sections": Array [],
Expand All @@ -162,6 +163,7 @@ it('should render content of subsections of a section that has no components', (
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"sections": Array [],
Expand All @@ -180,6 +182,7 @@ it('should render components of a single top section as root', () => {
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Button",
Expand All @@ -191,6 +194,7 @@ it('should render components of a single top section as root', () => {
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Input",
Expand All @@ -202,6 +206,7 @@ it('should render components of a single top section as root', () => {
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Textarea",
Expand Down
4 changes: 2 additions & 2 deletions src/client/rsg-components/TableOfContents/TableOfContents.tsx
Expand Up @@ -76,8 +76,8 @@ export default class TableOfContents extends Component<TableOfContentsProps> {
content,
selected,
shouldOpenInNewTab: !!section.href,
initialOpen:
!!this.state.searchTerm.length || this.props.tocMode !== 'collapse' || containsSelected,
initialOpen: this.props.tocMode !== 'collapse' || containsSelected,
forcedOpen: !!this.state.searchTerm.length,
};
});
return {
Expand Down
Expand Up @@ -32,6 +32,7 @@ exports[`should filter list when search field contains a query 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Button",
Expand All @@ -43,6 +44,7 @@ exports[`should filter list when search field contains a query 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Input",
Expand All @@ -54,6 +56,7 @@ exports[`should filter list when search field contains a query 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Textarea",
Expand Down Expand Up @@ -82,6 +85,7 @@ exports[`should filter list when search field contains a query 2`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": true,
"heading": false,
"initialOpen": true,
"name": "Button",
Expand Down Expand Up @@ -110,6 +114,7 @@ exports[`should filter section names 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Introduction",
Expand All @@ -135,6 +140,7 @@ exports[`should filter section names 1`] = `
Array [
Object {
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Button",
Expand All @@ -147,6 +153,7 @@ exports[`should filter section names 1`] = `
useHashId={true}
useRouterLinks={false}
/>,
"forcedOpen": false,
"heading": true,
"initialOpen": true,
"name": "Buttons",
Expand Down Expand Up @@ -176,6 +183,7 @@ exports[`should filter section names 1`] = `
Array [
Object {
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Input",
Expand All @@ -185,6 +193,7 @@ exports[`should filter section names 1`] = `
},
Object {
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Textarea",
Expand All @@ -197,6 +206,7 @@ exports[`should filter section names 1`] = `
useHashId={true}
useRouterLinks={false}
/>,
"forcedOpen": false,
"heading": true,
"initialOpen": true,
"name": "Forms",
Expand Down Expand Up @@ -225,6 +235,7 @@ exports[`should filter section names 2`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": true,
"heading": false,
"initialOpen": true,
"name": "Forms",
Expand Down Expand Up @@ -253,6 +264,7 @@ exports[`should render a renderer 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Button",
Expand All @@ -264,6 +276,7 @@ exports[`should render a renderer 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Input",
Expand All @@ -275,6 +288,7 @@ exports[`should render a renderer 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Textarea",
Expand Down Expand Up @@ -303,6 +317,7 @@ exports[`should render renderer with sections with nested components 1`] = `
Object {
"components": Array [],
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Introduction",
Expand All @@ -328,6 +343,7 @@ exports[`should render renderer with sections with nested components 1`] = `
Array [
Object {
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Button",
Expand All @@ -340,6 +356,7 @@ exports[`should render renderer with sections with nested components 1`] = `
useHashId={true}
useRouterLinks={false}
/>,
"forcedOpen": false,
"heading": true,
"initialOpen": true,
"name": "Buttons",
Expand Down Expand Up @@ -369,6 +386,7 @@ exports[`should render renderer with sections with nested components 1`] = `
Array [
Object {
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Input",
Expand All @@ -378,6 +396,7 @@ exports[`should render renderer with sections with nested components 1`] = `
},
Object {
"content": undefined,
"forcedOpen": false,
"heading": false,
"initialOpen": true,
"name": "Textarea",
Expand All @@ -390,6 +409,7 @@ exports[`should render renderer with sections with nested components 1`] = `
useHashId={true}
useRouterLinks={false}
/>,
"forcedOpen": false,
"heading": true,
"initialOpen": true,
"name": "Forms",
Expand Down
1 change: 1 addition & 0 deletions src/typings/RsgSection.d.ts
Expand Up @@ -37,6 +37,7 @@ declare namespace Rsg {
shouldOpenInNewTab?: boolean;
selected?: boolean;
initialOpen?: boolean;
forcedOpen?: boolean;
content?: React.ReactNode;
components?: TOCItem[];
sections?: TOCItem[];
Expand Down

0 comments on commit 89420ca

Please sign in to comment.