Alex Mikhalev
7 years ago
12 changed files with 106 additions and 53 deletions
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
import { observer } from "mobx-react"; |
||||
import * as React from "react"; |
||||
|
||||
import DeviceView from "@app/components/DeviceView"; |
||||
import { injectState, State } from "@app/state"; |
||||
|
||||
class DevicesView extends React.Component<{ state: State }> { |
||||
render() { |
||||
const { device } = this.props.state; |
||||
return <DeviceView device={device} />; |
||||
} |
||||
} |
||||
|
||||
export default injectState(observer(DevicesView)); |
@ -1,16 +0,0 @@
@@ -1,16 +0,0 @@
|
||||
import * as PropTypes from "prop-types"; |
||||
import * as React from "react"; |
||||
|
||||
export class Provider<T extends {}> extends React.Component<T> { |
||||
static childContextTypes = { |
||||
injected: any, |
||||
}; |
||||
} |
||||
|
||||
export function inject<P, T extends React.ComponentType<P>>(...names: string[]): (base: T) => React.ComponentClass<T> { |
||||
return (Component) => { |
||||
return class extends React.Component<T> { |
||||
|
||||
}; |
||||
}; |
||||
} |
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
import * as PropTypes from "prop-types"; |
||||
import * as React from "react"; |
||||
|
||||
import { State } from "@app/state"; |
||||
|
||||
interface IProvidedStateContext { |
||||
providedState: State; |
||||
} |
||||
|
||||
const providedStateContextTypes: PropTypes.ValidationMap<any> = { |
||||
providedState: PropTypes.object, |
||||
}; |
||||
|
||||
export class ProvideState extends React.Component<{ |
||||
state: State, |
||||
}> implements React.ChildContextProvider<IProvidedStateContext> { |
||||
static childContextTypes = providedStateContextTypes; |
||||
|
||||
getChildContext(): IProvidedStateContext { |
||||
return { |
||||
providedState: this.props.state, |
||||
}; |
||||
} |
||||
|
||||
render() { |
||||
return React.Children.only(this.props.children); |
||||
} |
||||
} |
||||
|
||||
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; |
||||
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; |
||||
|
||||
export function injectState<P extends { "state": State }, T extends React.ComponentClass<P>>(Component: T) { |
||||
return class extends React.Component<Omit<P, "state">> { |
||||
static contextTypes = providedStateContextTypes; |
||||
context: IProvidedStateContext; |
||||
|
||||
render() { |
||||
const state = this.context.providedState; |
||||
return <Component {...this.props} state={state} />; |
||||
} |
||||
}; |
||||
} |
@ -1,6 +1,6 @@
@@ -1,6 +1,6 @@
|
||||
{ |
||||
"compilerOptions": { |
||||
"experimentalDecorators": true, |
||||
"compilerOptions": { |
||||
"target": "es5" |
||||
} |
||||
} |
Loading…
Reference in new issue