-
Notifications
You must be signed in to change notification settings - Fork 178
/
index.js
201 lines (193 loc) · 6.02 KB
/
index.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// @flow
import React, { useRef, memo } from "react"
import Paper from "@mui/material/Paper"
import { makeStyles } from "@mui/styles"
import { createTheme, ThemeProvider } from "@mui/material/styles"
import styles from "./styles"
import classnames from "classnames"
import type { Region } from "../ImageCanvas/region-tools.js"
import IconButton from "@mui/material/IconButton"
import Button from "@mui/material/Button"
import TrashIcon from "@mui/icons-material/Delete"
import CheckIcon from "@mui/icons-material/Check"
import TextField from "@mui/material/TextField"
import Select from "react-select"
import CreatableSelect from "react-select/creatable"
import { asMutable } from "seamless-immutable"
const theme = createTheme()
const useStyles = makeStyles((theme) => styles)
type Props = {
region: Region,
editing?: boolean,
allowedClasses?: Array<string>,
allowedTags?: Array<string>,
cls?: string,
tags?: Array<string>,
onDelete: (Region) => null,
onChange: (Region) => null,
onClose: (Region) => null,
onOpen: (Region) => null,
onRegionClassAdded: () => {},
allowComments?: boolean,
}
export const RegionLabel = ({
region,
editing,
allowedClasses,
allowedTags,
onDelete,
onChange,
onClose,
onOpen,
onRegionClassAdded,
allowComments,
}: Props) => {
const classes = useStyles()
const commentInputRef = useRef(null)
const onCommentInputClick = (_) => {
// The TextField wraps the <input> tag with two divs
const commentInput = commentInputRef.current.children[0].children[0]
if (commentInput) return commentInput.focus()
}
return (
<ThemeProvider theme={theme}>
<Paper
onClick={() => (!editing ? onOpen(region) : null)}
className={classnames(classes.regionInfo, {
highlighted: region.highlighted,
})}
>
{!editing ? (
<div>
{region.cls && (
<div className="name">
<div
className="circle"
style={{ backgroundColor: region.color }}
/>
{region.cls}
</div>
)}
{region.tags && (
<div className="tags">
{region.tags.map((t) => (
<div key={t} className="tag">
{t}
</div>
))}
</div>
)}
</div>
) : (
<div style={{ width: 200 }}>
<div style={{ display: "flex", flexDirection: "row" }}>
<div
style={{
display: "flex",
backgroundColor: region.color || "#888",
color: "#fff",
padding: 4,
paddingLeft: 8,
paddingRight: 8,
borderRadius: 4,
fontWeight: "bold",
textShadow: "0px 0px 5px rgba(0,0,0,0.4)",
}}
>
{region.type}
</div>
<div style={{ flexGrow: 1 }} />
<IconButton
onClick={() => onDelete(region)}
tabIndex={-1}
style={{ width: 22, height: 22 }}
size="small"
variant="outlined"
>
<TrashIcon style={{ marginTop: -8, width: 16, height: 16 }} />
</IconButton>
</div>
{(allowedClasses || []).length > 0 && (
<div style={{ marginTop: 6 }}>
<CreatableSelect
placeholder="Classification"
onChange={(o, actionMeta) => {
if (actionMeta.action == "create-option") {
onRegionClassAdded(o.value)
}
return onChange({
...(region: any),
cls: o.value,
})
}}
value={
region.cls ? { label: region.cls, value: region.cls } : null
}
options={asMutable(
allowedClasses.map((c) => ({ value: c, label: c }))
)}
/>
</div>
)}
{(allowedTags || []).length > 0 && (
<div style={{ marginTop: 4 }}>
<Select
onChange={(newTags) =>
onChange({
...(region: any),
tags: newTags.map((t) => t.value),
})
}
placeholder="Tags"
value={(region.tags || []).map((c) => ({
label: c,
value: c,
}))}
isMulti
options={asMutable(
allowedTags.map((c) => ({ value: c, label: c }))
)}
/>
</div>
)}
{allowComments && (
<TextField
InputProps={{
className: classes.commentBox,
}}
fullWidth
multiline
rows={3}
ref={commentInputRef}
onClick={onCommentInputClick}
value={region.comment || ""}
onChange={(event) =>
onChange({ ...(region: any), comment: event.target.value })
}
/>
)}
{onClose && (
<div style={{ marginTop: 4, display: "flex" }}>
<div style={{ flexGrow: 1 }} />
<Button
onClick={() => onClose(region)}
size="small"
variant="contained"
color="primary"
>
<CheckIcon />
</Button>
</div>
)}
</div>
)}
</Paper>
</ThemeProvider>
)
}
export default memo(
RegionLabel,
(prevProps, nextProps) =>
prevProps.editing === nextProps.editing &&
prevProps.region === nextProps.region
)