@kirill.konshin/bootstrap 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ctirc +11 -0
- package/.storybook/main.ts +2 -0
- package/.storybook/preview.ts +2 -0
- package/.turbo/turbo-build.log +24 -0
- package/dist/adaptiveContainer.d.ts +4 -0
- package/dist/adaptiveContainer.d.ts.map +1 -0
- package/dist/adaptiveContainer.js +11 -0
- package/dist/adaptiveContainer.js.map +1 -0
- package/dist/controls.d.ts +38 -0
- package/dist/controls.d.ts.map +1 -0
- package/dist/controls.js +78 -0
- package/dist/controls.js.map +1 -0
- package/dist/controls.stories.d.ts +9 -0
- package/dist/controls.stories.d.ts.map +1 -0
- package/dist/error.d.ts +9 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +27 -0
- package/dist/error.js.map +1 -0
- package/dist/error.stories.d.ts +8 -0
- package/dist/error.stories.d.ts.map +1 -0
- package/dist/field.d.ts +11 -0
- package/dist/field.d.ts.map +1 -0
- package/dist/field.js +29 -0
- package/dist/field.js.map +1 -0
- package/dist/field.stories.d.ts +8 -0
- package/dist/field.stories.d.ts.map +1 -0
- package/dist/footer.d.ts +10 -0
- package/dist/footer.d.ts.map +1 -0
- package/dist/footer.js +45 -0
- package/dist/footer.js.map +1 -0
- package/dist/globalLoading.d.ts +6 -0
- package/dist/globalLoading.d.ts.map +1 -0
- package/dist/globalLoading.js +10 -0
- package/dist/globalLoading.js.map +1 -0
- package/dist/globalLoading.stories.d.ts +7 -0
- package/dist/globalLoading.stories.d.ts.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/loading.d.ts +10 -0
- package/dist/loading.d.ts.map +1 -0
- package/dist/loading.js +19 -0
- package/dist/loading.js.map +1 -0
- package/dist/loading.stories.d.ts +9 -0
- package/dist/loading.stories.d.ts.map +1 -0
- package/dist/responsiveHelper.d.ts +8 -0
- package/dist/responsiveHelper.d.ts.map +1 -0
- package/dist/responsiveHelper.js +46 -0
- package/dist/responsiveHelper.js.map +1 -0
- package/dist/screen.d.ts +22 -0
- package/dist/screen.d.ts.map +1 -0
- package/dist/screen.js +80 -0
- package/dist/screen.js.map +1 -0
- package/dist/screen.stories.d.ts +14 -0
- package/dist/screen.stories.d.ts.map +1 -0
- package/dist/toaster.d.ts +11 -0
- package/dist/toaster.d.ts.map +1 -0
- package/dist/toaster.js +34 -0
- package/dist/toaster.js.map +1 -0
- package/dist/toaster.stories.d.ts +8 -0
- package/dist/toaster.stories.d.ts.map +1 -0
- package/dist/useModal.d.ts +23 -0
- package/dist/useModal.d.ts.map +1 -0
- package/dist/useModal.js +50 -0
- package/dist/useModal.js.map +1 -0
- package/dist/useModal.stories.d.ts +9 -0
- package/dist/useModal.stories.d.ts.map +1 -0
- package/dist/useWrappedForm.d.ts +14 -0
- package/dist/useWrappedForm.d.ts.map +1 -0
- package/dist/useWrappedForm.js +32 -0
- package/dist/useWrappedForm.js.map +1 -0
- package/package.json +68 -0
- package/src/adaptiveContainer.tsx +12 -0
- package/src/controls.stories.tsx +62 -0
- package/src/controls.tsx +119 -0
- package/src/error.stories.tsx +34 -0
- package/src/error.tsx +37 -0
- package/src/field.stories.tsx +42 -0
- package/src/field.tsx +37 -0
- package/src/footer.tsx +61 -0
- package/src/globalLoading.stories.tsx +25 -0
- package/src/globalLoading.tsx +19 -0
- package/src/index.ts +12 -0
- package/src/loading.stories.tsx +35 -0
- package/src/loading.tsx +24 -0
- package/src/main.scss +86 -0
- package/src/responsiveHelper.tsx +55 -0
- package/src/screen.stories.tsx +160 -0
- package/src/screen.tsx +146 -0
- package/src/toaster.stories.tsx +32 -0
- package/src/toaster.tsx +40 -0
- package/src/useModal.stories.tsx +51 -0
- package/src/useModal.tsx +93 -0
- package/src/useWrappedForm.ts +42 -0
- package/tsconfig.json +10 -0
- package/turbo.json +10 -0
- package/vite.config.ts +2 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
3
|
+
import { fn } from '@storybook/test';
|
|
4
|
+
|
|
5
|
+
import { Screen } from './screen';
|
|
6
|
+
import { Loading } from './loading';
|
|
7
|
+
import { Dropdown, DropdownButton, Form, Nav, Navbar } from 'react-bootstrap';
|
|
8
|
+
import { Footer, FooterNavItem } from './footer';
|
|
9
|
+
import { AdaptiveContainer } from './adaptiveContainer';
|
|
10
|
+
|
|
11
|
+
const lorem = (
|
|
12
|
+
<>
|
|
13
|
+
<p>
|
|
14
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
|
|
15
|
+
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
|
|
16
|
+
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
|
|
17
|
+
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
|
|
18
|
+
anim id est laborum.
|
|
19
|
+
</p>
|
|
20
|
+
<h2>Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC</h2>
|
|
21
|
+
<p>
|
|
22
|
+
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem
|
|
23
|
+
aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
|
|
24
|
+
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni
|
|
25
|
+
dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor
|
|
26
|
+
sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore
|
|
27
|
+
magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis
|
|
28
|
+
suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in
|
|
29
|
+
ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas
|
|
30
|
+
nulla pariatur?
|
|
31
|
+
</p>
|
|
32
|
+
<h2>1914 translation by H. Rackham</h2>
|
|
33
|
+
<p>
|
|
34
|
+
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I
|
|
35
|
+
will give you a complete account of the system, and expound the actual teachings of the great explorer of
|
|
36
|
+
the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself,
|
|
37
|
+
because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter
|
|
38
|
+
consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain
|
|
39
|
+
pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can
|
|
40
|
+
procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical
|
|
41
|
+
exercise, except to obtain some advantage from it? But who has any right to find fault with a man who
|
|
42
|
+
chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no
|
|
43
|
+
resultant pleasure?
|
|
44
|
+
</p>
|
|
45
|
+
</>
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const meta: Meta<typeof Screen> = {
|
|
49
|
+
title: 'Bootstrap / Screen',
|
|
50
|
+
component: Screen,
|
|
51
|
+
parameters: {
|
|
52
|
+
layout: 'fullscreen',
|
|
53
|
+
},
|
|
54
|
+
tags: ['autodocs'],
|
|
55
|
+
argTypes: {
|
|
56
|
+
children: { control: 'text' },
|
|
57
|
+
title: { control: 'text' },
|
|
58
|
+
footer: { control: 'text' },
|
|
59
|
+
},
|
|
60
|
+
args: {
|
|
61
|
+
children: lorem,
|
|
62
|
+
title: 'Title',
|
|
63
|
+
menu: (
|
|
64
|
+
<Footer>
|
|
65
|
+
<FooterNavItem href="/application/process" icon="bi-cup-hot-fill" active>
|
|
66
|
+
Coffee
|
|
67
|
+
</FooterNavItem>
|
|
68
|
+
<FooterNavItem href="/application/transcribe" icon="bi-pen-fill">
|
|
69
|
+
Assistant
|
|
70
|
+
</FooterNavItem>
|
|
71
|
+
<FooterNavItem href="/application/documents" icon="bi-star-fill">
|
|
72
|
+
Docs
|
|
73
|
+
</FooterNavItem>
|
|
74
|
+
</Footer>
|
|
75
|
+
),
|
|
76
|
+
backCb: fn(),
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export default meta;
|
|
81
|
+
|
|
82
|
+
type Story = StoryObj<typeof meta>;
|
|
83
|
+
|
|
84
|
+
export const Default: Story = {};
|
|
85
|
+
|
|
86
|
+
export const LongContent: Story = {
|
|
87
|
+
args: {
|
|
88
|
+
header: <AdaptiveContainer>Header</AdaptiveContainer>,
|
|
89
|
+
footer: <div>Footer</div>,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const Loader: Story = {
|
|
94
|
+
parameters: {
|
|
95
|
+
docs: {
|
|
96
|
+
description: {
|
|
97
|
+
story: 'Centered content with hidden header and footer',
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
args: {
|
|
102
|
+
children: <Loading />,
|
|
103
|
+
centerContent: true,
|
|
104
|
+
noHeader: true,
|
|
105
|
+
noFooter: true,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const Centered: Story = {
|
|
110
|
+
args: {
|
|
111
|
+
children: 'Nothing to see here',
|
|
112
|
+
centerContent: true,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const ForwardCb: Story = {
|
|
117
|
+
args: {
|
|
118
|
+
forwardCb: fn(),
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export const ForwardIcon: Story = {
|
|
123
|
+
args: {
|
|
124
|
+
forwardCb: fn(),
|
|
125
|
+
forwardIcon: 'bi-trash',
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export const ForwardBtn: Story = {
|
|
130
|
+
args: {
|
|
131
|
+
forwardBtn: (
|
|
132
|
+
<DropdownButton
|
|
133
|
+
title={<span className="bi-three-dots" />}
|
|
134
|
+
// size="sm"
|
|
135
|
+
align="end"
|
|
136
|
+
variant="nav"
|
|
137
|
+
className={`dropdown-toggle-no-arrow`}
|
|
138
|
+
>
|
|
139
|
+
<Dropdown.Item>Preview</Dropdown.Item>
|
|
140
|
+
<Dropdown.Divider />
|
|
141
|
+
<Dropdown.Item className="text-danger">Remove</Dropdown.Item>
|
|
142
|
+
</DropdownButton>
|
|
143
|
+
),
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export const HeaderForm: Story = {
|
|
148
|
+
args: {
|
|
149
|
+
header: (
|
|
150
|
+
<Navbar className="pt-0 py-lg-3">
|
|
151
|
+
<AdaptiveContainer>
|
|
152
|
+
<div className="position-relative w-100">
|
|
153
|
+
<span className="bi-search position-absolute" style={{ right: '8px', top: '3px' }} />
|
|
154
|
+
<Form.Control placeholder="Search" size="sm" />
|
|
155
|
+
</div>
|
|
156
|
+
</AdaptiveContainer>
|
|
157
|
+
</Navbar>
|
|
158
|
+
),
|
|
159
|
+
},
|
|
160
|
+
};
|
package/src/screen.tsx
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import React, { FC, memo } from 'react';
|
|
2
|
+
import { Button, Navbar, Nav, Stack } from 'react-bootstrap';
|
|
3
|
+
import { useBreakpoint } from './responsiveHelper';
|
|
4
|
+
import { AdaptiveContainer } from './adaptiveContainer';
|
|
5
|
+
|
|
6
|
+
export type ScreenProps = {
|
|
7
|
+
backBtn?: any;
|
|
8
|
+
backCb?: any;
|
|
9
|
+
backIcon?: any;
|
|
10
|
+
forwardBtn?: any;
|
|
11
|
+
forwardIcon?: any;
|
|
12
|
+
forwardCb?: any;
|
|
13
|
+
children: any;
|
|
14
|
+
contentClassName?: string;
|
|
15
|
+
noContentPadding?: boolean;
|
|
16
|
+
header?: any; // resides on top panel in mobile, does not scroll, before content on desktop
|
|
17
|
+
footer?: any; // resides on bottom menu panel in mobile, does not scroll, or after content on desktop
|
|
18
|
+
menu?: any;
|
|
19
|
+
title?: any;
|
|
20
|
+
centerContent?: boolean;
|
|
21
|
+
noHeader?: boolean;
|
|
22
|
+
noFooter?: boolean;
|
|
23
|
+
noMenu?: boolean;
|
|
24
|
+
// noFolders?: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//TODO Footer -> menu
|
|
28
|
+
export const Screen: FC<ScreenProps> = memo(function Screen({
|
|
29
|
+
backBtn,
|
|
30
|
+
backCb,
|
|
31
|
+
backIcon,
|
|
32
|
+
forwardBtn,
|
|
33
|
+
forwardIcon,
|
|
34
|
+
forwardCb,
|
|
35
|
+
footer,
|
|
36
|
+
menu,
|
|
37
|
+
header,
|
|
38
|
+
children,
|
|
39
|
+
noContentPadding = false,
|
|
40
|
+
contentClassName = '',
|
|
41
|
+
title = '',
|
|
42
|
+
centerContent = false,
|
|
43
|
+
noHeader = false,
|
|
44
|
+
noFooter = false,
|
|
45
|
+
noMenu = false,
|
|
46
|
+
// noFolders = false,
|
|
47
|
+
}) {
|
|
48
|
+
// const router = useRouter();
|
|
49
|
+
|
|
50
|
+
backBtn =
|
|
51
|
+
backBtn ||
|
|
52
|
+
(backCb ? (
|
|
53
|
+
// backCb === true ? () => router.back() : backCb
|
|
54
|
+
<Button onClick={backCb} variant="nav">
|
|
55
|
+
{
|
|
56
|
+
<>
|
|
57
|
+
<span className={backIcon || 'bi-chevron-left icon-thick'} />
|
|
58
|
+
<span className="d-none d-lg-inline ms-2">Back</span>
|
|
59
|
+
</>
|
|
60
|
+
}
|
|
61
|
+
</Button>
|
|
62
|
+
) : null);
|
|
63
|
+
|
|
64
|
+
forwardBtn =
|
|
65
|
+
forwardBtn ||
|
|
66
|
+
(forwardCb ? (
|
|
67
|
+
<Button onClick={forwardCb}>{forwardIcon ? <span className={forwardIcon}></span> : 'Next'}</Button>
|
|
68
|
+
) : null);
|
|
69
|
+
|
|
70
|
+
const { isMobile, isDesktop } = useBreakpoint();
|
|
71
|
+
|
|
72
|
+
// useLoadingTimeout(1000); // less jerky UI
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<div className={`d-flex flex-column flex-grow-1 flex-lg-grow-0 ${isMobile ? 'vh-100' : ''}`} data-id="screen">
|
|
76
|
+
{/* HEADER */}
|
|
77
|
+
|
|
78
|
+
{/* FIXME postition fixed does not assign z-index automatically https://getbootstrap.com/docs/4.0/layout/overview/#z-index*/}
|
|
79
|
+
{!noHeader && (
|
|
80
|
+
<header className={isDesktop ? '' : 'shadow-sm border-bottom'} data-id="header">
|
|
81
|
+
{/* {!noFolders && <FolderBar />} */}
|
|
82
|
+
|
|
83
|
+
{/* {isDesktop && !noFooter && <Footer />} */}
|
|
84
|
+
{isDesktop && !noMenu && menu}
|
|
85
|
+
|
|
86
|
+
{title && (
|
|
87
|
+
// (isDesktop ? 'border-bottom' : '')
|
|
88
|
+
<Navbar expand className="main-nav">
|
|
89
|
+
<AdaptiveContainer className="gap-2">
|
|
90
|
+
{backBtn && <Nav className="hstack gap-3">{backBtn}</Nav>}
|
|
91
|
+
<Navbar.Brand className="me-auto">{title}</Navbar.Brand>
|
|
92
|
+
{forwardBtn && <Nav className="hstack gap-3">{forwardBtn}</Nav>}
|
|
93
|
+
</AdaptiveContainer>
|
|
94
|
+
</Navbar>
|
|
95
|
+
)}
|
|
96
|
+
|
|
97
|
+
{header}
|
|
98
|
+
</header>
|
|
99
|
+
)}
|
|
100
|
+
|
|
101
|
+
{/* CONTENT */}
|
|
102
|
+
|
|
103
|
+
<AdaptiveContainer
|
|
104
|
+
as="main"
|
|
105
|
+
className={[
|
|
106
|
+
isMobile ? 'overflow-y-auto overflow-x-hidden' : 'gap-3',
|
|
107
|
+
`flex-grow-1 d-flex flex-column`,
|
|
108
|
+
centerContent ? 'align-items-center justify-content-center' : 'justify-content-start',
|
|
109
|
+
contentClassName,
|
|
110
|
+
noContentPadding ? 'p-0' : 'py-3',
|
|
111
|
+
]
|
|
112
|
+
.filter(Boolean)
|
|
113
|
+
.join(' ')}
|
|
114
|
+
data-id="content"
|
|
115
|
+
>
|
|
116
|
+
{children}
|
|
117
|
+
|
|
118
|
+
{/* Repeat screen buttons at the bottom on desktop */}
|
|
119
|
+
{isDesktop && !noFooter && (
|
|
120
|
+
<>
|
|
121
|
+
{footer}
|
|
122
|
+
|
|
123
|
+
{/* TODO Possibly introduce a prop desktopFooterBeforeButtons */}
|
|
124
|
+
{(forwardBtn || backBtn) && (
|
|
125
|
+
// Navbar screws up the popper
|
|
126
|
+
<Stack gap={3} direction="horizontal">
|
|
127
|
+
{/* No container since already in container */}
|
|
128
|
+
{backBtn && <Nav className="hstack gap-3">{backBtn}</Nav>}
|
|
129
|
+
{forwardBtn && <Nav className="hstack gap-3 flex-grow-1">{forwardBtn}</Nav>}
|
|
130
|
+
</Stack>
|
|
131
|
+
)}
|
|
132
|
+
</>
|
|
133
|
+
)}
|
|
134
|
+
</AdaptiveContainer>
|
|
135
|
+
|
|
136
|
+
{/* FOOTER */}
|
|
137
|
+
|
|
138
|
+
{!noMenu && !noFooter && (
|
|
139
|
+
<footer className="shadow-lg border-top d-block d-lg-none" data-id="footer">
|
|
140
|
+
{!noFooter && footer}
|
|
141
|
+
{!noMenu && menu}
|
|
142
|
+
</footer>
|
|
143
|
+
)}
|
|
144
|
+
</div>
|
|
145
|
+
);
|
|
146
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { fn } from '@storybook/test';
|
|
3
|
+
|
|
4
|
+
import { Toaster } from './toaster';
|
|
5
|
+
|
|
6
|
+
// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
|
|
7
|
+
const meta: Meta<typeof Toaster> = {
|
|
8
|
+
title: 'Bootstrap / Toaster',
|
|
9
|
+
component: Toaster,
|
|
10
|
+
parameters: {
|
|
11
|
+
layout: 'centered',
|
|
12
|
+
},
|
|
13
|
+
tags: ['autodocs'],
|
|
14
|
+
argTypes: {},
|
|
15
|
+
args: { onClick: fn() },
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default meta;
|
|
19
|
+
|
|
20
|
+
type Story = StoryObj<typeof meta>;
|
|
21
|
+
|
|
22
|
+
export const Default: Story = {
|
|
23
|
+
args: {
|
|
24
|
+
toast: 'Hello, World!',
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const Object: Story = {
|
|
29
|
+
args: {
|
|
30
|
+
toast: { message: 'Toast', variant: 'dark' },
|
|
31
|
+
},
|
|
32
|
+
};
|
package/src/toaster.tsx
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, { useState, FC } from 'react';
|
|
4
|
+
import { Toast, ToastProps } from 'react-bootstrap';
|
|
5
|
+
// import { useToaster } from '../redux/toasterSlice';
|
|
6
|
+
|
|
7
|
+
export type ToasterProps = {
|
|
8
|
+
toast: string | { message: string; title?: string; variant?: 'dark' | 'success' | 'danger' };
|
|
9
|
+
} & ToastProps;
|
|
10
|
+
|
|
11
|
+
export const Toaster: FC<ToasterProps> = function Toaster({ toast }) {
|
|
12
|
+
//const { toast, setToast } = useToaster();
|
|
13
|
+
const [dismissed, setDismissed] = useState(false);
|
|
14
|
+
|
|
15
|
+
const msg = typeof toast === 'string' ? toast : toast?.message;
|
|
16
|
+
// const title = typeof toast === 'string' ? 'Notification' : toast?.title;
|
|
17
|
+
const variant = typeof toast === 'string' ? '' : toast?.variant;
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<Toast
|
|
21
|
+
onClose={() => setDismissed(true)}
|
|
22
|
+
show={!dismissed}
|
|
23
|
+
delay={10000}
|
|
24
|
+
autohide
|
|
25
|
+
bg={variant?.toLowerCase()}
|
|
26
|
+
className="position-fixed top-50 right-50 min-w-200px z-3"
|
|
27
|
+
>
|
|
28
|
+
<Toast.Header>
|
|
29
|
+
<strong className="me-auto">Notification</strong>
|
|
30
|
+
</Toast.Header>
|
|
31
|
+
<Toast.Body
|
|
32
|
+
className={
|
|
33
|
+
['dark', 'success', 'danger'].includes((variant as string)?.toLowerCase()) ? 'text-white' : ''
|
|
34
|
+
}
|
|
35
|
+
>
|
|
36
|
+
{msg}
|
|
37
|
+
</Toast.Body>
|
|
38
|
+
</Toast>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import React, { FC } from 'react';
|
|
2
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
3
|
+
import { useModal, UseModalProps } from './useModal';
|
|
4
|
+
import { Button } from 'react-bootstrap';
|
|
5
|
+
|
|
6
|
+
// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
|
|
7
|
+
const meta: Meta<FC<UseModalProps>> = {
|
|
8
|
+
title: 'Bootstrap / useModal',
|
|
9
|
+
parameters: {
|
|
10
|
+
layout: 'centered',
|
|
11
|
+
},
|
|
12
|
+
tags: ['autodocs'],
|
|
13
|
+
argTypes: {
|
|
14
|
+
children: { control: 'text' },
|
|
15
|
+
title: { control: 'text' },
|
|
16
|
+
cancelButton: { control: 'text' },
|
|
17
|
+
submitButton: { control: 'text' },
|
|
18
|
+
offcanvas: { control: 'check' },
|
|
19
|
+
},
|
|
20
|
+
args: {
|
|
21
|
+
children: 'Modal Content',
|
|
22
|
+
title: 'Modal Title',
|
|
23
|
+
},
|
|
24
|
+
render: function Demo(args) {
|
|
25
|
+
const { open, show, close, ModalDialog } = useModal();
|
|
26
|
+
return (
|
|
27
|
+
<>
|
|
28
|
+
<Button onClick={open}>Open Modal</Button>
|
|
29
|
+
<ModalDialog {...args} show={show}>
|
|
30
|
+
{args.children}
|
|
31
|
+
<Button onClick={close}>Close Modal</Button>
|
|
32
|
+
</ModalDialog>
|
|
33
|
+
</>
|
|
34
|
+
);
|
|
35
|
+
},
|
|
36
|
+
// args: { onSubmit: (e) => e.preventDefault() },
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default meta;
|
|
40
|
+
|
|
41
|
+
type Story = StoryObj<typeof meta>;
|
|
42
|
+
|
|
43
|
+
export const Default: Story = {
|
|
44
|
+
args: {},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const Offcanvas: Story = {
|
|
48
|
+
args: {
|
|
49
|
+
offcanvas: true,
|
|
50
|
+
},
|
|
51
|
+
};
|
package/src/useModal.tsx
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useState, JSX, FC, memo } from 'react';
|
|
2
|
+
import { Button, Modal, ModalProps, Offcanvas, OffcanvasProps, Stack } from 'react-bootstrap';
|
|
3
|
+
|
|
4
|
+
export type UseModalArgs = { onClose?: any; showOnMount?: boolean };
|
|
5
|
+
|
|
6
|
+
export type UseModalProps = {
|
|
7
|
+
children?: any;
|
|
8
|
+
show?: boolean;
|
|
9
|
+
onSubmit?: (e: any) => void;
|
|
10
|
+
submitButton?: string | any;
|
|
11
|
+
cancelButton?: string | any;
|
|
12
|
+
offcanvas?: boolean;
|
|
13
|
+
title?: string | any;
|
|
14
|
+
containerProps?: ModalProps | OffcanvasProps;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function useModal({ onClose, showOnMount }: UseModalArgs = {}): {
|
|
18
|
+
show: boolean | undefined;
|
|
19
|
+
close: () => void;
|
|
20
|
+
open: () => void;
|
|
21
|
+
ModalDialog: FC<ModalProps>;
|
|
22
|
+
} {
|
|
23
|
+
const [show, setShow] = useState(showOnMount);
|
|
24
|
+
|
|
25
|
+
const close = useCallback((): void => {
|
|
26
|
+
setShow(false);
|
|
27
|
+
onClose?.();
|
|
28
|
+
}, [onClose]);
|
|
29
|
+
|
|
30
|
+
const open = useCallback((): void => setShow(true), []);
|
|
31
|
+
|
|
32
|
+
const defaultOnSubmit = useCallback(
|
|
33
|
+
(e: any) => {
|
|
34
|
+
e.preventDefault();
|
|
35
|
+
close();
|
|
36
|
+
},
|
|
37
|
+
[close],
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const ModalDialog = useMemo(
|
|
41
|
+
() =>
|
|
42
|
+
memo(function ModalDialog({
|
|
43
|
+
children,
|
|
44
|
+
show,
|
|
45
|
+
onSubmit = defaultOnSubmit,
|
|
46
|
+
submitButton = 'Done',
|
|
47
|
+
cancelButton = 'Cancel',
|
|
48
|
+
offcanvas = false,
|
|
49
|
+
title = null as any,
|
|
50
|
+
containerProps = {},
|
|
51
|
+
}: ModalProps) {
|
|
52
|
+
const Container = offcanvas ? Offcanvas : Modal;
|
|
53
|
+
const containerDefaultProps = offcanvas ? { style: { maxWidth: '85%' } } : {};
|
|
54
|
+
const Body = offcanvas ? Offcanvas.Body : Modal.Body;
|
|
55
|
+
const Header = offcanvas ? Offcanvas.Header : Modal.Header;
|
|
56
|
+
const Title = offcanvas ? Offcanvas.Title : Modal.Title;
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<Container {...{ ...containerDefaultProps, ...containerProps }} show={show} onHide={close}>
|
|
60
|
+
<form onSubmit={onSubmit}>
|
|
61
|
+
{title && (
|
|
62
|
+
<Header closeButton>
|
|
63
|
+
<Title>{title}</Title>
|
|
64
|
+
</Header>
|
|
65
|
+
)}
|
|
66
|
+
|
|
67
|
+
<Body as={Stack} gap={3}>
|
|
68
|
+
{show && children}
|
|
69
|
+
</Body>
|
|
70
|
+
|
|
71
|
+
{!offcanvas && (submitButton || cancelButton) && (
|
|
72
|
+
<Modal.Footer>
|
|
73
|
+
{cancelButton && (
|
|
74
|
+
<Button variant="outline-secondary" onClick={close}>
|
|
75
|
+
{cancelButton}
|
|
76
|
+
</Button>
|
|
77
|
+
)}
|
|
78
|
+
{submitButton && (
|
|
79
|
+
<Button variant="secondary" type="submit">
|
|
80
|
+
{submitButton}
|
|
81
|
+
</Button>
|
|
82
|
+
)}
|
|
83
|
+
</Modal.Footer>
|
|
84
|
+
)}
|
|
85
|
+
</form>
|
|
86
|
+
</Container>
|
|
87
|
+
);
|
|
88
|
+
}),
|
|
89
|
+
[close, defaultOnSubmit],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return { show, close, open, ModalDialog };
|
|
93
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useForm, FormState } from 'react-hook-form';
|
|
2
|
+
import { useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
export const isFormLoading = (formState: FormState<any>): boolean => formState.isLoading || formState.isSubmitting;
|
|
5
|
+
|
|
6
|
+
export const useWrappedForm: typeof useForm = (options) => {
|
|
7
|
+
const form = useForm(options);
|
|
8
|
+
|
|
9
|
+
const handleSubmit: typeof form.handleSubmit = useCallback(
|
|
10
|
+
(onSubmit, onError) => {
|
|
11
|
+
const wrappedSubmit: typeof onSubmit = async (data) => {
|
|
12
|
+
try {
|
|
13
|
+
await onSubmit(data);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
form.setError('root', { type: 'server', message: (e as Error).message });
|
|
16
|
+
console.error('Error submitting form', e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
return form.handleSubmit(wrappedSubmit, onError);
|
|
20
|
+
},
|
|
21
|
+
[form],
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return { ...form, handleSubmit };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Issue #3: Submit event still propagates to parent when using portal
|
|
29
|
+
* https://reactjs.org/docs/portals.html#event-bubbling-through-portals
|
|
30
|
+
* Event bubbling goes through React DOM instead of HTML DOM
|
|
31
|
+
* Portals don't have an effect on this one, we need to stop event propagation
|
|
32
|
+
* This should be our default form handling method
|
|
33
|
+
* https://codesandbox.io/s/react-hook-form-nested-portal-6x3fvy?file=/src/App.tsx:1905-2261
|
|
34
|
+
* https://github.com/react-hook-form/react-hook-form/issues/1005#issuecomment-988380981
|
|
35
|
+
*/
|
|
36
|
+
export const stopPropagation =
|
|
37
|
+
(callback: any) =>
|
|
38
|
+
(e: Event): void => {
|
|
39
|
+
e.stopPropagation();
|
|
40
|
+
e.preventDefault();
|
|
41
|
+
callback(e);
|
|
42
|
+
};
|
package/tsconfig.json
ADDED
package/turbo.json
ADDED
package/vite.config.ts
ADDED