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

chore: Refactor RedirectBox to StudioRedirectBox #13908

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,25 @@
import React from 'react';
import type { Meta, StoryFn } from '@storybook/react';
import { StudioRedirectBox } from './StudioRedirectBox';
import { StudioParagraph } from '../StudioParagraph';

type Story = StoryFn<typeof StudioRedirectBox>;

const meta: Meta = {
title: 'Components/StudioRedirectBox',
component: StudioRedirectBox,
argTypes: {
title: {
control: 'text',
},
},
};

export const Preview: Story = (args): React.ReactElement => <StudioRedirectBox {...args} />;

Preview.args = {
title: 'title',
children: <StudioParagraph size='sm'>Children text</StudioParagraph>,
};

export default meta;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { StudioRedirectBox, type StudioRedirectBoxProps } from './StudioRedirectBox';
import { testRootClassNameAppending } from '../../test-utils/testRootClassNameAppending';

const mockTitle: string = 'title';

const defaultProps: StudioRedirectBoxProps = {
title: mockTitle,
children: <div />,
};

describe('StudioRedirectBox', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ref. previous comment, then we can check that className is applied to root by using the test-utils for that. 😊

it('should render the title and children', () => {
const mockChildrenText: string = 'children';
renderStudioRedirectBox({ children: <p>{mockChildrenText}</p> });

expect(screen.getByText(mockTitle)).toBeInTheDocument();
expect(screen.getByText(mockChildrenText)).toBeInTheDocument();
});

it('Appends given classname to internal classname', () => {
testRootClassNameAppending((className) => renderStudioRedirectBox({ className }));
});
});

const renderStudioRedirectBox = (props: Partial<StudioRedirectBoxProps> = {}) => {
return render(<StudioRedirectBox {...defaultProps} {...props} />);
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import React, { type ReactNode } from 'react';
import React, { type ReactElement, type HTMLAttributes } from 'react';
import cn from 'classnames';
import classes from './RedirectBox.module.css';
import classes from './StudioRedirectBox.module.css';
import { StudioLabelAsParagraph } from '@studio/components';

export type RedirectBoxProps = {
export type StudioRedirectBoxProps = {
title: string;
children: ReactNode;
className?: string;
};
} & HTMLAttributes<HTMLDivElement>;

export const RedirectBox = ({
export const StudioRedirectBox = ({
title,
children,
className: givenClassName,
}: RedirectBoxProps): React.ReactElement => {
}: StudioRedirectBoxProps): ReactElement => {
const className = cn(classes.wrapper, givenClassName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we use HTMLAttributes here? To ensure we stay consistent? Then we can need our "className" prop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree! 😄

return (
<div className={className}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { StudioRedirectBox } from './StudioRedirectBox';
1 change: 1 addition & 0 deletions frontend/libs/studio-components/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export * from './StudioParagraph';
export * from './StudioPopover';
export * from './StudioProperty';
export * from './StudioRecommendedNextAction';
export * from './StudioRedirectBox';
export * from './StudioResizableLayout';
export * from './StudioSectionHeader';
export * from './StudioSpinner';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from 'react';
import { Alert } from '@digdir/designsystemet-react';
import { StudioButton } from '@studio/components';
import { StudioButton, StudioRedirectBox } from '@studio/components';
import { useBpmnApiContext } from '../../../../contexts/BpmnApiContext';
import { useTranslation } from 'react-i18next';
import { ShieldLockIcon } from '@studio/icons';
import classes from './EditPolicy.module.css';
import { RedirectBox } from 'app-shared/components/RedirectBox';

export const EditPolicy = () => {
const { t } = useTranslation();
Expand All @@ -16,7 +15,7 @@ export const EditPolicy = () => {
<Alert severity='info' className={classes.alert}>
{t('process_editor.configuration_panel.edit_policy_alert_message')}
</Alert>
<RedirectBox
<StudioRedirectBox
title={t('process_editor.configuration_panel.edit_policy_open_policy_editor_heading')}
>
<StudioButton
Expand All @@ -28,7 +27,7 @@ export const EditPolicy = () => {
>
{t('process_editor.configuration_panel.edit_policy_open_policy_editor_button')}
</StudioButton>
</RedirectBox>
</StudioRedirectBox>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import classes from './RedirectToCreatePageButton.module.css';
import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams';
import { PackagesRouter } from 'app-shared/navigation/PackagesRouter';
import { PencilWritingIcon } from '@studio/icons';
import { StudioButton } from '@studio/components';
import { StudioButton, StudioRedirectBox } from '@studio/components';
import { useLocalStorage } from '@studio/components/src/hooks/useLocalStorage';
import { useTranslation } from 'react-i18next';
import { useBpmnApiContext } from '../../../../../contexts/BpmnApiContext';
import { RedirectBox } from 'app-shared/components/RedirectBox';
import { Link } from '@digdir/designsystemet-react';

export const RedirectToCreatePageButton = (): React.ReactElement => {
Expand All @@ -24,7 +23,7 @@ export const RedirectToCreatePageButton = (): React.ReactElement => {

return (
<div className={classes.goToCreatePageWrapper}>
<RedirectBox
<StudioRedirectBox
title={t('process_editor.configuration_panel_custom_receipt_navigate_to_design_title')}
>
<StudioButton asChild variant='primary' color='second' onClick={handleClick}>
Expand All @@ -36,7 +35,7 @@ export const RedirectToCreatePageButton = (): React.ReactElement => {
{t('process_editor.configuration_panel_custom_receipt_navigate_to_design_button')}
</Link>
</StudioButton>
</RedirectBox>
</StudioRedirectBox>
</div>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import { useTranslation } from 'react-i18next';
import { PencilIcon } from '@studio/icons';
import { StudioButton } from '@studio/components';
import { RedirectBox } from 'app-shared/components/RedirectBox';
import { StudioButton, StudioRedirectBox } from '@studio/components';
import classes from './RedirectToLayoutSet.module.css';
import { useAppContext } from '../../../../hooks';

Expand All @@ -22,7 +21,7 @@ export const RedirectToLayoutSet = ({
};

return (
<RedirectBox
<StudioRedirectBox
title={t('ux_editor.component_properties.subform.go_to_layout_set')}
className={classes.redirectContainer}
>
Expand All @@ -35,6 +34,6 @@ export const RedirectToLayoutSet = ({
>
{t('top_menu.create')}
</StudioButton>
</RedirectBox>
</StudioRedirectBox>
);
};