diff --git a/apps/mibao-ui-docs/src/app/pages/confirm.stories.tsx b/apps/mibao-ui-docs/src/app/pages/confirm.stories.tsx new file mode 100644 index 00000000..b7d4842c --- /dev/null +++ b/apps/mibao-ui-docs/src/app/pages/confirm.stories.tsx @@ -0,0 +1,48 @@ +import { Story, Meta } from '@storybook/react' +import React from 'react' +import { + ConfirmProvider, MibaoProvider, useConfirm, Button as MibaoButton, ConfirmProps +} from 'mibao-ui' +import { Box, Code } from '@chakra-ui/react' + +export default { + component: ConfirmProvider, + title: 'Components/Confirm', + argTypes: { + onConfirm: { + action: 'onConfirm' + }, + onCancel: { + action: 'onCancel' + } + } +} as Meta + +const args: ConfirmProps = { + title: '', + content: 'Content Content Content Content Content Content Content Content', + confirmText: 'OK', + cancelText: 'Cancel' +} + +const OpenModalButton: React.FC = (args) => { + const showModal = useConfirm() + return showModal(args)}>Button +} + +const Template: Story = (args) => { + return + + + {'const showModal = useConfirm()'}
+ {'return showModal(args)}>Button'} +
+
+ + + +
+} + +export const Confirm = Template.bind({}) +Confirm.args = args diff --git a/libs/mibao-ui/src/index.ts b/libs/mibao-ui/src/index.ts index d78f7ac0..755b53b5 100644 --- a/libs/mibao-ui/src/index.ts +++ b/libs/mibao-ui/src/index.ts @@ -4,8 +4,8 @@ export * from './lib/select/select' export * from './lib/pagination/pagination' export * from './lib/alert/alert' export * from './lib/toast/toast' +export * from './lib/confirm/confirm' export * from './lib/radio/radio' - export * from './lib/table/table' export * from './lib/button/button' export * from './lib/modal/modal' diff --git a/libs/mibao-ui/src/lib/confirm/confirm.module.scss b/libs/mibao-ui/src/lib/confirm/confirm.module.scss new file mode 100644 index 00000000..e69de29b diff --git a/libs/mibao-ui/src/lib/confirm/confirm.spec.tsx b/libs/mibao-ui/src/lib/confirm/confirm.spec.tsx new file mode 100644 index 00000000..6cd83ad5 --- /dev/null +++ b/libs/mibao-ui/src/lib/confirm/confirm.spec.tsx @@ -0,0 +1,10 @@ +import { render } from '@testing-library/react' + +import { ConfirmProvider } from './confirm' + +describe('Confirm', () => { + it('should render successfully', () => { + const { baseElement } = render() + expect(baseElement).toBeTruthy() + }) +}) diff --git a/libs/mibao-ui/src/lib/confirm/confirm.tsx b/libs/mibao-ui/src/lib/confirm/confirm.tsx new file mode 100644 index 00000000..01e7bcb0 --- /dev/null +++ b/libs/mibao-ui/src/lib/confirm/confirm.tsx @@ -0,0 +1,94 @@ +import './confirm.module.scss' +import React, { ReactNode, createContext, useState, useContext } from 'react' +import { + Modal, + ModalBody, + ModalCloseButton, + ModalContent, + ModalFooter, + ModalFooterButtonGroup, + ModalHeader, + ModalOverlay, + useDisclosure +} from '../modal/modal' +import { Button } from '../button/button' + +export interface ConfirmProps { + title?: ReactNode + content?: ReactNode + onConfirm?: () => void + confirmText?: ReactNode + onCancel?: () => void + cancelText?: ReactNode +} + +const ConfirmContext = createContext<{ props: ConfirmProps | null, showModal: (props: ConfirmProps) => void}>({ + props: null, + showModal (props: ConfirmProps) { + this.props = props + } +}) + +const ConfirmContextProvider = ConfirmContext.Provider + +const ConfirmModal: React.FC<{ + isOpen: boolean + onOpen: () => void + onClose: () => void +}> = (props) => { + const context = useContext(ConfirmContext) + + return + + + {context.props?.title} + + + {context.props?.content} + + + + + + + { + context.props?.onCancel && + } + + + + +} + +export const ConfirmProvider: React.FC = (props) => { + const [modalProps, setModalProps] = useState(null) + const { isOpen, onOpen, onClose } = useDisclosure() + + return ( + { + onOpen() + setModalProps(props) + } + }}> + + {props.children} + + ) +} + +export function useConfirm () { + const context = useContext(ConfirmContext) + return context.showModal +}