Refactor overlay technology to not use redux store
parent
f4ab45d420
commit
b76e2be6c1
|
@ -2,7 +2,6 @@ import React, {SFC, ReactChildren} from 'react'
|
||||||
|
|
||||||
import SideNav from 'src/side_nav'
|
import SideNav from 'src/side_nav'
|
||||||
import Notifications from 'src/shared/components/Notifications'
|
import Notifications from 'src/shared/components/Notifications'
|
||||||
import Overlay from 'src/shared/components/OverlayTechnology'
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
children: ReactChildren
|
children: ReactChildren
|
||||||
|
@ -10,7 +9,6 @@ interface Props {
|
||||||
|
|
||||||
const App: SFC<Props> = ({children}) => (
|
const App: SFC<Props> = ({children}) => (
|
||||||
<div className="chronograf-root">
|
<div className="chronograf-root">
|
||||||
<Overlay />
|
|
||||||
<Notifications />
|
<Notifications />
|
||||||
<SideNav />
|
<SideNav />
|
||||||
{children}
|
{children}
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
import {ReactElement} from 'react'
|
|
||||||
|
|
||||||
type OverlayNodeType = ReactElement<any>
|
|
||||||
|
|
||||||
interface Options {
|
|
||||||
dismissOnClickOutside?: boolean
|
|
||||||
dismissOnEscape?: boolean
|
|
||||||
transitionTime?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ShowOverlayActionCreator = (
|
|
||||||
OverlayNode: OverlayNodeType,
|
|
||||||
options: Options
|
|
||||||
) => ShowOverlayAction
|
|
||||||
|
|
||||||
interface ShowOverlayAction {
|
|
||||||
type: 'SHOW_OVERLAY'
|
|
||||||
payload: {
|
|
||||||
OverlayNode
|
|
||||||
options
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const showOverlay = (
|
|
||||||
OverlayNode: OverlayNodeType,
|
|
||||||
options: Options
|
|
||||||
): ShowOverlayAction => ({
|
|
||||||
type: 'SHOW_OVERLAY',
|
|
||||||
payload: {OverlayNode, options},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const dismissOverlay = () => ({
|
|
||||||
type: 'DISMISS_OVERLAY',
|
|
||||||
})
|
|
|
@ -1,104 +1,74 @@
|
||||||
import React, {PureComponent, Component} from 'react'
|
import React, {Component} from 'react'
|
||||||
import {connect} from 'react-redux'
|
|
||||||
import {ErrorHandling} from 'src/shared/decorators/errors'
|
import {ErrorHandling} from 'src/shared/decorators/errors'
|
||||||
|
|
||||||
import {dismissOverlay} from 'src/shared/actions/overlayTechnology'
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
OverlayNode?: Component<any>
|
children: JSX.Element
|
||||||
dismissOnClickOutside?: boolean
|
|
||||||
dismissOnEscape?: boolean
|
|
||||||
transitionTime?: number
|
|
||||||
handleDismissOverlay: () => void
|
|
||||||
}
|
|
||||||
|
|
||||||
interface State {
|
|
||||||
visible: boolean
|
visible: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const OverlayContext = React.createContext()
|
interface State {
|
||||||
|
showChildren: boolean
|
||||||
@ErrorHandling
|
|
||||||
class Overlay extends PureComponent<Props, State> {
|
|
||||||
public static defaultProps: Partial<Props> = {
|
|
||||||
dismissOnClickOutside: false,
|
|
||||||
dismissOnEscape: false,
|
|
||||||
transitionTime: 300,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ErrorHandling
|
||||||
|
class OverlayTechnology extends Component<Props, State> {
|
||||||
private animationTimer: number
|
private animationTimer: number
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props: Props) {
|
||||||
super(props)
|
super(props)
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
visible: false,
|
showChildren: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public componentDidUpdate(prevProps) {
|
public componentDidUpdate(prevProps) {
|
||||||
if (prevProps.OverlayNode === null && this.props.OverlayNode) {
|
if (prevProps.visible === true && this.props.visible === false) {
|
||||||
return this.setState({visible: true})
|
this.animationTimer = window.setTimeout(this.hideChildren, 300)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (prevProps.visible === false && this.props.visible === true) {
|
||||||
|
this.showChildren()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
public render() {
|
public render() {
|
||||||
const {OverlayNode} = this.props
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<OverlayContext.Provider
|
|
||||||
value={{
|
|
||||||
onDismissOverlay: this.handleAnimateDismiss,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div className={this.overlayClass}>
|
<div className={this.overlayClass}>
|
||||||
<div className="overlay--dialog">{OverlayNode}</div>
|
{this.childContainer}
|
||||||
<div className="overlay--mask" onClick={this.handleClickOutside} />
|
<div className="overlay--mask" />
|
||||||
</div>
|
</div>
|
||||||
</OverlayContext.Provider>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private get childContainer(): JSX.Element {
|
||||||
|
const {children} = this.props
|
||||||
|
const {showChildren} = this.state
|
||||||
|
|
||||||
|
if (showChildren) {
|
||||||
|
return <div className="overlay--dialog">{children}</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
return <div className="overlay--dialog" />
|
||||||
|
}
|
||||||
|
|
||||||
private get overlayClass(): string {
|
private get overlayClass(): string {
|
||||||
const {visible} = this.state
|
const {visible} = this.props
|
||||||
return `overlay-tech ${visible ? 'show' : ''}`
|
return `overlay-tech ${visible ? 'show' : ''}`
|
||||||
}
|
}
|
||||||
|
|
||||||
public handleClickOutside = () => {
|
private showChildren = (): void => {
|
||||||
const {handleDismissOverlay, dismissOnClickOutside} = this.props
|
this.setState({showChildren: true})
|
||||||
|
|
||||||
if (dismissOnClickOutside) {
|
|
||||||
handleDismissOverlay()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public handleAnimateDismiss = () => {
|
private hideChildren = (): void => {
|
||||||
const {transitionTime} = this.props
|
this.setState({showChildren: false})
|
||||||
this.setState({visible: false})
|
|
||||||
this.animationTimer = window.setTimeout(this.handleDismiss, transitionTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
public handleDismiss = () => {
|
|
||||||
const {handleDismissOverlay} = this.props
|
|
||||||
handleDismissOverlay()
|
|
||||||
clearTimeout(this.animationTimer)
|
clearTimeout(this.animationTimer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const mapStateToProps = ({
|
export default OverlayTechnology
|
||||||
overlayTechnology: {
|
|
||||||
OverlayNode,
|
|
||||||
options: {dismissOnClickOutside, dismissOnEscape, transitionTime},
|
|
||||||
},
|
|
||||||
}) => ({
|
|
||||||
OverlayNode,
|
|
||||||
dismissOnClickOutside,
|
|
||||||
dismissOnEscape,
|
|
||||||
transitionTime,
|
|
||||||
})
|
|
||||||
|
|
||||||
const mapDispatchToProps = {
|
|
||||||
handleDismissOverlay: dismissOverlay,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(Overlay)
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
const initialState = {
|
|
||||||
options: {
|
|
||||||
dismissOnClickOutside: false,
|
|
||||||
dismissOnEscape: false,
|
|
||||||
transitionTime: 300,
|
|
||||||
},
|
|
||||||
OverlayNode: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function overlayTechnology(state = initialState, action) {
|
|
||||||
switch (action.type) {
|
|
||||||
case 'SHOW_OVERLAY': {
|
|
||||||
const {OverlayNode, options} = action.payload
|
|
||||||
|
|
||||||
return {...state, OverlayNode, options}
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'DISMISS_OVERLAY': {
|
|
||||||
const {options} = initialState
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
OverlayNode: null,
|
|
||||||
options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return state
|
|
||||||
}
|
|
|
@ -14,7 +14,6 @@ import adminReducers from 'src/admin/reducers'
|
||||||
import kapacitorReducers from 'src/kapacitor/reducers'
|
import kapacitorReducers from 'src/kapacitor/reducers'
|
||||||
import dashboardUI from 'src/dashboards/reducers/ui'
|
import dashboardUI from 'src/dashboards/reducers/ui'
|
||||||
import cellEditorOverlay from 'src/dashboards/reducers/cellEditorOverlay'
|
import cellEditorOverlay from 'src/dashboards/reducers/cellEditorOverlay'
|
||||||
import overlayTechnology from 'src/shared/reducers/overlayTechnology'
|
|
||||||
import dashTimeV1 from 'src/dashboards/reducers/dashTimeV1'
|
import dashTimeV1 from 'src/dashboards/reducers/dashTimeV1'
|
||||||
import persistStateEnhancer from './persistStateEnhancer'
|
import persistStateEnhancer from './persistStateEnhancer'
|
||||||
import servicesReducer from 'src/shared/reducers/services'
|
import servicesReducer from 'src/shared/reducers/services'
|
||||||
|
@ -28,7 +27,6 @@ const rootReducer = combineReducers({
|
||||||
...adminReducers,
|
...adminReducers,
|
||||||
dashboardUI,
|
dashboardUI,
|
||||||
cellEditorOverlay,
|
cellEditorOverlay,
|
||||||
overlayTechnology,
|
|
||||||
dashTimeV1,
|
dashTimeV1,
|
||||||
logs: logsReducer,
|
logs: logsReducer,
|
||||||
routing: routerReducer,
|
routing: routerReducer,
|
||||||
|
|
Loading…
Reference in New Issue