-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc1def6
commit e761c2a
Showing
10 changed files
with
125 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
import { FC } from 'react' | ||
import { components, events, renderClassName } from 'fastui' | ||
import { components, events, renderClassName, EventContextProvider } from 'fastui' | ||
import BootstrapModal from 'react-bootstrap/Modal' | ||
|
||
export const Modal: FC<components.ModalProps> = (props) => { | ||
const { className, title, body, footer, openTrigger } = props | ||
const { className, title, body, footer, openTrigger, openContext } = props | ||
|
||
const [open, toggle] = events.useEventListenerToggle(openTrigger, props.open) | ||
const { eventContext, clear } = events.usePageEventListen(openTrigger, openContext) | ||
|
||
return ( | ||
<BootstrapModal className={renderClassName(className)} show={open} onHide={toggle}> | ||
<BootstrapModal.Header closeButton> | ||
<BootstrapModal.Title>{title}</BootstrapModal.Title> | ||
</BootstrapModal.Header> | ||
<BootstrapModal.Body> | ||
<components.AnyCompList propsList={body} /> | ||
</BootstrapModal.Body> | ||
{footer && ( | ||
<BootstrapModal.Footer className="modal-footer"> | ||
<components.AnyCompList propsList={footer} /> | ||
</BootstrapModal.Footer> | ||
)} | ||
</BootstrapModal> | ||
<EventContextProvider context={eventContext}> | ||
<BootstrapModal className={renderClassName(className)} show={!!eventContext} onHide={clear}> | ||
<BootstrapModal.Header closeButton> | ||
<BootstrapModal.Title>{title}</BootstrapModal.Title> | ||
</BootstrapModal.Header> | ||
<BootstrapModal.Body> | ||
<components.AnyCompList propsList={body} /> | ||
</BootstrapModal.Body> | ||
{footer && ( | ||
<BootstrapModal.Footer className="modal-footer"> | ||
<components.AnyCompList propsList={footer} /> | ||
</BootstrapModal.Footer> | ||
)} | ||
</BootstrapModal> | ||
</EventContextProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
import { FC, useEffect } from 'react' | ||
|
||
import { ClassName } from '../hooks/className' | ||
import { PageEvent, useEventListenerToggle } from '../events' | ||
import type { FastProps } from './index' | ||
import type { ContextType } from '../hooks/eventContext' | ||
|
||
import { FastProps } from './index' | ||
import { ClassName } from '../hooks/className' | ||
import { PageEvent, usePageEventListen } from '../events' | ||
|
||
export interface ModalProps { | ||
type: 'Modal' | ||
title: string | ||
body: FastProps[] | ||
footer?: FastProps[] | ||
openTrigger?: PageEvent | ||
open?: boolean | ||
openContext?: ContextType | ||
className?: ClassName | ||
} | ||
|
||
export const ModalComp: FC<ModalProps> = (props) => { | ||
const { title, openTrigger } = props | ||
const { title, openTrigger, openContext } = props | ||
|
||
const [open, toggle] = useEventListenerToggle(openTrigger, props.open) | ||
const { eventContext, clear } = usePageEventListen(openTrigger, openContext) | ||
const open = !!eventContext | ||
|
||
useEffect(() => { | ||
if (open) { | ||
setTimeout(() => { | ||
alert(`${title}\n\nNote: modals are not implemented by pure FastUI, implement a component for 'ModalProps'.`) | ||
toggle() | ||
clear() | ||
}) | ||
} | ||
}, [open, title, toggle]) | ||
}, [open, title, clear]) | ||
|
||
return <></> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createContext, FC, ReactNode, useCallback, useContext } from 'react' | ||
|
||
export type ContextType = Record<string, string | number> | ||
|
||
const EventContext = createContext<ContextType | null>(null) | ||
|
||
export const useEventContext = (): ((template: string) => string) => { | ||
const context = useContext(EventContext) | ||
|
||
return useCallback((template: string): string => applyContext(template, context), [context]) | ||
} | ||
|
||
export const EventContextProvider: FC<{ children: ReactNode; context: ContextType | null }> = ({ | ||
children, | ||
context, | ||
}) => { | ||
return <EventContext.Provider value={context}>{children}</EventContext.Provider> | ||
} | ||
|
||
const applyContext = (template: string, context: ContextType | null): string => { | ||
if (!context) { | ||
return template | ||
} | ||
|
||
return template.replace(/{(.+?)}/g, (_, key: string): string => { | ||
const v = context[key] | ||
if (v === undefined) { | ||
throw new Error(`field "${key}" not found in ${JSON.stringify(context)}`) | ||
} else { | ||
return v.toString() | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters