Skip to content

Commit 4493328

Browse files
authored
feat(fuselage): Modal wrapper & wrapperFunction prop (#959)
* feat: modal-wrapper * chore: wrapperFunction
1 parent a84abb2 commit 4493328

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

packages/fuselage/src/components/Modal/Modal.stories.tsx

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { action } from '@storybook/addon-actions';
2+
import type { ComponentProps } from 'react';
23
import React from 'react';
34

4-
import { Button, Modal } from '../..';
5+
import { FieldGroup, Field, TextInput, Button, Modal, Box } from '../..';
56

67
export default {
78
title: 'Containers/Modal',
@@ -168,3 +169,43 @@ export const _WithHeroImage = () => (
168169
</Modal.Footer>
169170
</Modal>
170171
);
172+
173+
const FormContainer = (props: ComponentProps<typeof Box>) => (
174+
<Box
175+
{...props}
176+
is='form'
177+
onSubmit={(e) => {
178+
e.preventDefault();
179+
action('form submitted')(e);
180+
}}
181+
/>
182+
);
183+
184+
export const WithForm = () => (
185+
<Modal wrapper={FormContainer}>
186+
<Modal.Header>
187+
<Modal.HeaderText>
188+
<Modal.Title>Modal Header</Modal.Title>
189+
</Modal.HeaderText>
190+
<Modal.Close />
191+
</Modal.Header>
192+
<Modal.Content>
193+
<FieldGroup>
194+
<Field>
195+
<Field.Label>Label</Field.Label>
196+
<Field.Row>
197+
<TextInput placeholder='Placeholder' />
198+
</Field.Row>
199+
</Field>
200+
</FieldGroup>
201+
</Modal.Content>
202+
<Modal.Footer>
203+
<Modal.FooterControllers>
204+
<Button>Cancel</Button>
205+
<Button type='submit' primary>
206+
Submit
207+
</Button>
208+
</Modal.FooterControllers>
209+
</Modal.Footer>
210+
</Modal>
211+
);
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
import type { ComponentProps, Ref } from 'react';
2-
import React, { forwardRef } from 'react';
1+
import type { ComponentProps, Ref, ElementType, ReactNode } from 'react';
2+
import React, { createElement, forwardRef } from 'react';
33

44
import Box from '../Box';
55

6-
type ModalProps = ComponentProps<typeof Box>;
6+
type ModalProps = {
7+
wrapperFunction?: (
8+
props: Pick<
9+
ComponentProps<typeof Box>,
10+
'elevation' | 'className' | 'children'
11+
>
12+
) => ReactNode;
13+
wrapper?: ElementType<
14+
Pick<ComponentProps<typeof Box>, 'elevation' | 'className' | 'children'>
15+
>;
16+
} & ComponentProps<typeof Box>;
717

818
export const Modal = forwardRef(
9-
({ children, ...props }: ModalProps, ref: Ref<HTMLDivElement>) => (
10-
<Box is='dialog' rcx-modal {...props}>
11-
<Box ref={ref} rcx-modal__inner elevation='2'>
12-
{children}
19+
(
20+
{ children, wrapper = Box, wrapperFunction, ...props }: ModalProps,
21+
ref: Ref<Element>
22+
) => {
23+
const wrapperProps = {
24+
children,
25+
className: 'rcx-modal__inner',
26+
elevation: '2',
27+
} as const;
28+
29+
return (
30+
<Box is='dialog' rcx-modal ref={ref} {...props}>
31+
{wrapperFunction
32+
? wrapperFunction(wrapperProps)
33+
: createElement(wrapper, wrapperProps)}
1334
</Box>
14-
</Box>
15-
)
35+
);
36+
}
1637
);

0 commit comments

Comments
 (0)