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(fuselage): Modal wrapper & wrapperFunction prop #959

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 42 additions & 1 deletion packages/fuselage/src/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { action } from '@storybook/addon-actions';
import type { ComponentProps } from 'react';
import React from 'react';

import { Button, Modal } from '../..';
import { FieldGroup, Field, TextInput, Button, Modal, Box } from '../..';

export default {
title: 'Containers/Modal',
Expand Down Expand Up @@ -168,3 +169,43 @@ export const _WithHeroImage = () => (
</Modal.Footer>
</Modal>
);

const FormContainer = (props: ComponentProps<typeof Box>) => (
<Box
{...props}
is='form'
onSubmit={(e) => {
e.preventDefault();
action('form submitted')(e);
}}
/>
);

export const WithForm = () => (
<Modal wrapper={FormContainer}>
<Modal.Header>
<Modal.HeaderText>
<Modal.Title>Modal Header</Modal.Title>
</Modal.HeaderText>
<Modal.Close />
</Modal.Header>
<Modal.Content>
<FieldGroup>
<Field>
<Field.Label>Label</Field.Label>
<Field.Row>
<TextInput placeholder='Placeholder' />
</Field.Row>
</Field>
</FieldGroup>
</Modal.Content>
<Modal.Footer>
<Modal.FooterControllers>
<Button>Cancel</Button>
<Button type='submit' primary>
Submit
</Button>
</Modal.FooterControllers>
</Modal.Footer>
</Modal>
);
39 changes: 30 additions & 9 deletions packages/fuselage/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import type { ComponentProps, Ref } from 'react';
import React, { forwardRef } from 'react';
import type { ComponentProps, Ref, ElementType, ReactNode } from 'react';
import React, { createElement, forwardRef } from 'react';

import Box from '../Box';

type ModalProps = ComponentProps<typeof Box>;
type ModalProps = {
wrapperFunction?: (
props: Pick<
ComponentProps<typeof Box>,
'elevation' | 'className' | 'children'
>
) => ReactNode;
wrapper?: ElementType<
Pick<ComponentProps<typeof Box>, 'elevation' | 'className' | 'children'>
>;
} & ComponentProps<typeof Box>;

export const Modal = forwardRef(
({ children, ...props }: ModalProps, ref: Ref<HTMLDivElement>) => (
<Box is='dialog' rcx-modal {...props}>
<Box ref={ref} rcx-modal__inner elevation='2'>
{children}
(
{ children, wrapper = Box, wrapperFunction, ...props }: ModalProps,
ref: Ref<Element>
) => {
const wrapperProps = {
children,
className: 'rcx-modal__inner',
elevation: '2',
} as const;

return (
<Box is='dialog' rcx-modal ref={ref} {...props}>
{wrapperFunction
? wrapperFunction(wrapperProps)
: createElement(wrapper, wrapperProps)}
</Box>
</Box>
)
);
}
);