-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKnobs.tsx
More file actions
67 lines (62 loc) · 2.02 KB
/
Knobs.tsx
File metadata and controls
67 lines (62 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as React from "react";
import styles from "./styles.css";
import useComponents from "./useComponents";
interface Props {
options?: { [key: string]: string[] };
}
const Knobs: React.FC<Props> = props => {
const components = useComponents();
const { children, options } = props;
console.log(children);
const child = React.Children.toArray(children)[0];
const compProps = (child as any).props;
const editablePropNames = Object.keys(compProps).reduce(
(acc: string[], prop) => {
if (prop === "children") return acc;
if (typeof compProps[prop] === "object") return acc;
return [...acc, prop];
},
[]
);
const [knobs, setKnobs] = React.useState(compProps);
return (
<div className={styles.root}>
<div className={styles.container}>
{React.cloneElement(child as any, { ...knobs })}
</div>
<div className={styles.menu}>
{editablePropNames.map(p => (
<div key={p}>
<components.label>{p}</components.label>
{typeof knobs[p] === "boolean" ? (
<components.checkbox
checked={knobs[p]}
onChange={e => setKnobs({ ...knobs, [p]: e.target.checked })}
/>
) : null}
{typeof knobs[p] === "string" || typeof knobs[p] === "number" ? (
options && options[p] ? (
<components.select
value={knobs[p]}
onChange={e => setKnobs({ ...knobs, [p]: e.target.value })}
>
{options[p].map((opt: string) => (
<option value={opt} key={opt}>
{opt}
</option>
))}
</components.select>
) : (
<components.input
value={knobs[p]}
onChange={e => setKnobs({ ...knobs, [p]: e.target.value })}
/>
)
) : null}
</div>
))}
</div>
</div>
);
};
export default Knobs;