-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
2b8e0eb
commit 6f65789
Showing
6 changed files
with
177 additions
and
79 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
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,77 @@ | ||
import React, { useContext, useEffect, useState } from 'react' | ||
|
||
class RouterContextClass { | ||
routes: { [key: string]: (active: boolean) => void } = {} | ||
activeRoute?: string | ||
registerRoute(route: string, cb: (active: boolean) => void) { | ||
this.routes[route] = cb | ||
if (this.activeRoute && this.activeRoute === route) { | ||
this.routes[route](true) | ||
} | ||
} | ||
unregisterRoute(route: string) { | ||
console.log('unregistering', route) | ||
console.log('unregistering', route) | ||
console.log('unregistering', route) | ||
console.log('unregistering', route) | ||
console.log('unregistering', route) | ||
delete this.routes[route] | ||
} | ||
setRoute(route: string) { | ||
if (route !== this.activeRoute && this.routes[route]) { | ||
if (this.activeRoute && this.routes[this.activeRoute]) { | ||
this.routes[this.activeRoute](false) | ||
} | ||
this.activeRoute = route | ||
// CONTINUE: Find out why <Route>'s are being removed from the "DOM" | ||
setTimeout(() => { | ||
this.routes[route](true) | ||
}) | ||
} | ||
} | ||
setDefaultRoute(route: string) { | ||
if (!this.activeRoute) { | ||
this.setRoute(route) | ||
} | ||
} | ||
} | ||
|
||
const contextState = new RouterContextClass() | ||
|
||
export const RouterContext = React.createContext(contextState) | ||
|
||
export interface RouterProps { | ||
defaultRoute: string | ||
} | ||
|
||
export const Router: React.FC<RouterProps> = props => { | ||
const ctx = useContext(RouterContext) | ||
|
||
useEffect(() => { | ||
ctx.setDefaultRoute(props.defaultRoute) | ||
}, [props.defaultRoute]) | ||
|
||
return <RouterContext.Provider value={contextState}>{props.children}</RouterContext.Provider> | ||
} | ||
|
||
export interface RouteProps { | ||
component: any | ||
path: string | ||
} | ||
|
||
export const Route: React.FC<RouteProps> = ({ component, path }) => { | ||
const ctx = useContext(RouterContext) | ||
const [active, setActive] = useState(false) | ||
|
||
useEffect(() => { | ||
const callback = active => { | ||
setActive(active) | ||
} | ||
ctx.registerRoute(path, callback) | ||
return () => { | ||
ctx.unregisterRoute(path) | ||
} | ||
}, [path]) | ||
|
||
return active ? <>{component}</> : null | ||
} |
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,39 +1,19 @@ | ||
import { render, Color, Box, Text } from 'ink' | ||
import { render } from 'ink' | ||
import React from 'react' | ||
import { Progress } from './components/Progress' | ||
import BorderBox from './components/BorderBox' | ||
import chalk from 'chalk' | ||
// import Step0StarterVsBlank from './newInit/Step0StarterVsBlank' | ||
import { TabApp } from './components/TabIndex' | ||
|
||
class Compy extends React.Component<any, { step: number; progress: number }> { | ||
state = { | ||
step: 0, | ||
progress: 0, | ||
} | ||
componentDidMount() { | ||
setInterval(() => { | ||
this.setState(({ step }) => { | ||
step += 0.05 | ||
|
||
return { | ||
step, | ||
progress: Math.abs(Math.sin(step)), | ||
} | ||
}) | ||
}, 16) | ||
} | ||
render() { | ||
return ( | ||
<BorderBox flexDirection="column" title={chalk.bold('Hello World ' + Math.round(this.state.progress * 100))}> | ||
<Progress progress={this.state.progress} /> | ||
</BorderBox> | ||
) | ||
} | ||
} | ||
import { TabIndexProvider } from './components/TabIndex' | ||
import { Router, Route } from './components/Router' | ||
import { TabApp } from './screens/TabApp' | ||
import { TabApp2 } from './screens/TabApp2' | ||
|
||
export function renderInk() { | ||
return new Promise(resolve => { | ||
render(<TabApp />) | ||
render( | ||
<TabIndexProvider> | ||
<Router defaultRoute="home"> | ||
<Route path="home" component={<TabApp />} /> | ||
<Route path="asd" component={<TabApp2 />} /> | ||
</Router> | ||
</TabIndexProvider>, | ||
) | ||
}) | ||
} |
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,38 @@ | ||
import React, { useState } from 'react' | ||
import { Box, Color } from 'ink' | ||
import BorderBox from '../components/BorderBox' | ||
import { TextInput } from '../components/inputs/TextInput' | ||
import { Checkbox } from '../components/inputs/Checkbox' | ||
import { Link } from '../components/Link' | ||
|
||
export function TabApp() { | ||
const [value0, setValue0] = useState('') | ||
const [value1, setValue1] = useState('') | ||
const [value2, setValue2] = useState('') | ||
const [value3, setValue3] = useState(false) | ||
|
||
return ( | ||
<Box marginTop={1} flexDirection="column"> | ||
<Color bold>This is a title</Color> | ||
<Color dim>This is TabApp 1</Color> | ||
<BorderBox title="Some Title" flexDirection="column" marginTop={1}> | ||
<TextInput | ||
label="some labe" | ||
value={value0} | ||
placeholder="this is a placeholder" | ||
tabIndex={0} | ||
onChange={setValue0} | ||
/> | ||
<Color dim>{' '}Some other text</Color> | ||
<TextInput label="some lable" value={value1} tabIndex={1} onChange={setValue1} /> | ||
<TextInput label="lubel" value={value2} tabIndex={2} onChange={setValue2} /> | ||
<Checkbox checked={value3} onChange={setValue3} tabIndex={3} label="This is a checkbox" /> | ||
<Link href="asd" label="Hellooo" tabIndex={4} /> | ||
</BorderBox> | ||
<Box marginTop={1} flexDirection="column"> | ||
<Link href="asd" label="Create" tabIndex={5} kind="forward" /> | ||
<Link href="asd" label="Back" tabIndex={6} kind="back" description="(Database options)" /> | ||
</Box> | ||
</Box> | ||
) | ||
} |
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,38 @@ | ||
import React, { useState } from 'react' | ||
import { Box, Color } from 'ink' | ||
import BorderBox from '../components/BorderBox' | ||
import { TextInput } from '../components/inputs/TextInput' | ||
import { Checkbox } from '../components/inputs/Checkbox' | ||
import { Link } from '../components/Link' | ||
|
||
export function TabApp2() { | ||
const [value0, setValue0] = useState('') | ||
const [value1, setValue1] = useState('') | ||
const [value2, setValue2] = useState('') | ||
const [value3, setValue3] = useState(false) | ||
|
||
return ( | ||
<Box marginTop={1} flexDirection="column"> | ||
<Color bold>This is a title 2</Color> | ||
<Color dim>This is TabApp 2</Color> | ||
<BorderBox title="Some Title" flexDirection="column" marginTop={1}> | ||
<TextInput | ||
label="some labe" | ||
value={value0} | ||
placeholder="this is a placeholder" | ||
tabIndex={0} | ||
onChange={setValue0} | ||
/> | ||
<Color dim>{' '}Some other text</Color> | ||
<TextInput label="some lable" value={value1} tabIndex={1} onChange={setValue1} /> | ||
<TextInput label="lubel" value={value2} tabIndex={2} onChange={setValue2} /> | ||
<Checkbox checked={value3} onChange={setValue3} tabIndex={3} label="This is a checkbox" /> | ||
<Link href="home" label="Hellooo" tabIndex={4} /> | ||
</BorderBox> | ||
<Box marginTop={1} flexDirection="column"> | ||
<Link href="home" label="Create" tabIndex={5} kind="forward" /> | ||
<Link href="home" label="Back" tabIndex={6} kind="back" description="(Database options)" /> | ||
</Box> | ||
</Box> | ||
) | ||
} |