forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserTemplate.tsx
More file actions
64 lines (59 loc) · 1.49 KB
/
UserTemplate.tsx
File metadata and controls
64 lines (59 loc) · 1.49 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
import React from 'react';
import { ColorIcons as Icons } from '@codesandbox/template-icons';
import { Template } from '../../types/index';
import getIcon from '../../templates/icons';
import { ENTER } from '../../utils/keycodes';
import { Button, IconContainer, Title } from './elements';
import { TemplateType } from '../../templates';
import { getSandboxName } from '../../utils/get-sandbox-name';
interface UserTemplate extends Template {
iconUrl?: string;
id?: string;
title?: string;
sandbox?: {
alias: string;
id: string;
title: string;
source: {
template: TemplateType;
};
};
}
interface IUserTemplateProps {
template: UserTemplate;
selectTemplate: (t: Template) => void;
small: boolean;
}
export const UserTemplate = ({
template,
selectTemplate,
small,
}: IUserTemplateProps) => {
const Icon =
template.iconUrl && Icons[template.iconUrl]
? Icons[template.iconUrl]
: getIcon(template.sandbox.source.template);
const select = () =>
selectTemplate({
...template,
shortid: template.sandbox.alias || template.sandbox.id,
});
return (
<Button
onClick={select}
color={template.color}
custom
onKeyDown={e => {
if (e.keyCode === ENTER) {
select();
}
}}
tabIndex={0}
>
<IconContainer>
<Icon width={small ? 24 : 32} height={small ? 24 : 32} />
</IconContainer>
<Title>{getSandboxName(template.sandbox)}</Title>
</Button>
);
};