Skip to content

Commit 8102c8e

Browse files
feat(overmind-components): new state API and provoked problem
1 parent 23c9f36 commit 8102c8e

File tree

7 files changed

+125
-21
lines changed

7 files changed

+125
-21
lines changed

packages/node_modules/overmind-components/src/component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ function init(componentNode: VNode): void {
3939
const name = component.name
4040
const tree = (component.tree = getApp().proxyStateTree.getTrackStateTree())
4141

42+
component.state = component.fn.getInitialState
43+
? component.fn.getInitialState(component.props)
44+
: {}
4245
component.currentNode = componentNode
4346
component.changeState = (newState) => {
44-
component.state = newState
47+
Object.assign(component.state, newState)
4548
patch(component.currentNode, {
4649
...component.currentNode,
4750
forceUpdate: true,

packages/node_modules/overmind-components/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ export interface IComponent<
1212
props: Props,
1313
overmind: {
1414
state: State
15-
changeState: (newState: State) => void
15+
changeState: (newState: Partial<State>) => void
1616
appState: Overmind<Config>['state']
1717
actions: Overmind<Config>['actions']
1818
effects: Overmind<Config>['effects']
1919
reaction: Overmind<Config>['reaction']
2020
}
2121
): any
22+
getInitialState?: (props: Props) => State
2223
}
2324

2425
export type Child = React.ReactNode

packages/node_modules/overmind-devtools-client/src/components/Actions/index.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import * as textStyles from '../../styles/text'
55
import Action from '../Action'
66
import ActionsList from '../ActionsList'
77
import ActionsTools from '../ActionsTools'
8+
import SplitPane from '../SplitPane'
89
import * as styles from './styles'
910

10-
const Actions: Component = (_, { appState }) => (
11+
const Actions: Component = (_, { appState, actions }) => (
1112
<self className={styles.wrapper}>
1213
<ActionsTools />
1314
{appState.currentAction ? (
1415
<div className={styles.columns}>
15-
<ActionsList />
16-
<Action action={appState.currentAction} />
16+
<SplitPane
17+
minSize={100}
18+
defaultSize={appState.actionsSplitSize}
19+
onChange={(size) => actions.updateActionsSplitSize(size)}
20+
>
21+
<ActionsList />
22+
<Action action={appState.currentAction} />
23+
</SplitPane>
1724
</div>
1825
) : (
1926
<div className={styles.centerWrapper}>
@@ -24,13 +31,3 @@ const Actions: Component = (_, { appState }) => (
2431
)
2532

2633
export default Actions
27-
28-
/* <SplitPane
29-
split="vertical"
30-
minSize={100}
31-
defaultSize={appState.actionsSplitSize}
32-
onChange={(size) => actions.updateActionsSplitSize(size)}
33-
>
34-
35-
</SplitPane>
36-
*/

packages/node_modules/overmind-devtools-client/src/components/ActionsList/styles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const separator = css({
99
})
1010

1111
export const wrapper = css({
12+
display: 'block',
1213
padding: '1rem',
1314
height: '100%',
1415
overflowY: 'scroll',
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Child, createElement } from 'overmind-components'
2+
3+
import { Component } from '../../overmind'
4+
import * as styles from './styles'
5+
6+
type Props = {
7+
minSize: number
8+
defaultSize: number
9+
onChange: (size: number) => void
10+
children: [Child, Child]
11+
}
12+
13+
type State = {
14+
isDragging: boolean
15+
size: number
16+
initialX: number
17+
}
18+
19+
const SplitPane: Component<Props, State> = (
20+
{ children, minSize, defaultSize, onChange },
21+
{ state, changeState }
22+
) => {
23+
return (
24+
<self
25+
onMount={() => {
26+
function onMouseMove(event) {
27+
if (state.isDragging) {
28+
const newSize = defaultSize + (event.clientX - state.initialX)
29+
30+
changeState({
31+
size: Math.max(minSize, newSize),
32+
})
33+
}
34+
}
35+
function onMouseUp() {
36+
changeState({
37+
isDragging: false,
38+
})
39+
onChange(state.size)
40+
}
41+
42+
window.addEventListener('mousemove', onMouseMove)
43+
window.addEventListener('mouseup', onMouseUp)
44+
45+
return () => {
46+
window.removeEventListener('mousemove', onMouseMove)
47+
window.removeEventListener('mouseup', onMouseUp)
48+
}
49+
}}
50+
className={styles.wrapper}
51+
>
52+
<div
53+
style={{
54+
width: state.size + 'px',
55+
}}
56+
>
57+
{children[0]}
58+
</div>
59+
<div
60+
className={styles.splitter}
61+
onMouseDown={(event) => {
62+
changeState({
63+
isDragging: true,
64+
initialX: event.clientX,
65+
})
66+
}}
67+
/>
68+
{children[1]}
69+
</self>
70+
)
71+
}
72+
73+
SplitPane.getInitialState = ({ defaultSize }) => ({
74+
size: defaultSize,
75+
isDragging: false,
76+
initialX: 0,
77+
})
78+
79+
export default SplitPane
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { css } from 'emotion'
2+
3+
import { colors } from '../../theme'
4+
5+
export const wrapper = css({
6+
display: 'flex',
7+
})
8+
9+
export const splitter = css({
10+
margin: '0 1rem',
11+
width: 3,
12+
backgroundColor: colors.foreground,
13+
cursor: 'col-resize',
14+
userSelect: 'none',
15+
})

packages/node_modules/overmind-devtools-client/src/components/ValueInspector/index.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,38 @@ type Props = {
99
small?: boolean
1010
}
1111

12-
const ValueInspector: Component<Props, string[]> = (
12+
const ValueInspector: Component<Props, { expandedPaths: string[] }> = (
1313
{ value, small },
14-
{ state = [], changeState }
14+
{ state: { expandedPaths }, changeState }
1515
) => {
1616
function onToggleExpand(path: string[]) {
1717
const pathString = path.join('.')
1818

19-
if (state.includes(pathString)) {
20-
changeState(state.filter((currentPath) => currentPath !== pathString))
19+
if (expandedPaths.includes(pathString)) {
20+
changeState({
21+
expandedPaths: expandedPaths.filter(
22+
(currentPath) => currentPath !== pathString
23+
),
24+
})
2125
} else {
22-
changeState(state.concat(pathString))
26+
changeState({ expandedPaths: expandedPaths.concat(pathString) })
2327
}
2428
}
2529

2630
return (
2731
<self className={styles.wrapper}>
2832
<Inspector
2933
value={value}
30-
expandedPaths={state}
34+
expandedPaths={expandedPaths}
3135
onToggleExpand={onToggleExpand}
3236
small={small}
3337
/>
3438
</self>
3539
)
3640
}
3741

42+
ValueInspector.getInitialState = () => ({
43+
expandedPaths: [],
44+
})
45+
3846
export default ValueInspector

0 commit comments

Comments
 (0)