forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
88 lines (75 loc) · 2.24 KB
/
index.tsx
File metadata and controls
88 lines (75 loc) · 2.24 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, { ComponentProps, FunctionComponent } from 'react';
import Tooltip from '../Tooltip';
import { Container } from './elements';
import { PreferenceDropdown } from './PreferenceDropdown';
import { PreferenceKeybinding } from './PreferenceKeybinding';
import { PreferenceNumber } from './PreferenceNumber';
import { PreferenceSwitch } from './PreferenceSwitch';
import { PreferenceText } from './PreferenceText';
type PreferenceType =
| 'boolean'
| 'dropdown'
| 'keybinding'
| 'number'
| 'string';
type PreferenceProps<TString extends PreferenceType> = {
className?: string;
style?: React.CSSProperties;
title: string;
tooltip?: string;
options?: any[];
type: TString;
};
export type BooleanPreference = PreferenceProps<'boolean'> &
ComponentProps<typeof PreferenceSwitch>;
export type StringPreference = PreferenceProps<'string'> &
ComponentProps<typeof PreferenceText>;
export type DropdownPreference = PreferenceProps<'dropdown'> &
ComponentProps<typeof PreferenceDropdown>;
export type KeybindingPreference = PreferenceProps<'keybinding'> &
ComponentProps<typeof PreferenceKeybinding>;
export type NumberPreference = PreferenceProps<'number'> &
ComponentProps<typeof PreferenceNumber>;
export type Props =
| BooleanPreference
| StringPreference
| DropdownPreference
| KeybindingPreference
| NumberPreference;
export const Preference: FunctionComponent<Props> = ({
className,
style,
title,
tooltip,
...contentProps
}) => {
const getContent = () => {
switch (
contentProps.type // need 'type' as discriminant of union type
) {
case 'boolean':
return <PreferenceSwitch {...contentProps} />;
case 'string':
return <PreferenceText {...contentProps} />;
case 'dropdown':
return <PreferenceDropdown {...contentProps} />;
case 'keybinding':
return <PreferenceKeybinding {...contentProps} />;
default:
return <PreferenceNumber {...contentProps} />;
}
};
const Title = tooltip ? (
<Tooltip content={tooltip} placement="right">
{title}
</Tooltip>
) : (
<span>{title}</span>
);
return (
<Container className={className} style={style}>
{Title}
<div>{getContent()}</div>
</Container>
);
};