Skip to content

Commit 551d2ca

Browse files
feat(overmind-devtools-client): change how to edit state
1 parent 2fd20b9 commit 551d2ca

File tree

3 files changed

+96
-84
lines changed

3 files changed

+96
-84
lines changed

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

Lines changed: 62 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ import { isValidJson } from '../../overmind/utils'
44
import * as styles from './styles'
55
import { css } from 'emotion'
66
import { colors } from '../../theme'
7+
import { FaEdit } from 'react-icons/fa'
8+
9+
type PathToolsProps = {
10+
path: string[]
11+
onClickPath: (path: string[]) => void
12+
}
13+
14+
const PathTools: FunctionComponent<PathToolsProps> = ({
15+
path,
16+
onClickPath,
17+
children,
18+
}) => {
19+
return (
20+
<div className={styles.pathToolsWrapper}>
21+
{children}{' '}
22+
<FaEdit
23+
className={styles.toolIcon}
24+
onClick={(event) => {
25+
event.stopPropagation()
26+
onClickPath(path)
27+
}}
28+
/>
29+
</div>
30+
)
31+
}
732

833
type EditValueProps = {
934
value: any
@@ -79,15 +104,7 @@ const Nested: FunctionComponent<NestedProps> = ({
79104
}}
80105
>
81106
{path.length ? (
82-
<span
83-
className={styles.key}
84-
onClick={(event) => {
85-
event.stopPropagation()
86-
onClickPath(path)
87-
}}
88-
>
89-
{path[path.length - 1]}:
90-
</span>
107+
<span className={styles.key}>{path[path.length - 1]}:</span>
91108
) : null}
92109
<EditValue value={value} onSubmit={onSubmitState} />
93110
</div>
@@ -103,24 +120,18 @@ const Nested: FunctionComponent<NestedProps> = ({
103120
onToggleExpand(path)
104121
}}
105122
>
106-
{path.length ? (
107-
<span
108-
className={styles.key}
109-
onClick={(event) => {
110-
event.stopPropagation()
111-
onClickPath(path)
112-
}}
113-
>
114-
{path[path.length - 1]}:
123+
<PathTools path={path} onClickPath={onClickPath}>
124+
{path.length ? (
125+
<span className={styles.key}>{path[path.length - 1]}:</span>
126+
) : null}
127+
{startBracket}
128+
<span className={styles.keyCount}>
129+
{isArray
130+
? keys.length + ' items'
131+
: keys.slice(0, 3).join(', ') + '...'}
115132
</span>
116-
) : null}
117-
{startBracket}
118-
<span className={styles.keyCount}>
119-
{isArray
120-
? keys.length + ' items'
121-
: keys.slice(0, 3).join(', ') + '...'}
122-
</span>
123-
{endBracket}
133+
{endBracket}
134+
</PathTools>
124135
</div>
125136
)
126137
}
@@ -134,18 +145,12 @@ const Nested: FunctionComponent<NestedProps> = ({
134145
onToggleExpand(path)
135146
}}
136147
>
137-
{path.length ? (
138-
<span
139-
className={styles.key}
140-
onClick={(event) => {
141-
event.stopPropagation()
142-
onClickPath(path)
143-
}}
144-
>
145-
{path[path.length - 1]}:
146-
</span>
147-
) : null}
148-
{startBracket}
148+
<PathTools path={path} onClickPath={onClickPath}>
149+
{path.length ? (
150+
<span className={styles.key}>{path[path.length - 1]}:</span>
151+
) : null}
152+
{startBracket}
153+
</PathTools>
149154
</div>
150155
<div className={styles.nestedChildren}>{children()}</div>
151156
<div className={styles.bracket(false)}>{endBracket}</div>
@@ -172,15 +177,7 @@ const ValueComponent: FunctionComponent<ValueComponentProps> = ({
172177
return (
173178
<div className={styles.genericValue}>
174179
{path.length ? (
175-
<span
176-
className={styles.key}
177-
onClick={(event) => {
178-
event.stopPropagation()
179-
onClickPath(path)
180-
}}
181-
>
182-
{path[path.length - 1]}:
183-
</span>
180+
<span className={styles.key}>{path[path.length - 1]}:</span>
184181
) : null}
185182
<EditValue value={value} onSubmit={onSubmitState} />
186183
</div>
@@ -194,55 +191,37 @@ const ValueComponent: FunctionComponent<ValueComponentProps> = ({
194191
) {
195192
return (
196193
<div className={styles.otherValue}>
197-
{path.length ? (
198-
<span
199-
className={styles.key}
200-
onClick={(event) => {
201-
event.stopPropagation()
202-
onClickPath(path)
203-
}}
204-
>
205-
{path[path.length - 1]}:
206-
</span>
207-
) : null}
208-
{value.substr(1, value.length - 2)}
194+
<PathTools path={path} onClickPath={onClickPath}>
195+
{path.length ? (
196+
<span className={styles.key}>{path[path.length - 1]}:</span>
197+
) : null}
198+
{value.substr(1, value.length - 2)}
199+
</PathTools>
209200
</div>
210201
)
211202
}
212203

213204
if (typeof value === 'string') {
214205
return (
215206
<div className={styles.stringValue}>
216-
{path.length ? (
217-
<span
218-
className={styles.key}
219-
onClick={(event) => {
220-
event.stopPropagation()
221-
onClickPath(path)
222-
}}
223-
>
224-
{path[path.length - 1]}:
225-
</span>
226-
) : null}
227-
"{value.length > 50 ? value.substr(0, 50) + '...' : value}"
207+
<PathTools path={path} onClickPath={onClickPath}>
208+
{path.length ? (
209+
<span className={styles.key}>{path[path.length - 1]}:</span>
210+
) : null}
211+
"{value.length > 50 ? value.substr(0, 50) + '...' : value}"
212+
</PathTools>
228213
</div>
229214
)
230215
}
231216

232217
return (
233218
<div className={styles.genericValue}>
234-
{path.length ? (
235-
<span
236-
className={styles.key}
237-
onClick={(event) => {
238-
event.stopPropagation()
239-
onClickPath(path)
240-
}}
241-
>
242-
{path[path.length - 1]}:
243-
</span>
244-
) : null}
245-
{String(value)}
219+
<PathTools path={path} onClickPath={onClickPath}>
220+
{path.length ? (
221+
<span className={styles.key}>{path[path.length - 1]}:</span>
222+
) : null}
223+
{String(value)}
224+
</PathTools>
246225
</div>
247226
)
248227
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ export const key = css({
2323
},
2424
})
2525

26+
export const pathToolsWrapper = css({
27+
display: 'flex',
28+
alignItems: 'center',
29+
svg: {
30+
color: colors.white4,
31+
cursor: 'pointer',
32+
display: 'none',
33+
opacity: 0.5,
34+
},
35+
'svg:hover': {
36+
opacity: 1,
37+
},
38+
':hover svg': {
39+
display: 'block',
40+
},
41+
})
42+
43+
export const toolIcon = css({
44+
margin: '0 0.75rem',
45+
})
46+
2647
export const inlineNested = css({
2748
cursor: 'pointer',
2849
})

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createElement, FunctionComponent } from 'react'
1+
import { createElement, FunctionComponent, useEffect } from 'react'
22
import { useOvermind } from '../../overmind'
33
import * as styles from './styles'
44
import * as textStyles from '../../styles/text'
@@ -21,6 +21,18 @@ const DerivedWrapper: FunctionComponent = ({ children }) => (
2121
const State: FunctionComponent = () => {
2222
const { state, actions } = useOvermind()
2323

24+
useEffect(() => {
25+
const onKeyDown = (event) => {
26+
if (event.keyCode === 27) {
27+
actions.undoSettingState()
28+
}
29+
}
30+
window.addEventListener('keydown', onKeyDown)
31+
return () => {
32+
window.removeEventListener('keydown', onKeyDown)
33+
}
34+
}, [])
35+
2436
return (
2537
<div className={styles.wrapper} onClick={() => actions.undoSettingState()}>
2638
<Inspector

0 commit comments

Comments
 (0)