-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathPrioritySelector.tsx
More file actions
74 lines (67 loc) · 2.06 KB
/
PrioritySelector.tsx
File metadata and controls
74 lines (67 loc) · 2.06 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
import { t } from "@/i18n";
import type { Translations } from "@/i18n/translation";
import classNames from "classnames";
import type React from "react";
import { Button, type Key, Label, Menu, MenuItem, MenuTrigger } from "react-aria-components";
import type { Priority } from "../../api/domain/task";
import { ObsidianIcon } from "../components/obsidian-icon";
import { Popover } from "./Popover";
type Props = {
selected: Priority;
setSelected: (selected: Priority) => void;
};
const options: Priority[] = [4, 3, 2, 1];
export const PrioritySelector: React.FC<Props> = ({ selected, setSelected }) => {
const onSelected = (key: Key) => {
if (typeof key === "string") {
throw Error("unexpected key type");
}
// Should be a safe cast since we only use valid priorities
// as keys.
setSelected(key as Priority);
};
const i18n = t().createTaskModal.prioritySelector;
const label = getLabel(selected, i18n);
return (
<MenuTrigger>
<Button className="priority-selector" aria-label={i18n.buttonLabel}>
<ObsidianIcon size="m" id="flag" />
<span>{label}</span>
</Button>
<Popover>
<Menu
className="task-option-dialog task-priority-menu"
autoFocus="first"
aria-label={i18n.optionsLabel}
onAction={onSelected}
>
{options.map((priority) => {
const label = getLabel(priority, i18n);
const isSelected = priority === selected;
const className = classNames("priority-option", { "is-selected": isSelected });
return (
<MenuItem key={priority} className={className} id={priority}>
<Label>{label}</Label>
</MenuItem>
);
})}
</Menu>
</Popover>
</MenuTrigger>
);
};
const getLabel = (
priority: Priority,
i18n: Translations["createTaskModal"]["prioritySelector"],
): string => {
switch (priority) {
case 1:
return i18n.p4;
case 2:
return i18n.p3;
case 3:
return i18n.p2;
case 4:
return i18n.p1;
}
};