-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalEntryField.tsx
More file actions
107 lines (104 loc) · 3.15 KB
/
Copy pathGlobalEntryField.tsx
File metadata and controls
107 lines (104 loc) · 3.15 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { useState } from "react";
import { Button, Chip, InputAdornment, TextField } from "@mui/material";
import type { SxProps, Theme } from "@mui/material";
import GlobalEntryDialog from "./GlobalEntryDialog";
import type { GlobalFieldRouting } from "../routing/pieceRouting";
export interface GlobalEntryFieldProps {
globalName: string;
label: string;
value: string;
onSelect: (entry: { id: string; name: string } | null) => void;
helperText?: string;
required?: boolean;
canCreate?: boolean;
hideLabel?: boolean;
disabled?: boolean;
hideActionWhenDisabled?: boolean;
sx?: SxProps<Theme>;
// Injected by RoutedGlobalEntryField when inside a /pieces/:id context.
// Absent when used in unrouted contexts (e.g. NewPieceDialog).
routing?: GlobalFieldRouting;
}
export default function GlobalEntryField({
globalName,
label,
value,
onSelect,
helperText,
required = false,
canCreate = false,
disabled = false,
hideActionWhenDisabled = false,
sx,
routing,
}: GlobalEntryFieldProps) {
const [localOpen, setLocalOpen] = useState(false);
// Disabled fields never open the picker, even when the URL targets this field.
const open = !disabled && (routing?.open ?? localOpen);
const onOpen = routing?.onOpen ?? (() => setLocalOpen(true));
const onClose = routing?.onClose ?? (() => setLocalOpen(false));
const showAction = !disabled || !hideActionWhenDisabled;
return (
<>
<TextField
label={label}
value={value ? "" : "None selected"}
helperText={helperText}
required={required}
disabled={disabled}
fullWidth
slotProps={{
input: {
readOnly: true,
startAdornment: value ? (
<InputAdornment position="start">
<Chip
label={value}
size="small"
onDelete={disabled ? undefined : () => onSelect(null)}
/>
</InputAdornment>
) : null,
endAdornment: showAction ? (
<InputAdornment position="end">
<Button
size="small"
onClick={(e) => {
e.stopPropagation();
onOpen();
}}
disabled={disabled}
aria-label={value ? `Change ${label}` : `Browse ${label}`}
>
{value ? "Change" : "Browse"}
</Button>
</InputAdornment>
) : null,
},
}}
sx={[
{
"& .MuiInputBase-root": {
cursor: "pointer",
// If there's a value, the actual input text is hidden
"& input": {
display: value ? "none" : "block",
},
},
},
...(Array.isArray(sx) ? sx : [sx]),
]}
onClick={disabled ? undefined : onOpen}
/>
<GlobalEntryDialog
globalName={globalName}
open={open}
tab={routing?.tab}
onTabChange={routing?.onTabChange}
onClose={onClose}
onSelect={onSelect}
canCreate={canCreate}
/>
</>
);
}