Skip to content

Commit 20374bf

Browse files
authored
✨ Add possibility to link to open faq question (#1184)
1 parent 3aba859 commit 20374bf

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

src/organisms/Faq/Category/Question/Question.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { type FC, useState } from 'react';
1+
import { type FC, useRef, useState } from 'react';
22

33
import { Button, Icon, Typography } from '@equinor/eds-core-react';
44
import { chevron_down, chevron_up } from '@equinor/eds-icons';
55
import { type FaqDto, FaqService } from '@equinor/subsurface-app-management';
6+
import { useLocation } from '@tanstack/react-router';
67

78
import {
89
Container,
@@ -24,10 +25,15 @@ export const Question: FC<FaqDto> = ({ id, question, createdDate, answer }) => {
2425
onImageRead: (path) => FaqService.getFaqImage(path),
2526
/* v8 ignore end */
2627
});
27-
const [expanded, setExpanded] = useState(false);
28+
const { hash } = useLocation();
29+
const initialExpanded = useRef(hash === `faq-${id}`);
30+
const [expanded, setExpanded] = useState(initialExpanded.current);
2831

2932
const handleOnToggleExpanded = () => {
3033
setExpanded((prev) => !prev);
34+
if (initialExpanded.current) {
35+
initialExpanded.current = false;
36+
}
3137
};
3238

3339
return (
@@ -56,7 +62,7 @@ export const Question: FC<FaqDto> = ({ id, question, createdDate, answer }) => {
5662
<AnimatePresence>
5763
{expanded && (
5864
<ExpandWrapper
59-
initial={{ height: 0 }}
65+
initial={{ height: initialExpanded.current ? 'auto' : 0 }}
6066
exit={{ height: 0 }}
6167
animate={{ height: 'auto' }}
6268
>

src/organisms/Faq/Faq.stories.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ export const SearchFaqs: Story = {
154154
});
155155

156156
await step('Verify non-matching categories are hidden', async () => {
157-
expect(
157+
await expect(
158158
canvas.queryByRole('heading', { name: 'Working with Data' })
159159
).not.toBeInTheDocument();
160-
expect(
160+
await expect(
161161
canvas.queryByRole('heading', { name: 'Troubleshooting' })
162162
).not.toBeInTheDocument();
163-
expect(
163+
await expect(
164164
canvas.queryByRole('heading', { name: 'Security & Access' })
165165
).not.toBeInTheDocument();
166166
});
@@ -304,7 +304,26 @@ export const OpenQuestion: Story = {
304304
/Navigate to .+ and use your company credentials/,
305305
{ exact: false }
306306
);
307-
expect(answerText).toBeVisible();
307+
await expect(answerText).toBeVisible();
308+
});
309+
},
310+
};
311+
312+
export const InitiallyOpenQuestion: Story = {
313+
tags: ['test-only'],
314+
parameters: {
315+
router: {
316+
initialEntries: ['/faq#faq-1'],
317+
routes: ['/faq'],
318+
},
319+
},
320+
play: async ({ canvas, step }) => {
321+
await step('Verify that the answer is visible on load', async () => {
322+
const answerText = await canvas.findByText(
323+
/Navigate to .+ and use your company credentials/,
324+
{ exact: false }
325+
);
326+
await expect(answerText).toBeVisible();
308327
});
309328
},
310329
};

0 commit comments

Comments
 (0)