-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathaxis_unit_picker.tsx
More file actions
57 lines (48 loc) · 1.49 KB
/
axis_unit_picker.tsx
File metadata and controls
57 lines (48 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
import styled from '@emotion/styled';
import type { AxisUnit } from '@zakodium/nmrium-core';
import type { ReactNode } from 'react';
import { assert } from 'react-science/ui';
import type { ContextMenuItem } from '../../elements/ContextMenuBluePrint.tsx';
import { ContextMenu } from '../../elements/ContextMenuBluePrint.tsx';
import useCheckExperimentalFeature from '../../hooks/useCheckExperimentalFeature.ts';
import { axisUnitToLabel } from '../../hooks/use_axis_unit.ts';
interface AxisUnitPickerProps {
unit: AxisUnit;
allowedUnits: AxisUnit[];
onChange: (unit: AxisUnit) => void;
children: ReactNode;
}
export function AxisUnitPicker(props: AxisUnitPickerProps) {
const { unit, allowedUnits, onChange, children } = props;
const isExperimental = useCheckExperimentalFeature();
if (!isExperimental) return children;
const options: ContextMenuItem[] = allowedUnits.map((allowedUnit) => ({
key: allowedUnit,
roleStructure: 'listoption',
text: axisUnitToLabel[allowedUnit],
selected: allowedUnit === unit,
data: { unit: allowedUnit } as Data,
}));
function onSelect(data?: object) {
assert(data);
const { unit } = data as Data;
onChange(unit);
}
return (
<ContextMenuStyled
as="g"
className="unit"
options={options}
onSelect={onSelect}
>
{children}
</ContextMenuStyled>
);
}
const ContextMenuStyled = styled(ContextMenu)`
pointer-events: auto;
cursor: context-menu;
`;
interface Data {
unit: AxisUnit;
}