@object-ui/fields 16.1.0 → 17.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +432 -0
- package/dist/FieldEditWidget.d.ts +7 -0
- package/dist/field-type-alias.d.ts +16 -0
- package/dist/index.cjs +5 -5
- package/dist/index.d.ts +14 -11
- package/dist/index.js +2428 -2100
- package/dist/widgets/FilterConditionField.d.ts +32 -0
- package/dist/widgets/ImageLightbox.d.ts +30 -0
- package/dist/widgets/ImageLightbox.test.d.ts +1 -0
- package/dist/widgets/LookupField.hydration.test.d.ts +1 -0
- package/dist/widgets/LookupField.idField.test.d.ts +1 -0
- package/dist/widgets/RecipientPickerField.test.d.ts +0 -0
- package/dist/widgets/__tests__/FilterConditionField.matchAll.test.d.ts +8 -0
- package/dist/widgets/__tests__/FilterConditionField.operators.test.d.ts +8 -0
- package/dist/widgets/file-value.d.ts +112 -0
- package/dist/widgets/file-value.test.d.ts +1 -0
- package/dist/widgets/useFieldTranslation.d.ts +2 -0
- package/package.json +9 -8
- package/src/FieldEditWidget.test.ts +74 -2
- package/src/FieldEditWidget.tsx +44 -4
- package/src/field-type-alias.ts +100 -0
- package/src/index.tsx +94 -133
- package/src/widgets/CheckboxesField.tsx +8 -5
- package/src/widgets/FileField.tsx +88 -57
- package/src/widgets/FilterConditionField.tsx +109 -9
- package/src/widgets/ImageField.tsx +71 -33
- package/src/widgets/ImageLightbox.test.tsx +52 -0
- package/src/widgets/ImageLightbox.tsx +108 -0
- package/src/widgets/LookupField.hydration.test.tsx +136 -0
- package/src/widgets/LookupField.idField.test.tsx +61 -0
- package/src/widgets/LookupField.tsx +106 -9
- package/src/widgets/MultiSelectField.tsx +6 -3
- package/src/widgets/ObjectRefField.tsx +9 -4
- package/src/widgets/PeoplePicker.test.tsx +20 -0
- package/src/widgets/PeoplePicker.tsx +15 -3
- package/src/widgets/RadioField.tsx +6 -3
- package/src/widgets/RecipientPickerField.test.tsx +157 -0
- package/src/widgets/RecipientPickerField.tsx +36 -16
- package/src/widgets/TextAreaField.tsx +4 -1
- package/src/widgets/__tests__/FilterConditionField.matchAll.test.ts +66 -0
- package/src/widgets/__tests__/FilterConditionField.operators.test.ts +142 -0
- package/src/widgets/file-value.test.ts +230 -0
- package/src/widgets/file-value.ts +230 -0
- package/src/widgets/useFieldTranslation.ts +30 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File field value shapes (ObjectStack ADR-0104 D3 wave 2).
|
|
3
|
+
*
|
|
4
|
+
* A `file`/`image`/`avatar`/`video`/`audio` field value reaches the UI in one
|
|
5
|
+
* of three forms, and every widget that renders one has to cope with all
|
|
6
|
+
* three — so the rules live here once rather than being re-derived per widget.
|
|
7
|
+
*
|
|
8
|
+
* 1. **Reference** — a bare `sys_file` id string. The form the backend stores
|
|
9
|
+
* once file-as-reference is adopted.
|
|
10
|
+
* 2. **Expanded** — `{ id, name, size, mimeType, url }`, what the read path
|
|
11
|
+
* returns after resolving a stored reference. Note `mimeType`: this comes
|
|
12
|
+
* from the platform's spec-owned `FileValueSchema` and is **camelCase**.
|
|
13
|
+
* 3. **Legacy inline blob** — `{ file_id?, name, original_name, size,
|
|
14
|
+
* mime_type, url }`, the pre-reference shape this package used to build
|
|
15
|
+
* itself, with **snake_case** `mime_type`.
|
|
16
|
+
*
|
|
17
|
+
* That casing split is the whole reason this module exists: a widget reading
|
|
18
|
+
* only `mime_type` silently stops recognising images the moment the backend
|
|
19
|
+
* starts returning the expanded form, and the failure looks like "thumbnails
|
|
20
|
+
* disappeared" rather than anything pointing at a value shape.
|
|
21
|
+
*
|
|
22
|
+
* ## Submitting
|
|
23
|
+
*
|
|
24
|
+
* When the upload adapter surfaced a `fileId`, the widget submits the **bare
|
|
25
|
+
* id** — the reference form. When it did not (the object-URL fallback adapter,
|
|
26
|
+
* an older backend), it submits the legacy blob unchanged. So the same build
|
|
27
|
+
* works against a backend that has adopted references and one that has not.
|
|
28
|
+
* Action params already POST a bare fileId; this brings record field values
|
|
29
|
+
* onto the same contract.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/** A file value normalised for rendering, whatever form it arrived in. */
|
|
33
|
+
export interface FileValueView {
|
|
34
|
+
/** `sys_file` id, when the value carries one. */
|
|
35
|
+
id?: string;
|
|
36
|
+
/** Best available display name. Never empty. */
|
|
37
|
+
name: string;
|
|
38
|
+
/** Resolvable URL, when the value carries one. A bare reference does not. */
|
|
39
|
+
url?: string;
|
|
40
|
+
size?: number;
|
|
41
|
+
mimeType?: string;
|
|
42
|
+
/** The value as it arrived, so callers can pass it through untouched. */
|
|
43
|
+
raw: unknown;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A minted file id: uuid/nanoid-shaped, and crucially not a URL — a URL always
|
|
48
|
+
* carries `:`, `/` or `.`, so it can never match. Mirrors the platform's
|
|
49
|
+
* `isFileIdToken`, which is the arbiter on the server side of the same question.
|
|
50
|
+
*/
|
|
51
|
+
export function isFileIdToken(value: unknown): value is string {
|
|
52
|
+
return typeof value === 'string' && /^[A-Za-z0-9_-]{1,64}$/.test(value);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The `sys_file` id a value refers to, or `undefined`.
|
|
57
|
+
*
|
|
58
|
+
* `file_id ?? id` matches the rule `serializeParamValues` already applies to
|
|
59
|
+
* action params — one extraction rule for both surfaces.
|
|
60
|
+
*/
|
|
61
|
+
export function fileIdOf(value: unknown): string | undefined {
|
|
62
|
+
if (isFileIdToken(value)) return value;
|
|
63
|
+
if (value && typeof value === 'object') {
|
|
64
|
+
const o = value as Record<string, unknown>;
|
|
65
|
+
const id = o.file_id ?? o.id;
|
|
66
|
+
if (typeof id === 'string' && id) return id;
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The stable download endpoint a bare `sys_file` id resolves to.
|
|
73
|
+
*
|
|
74
|
+
* `createObjectStackUploadAdapter` stores exactly this URL (`${basePath}/files/
|
|
75
|
+
* :id`, default base `/api/v1/storage`) as a completed upload's value, and the
|
|
76
|
+
* endpoint 302-redirects to a freshly-signed short-lived URL on every request —
|
|
77
|
+
* so it can be used directly as an `<img src>`. Building it here means a value
|
|
78
|
+
* still in its bare-reference form (the backend read path didn't expand it —
|
|
79
|
+
* seen on the edit-form data path, objectui image thumbnails rendering broken)
|
|
80
|
+
* resolves to a real URL instead of an empty `src`.
|
|
81
|
+
*/
|
|
82
|
+
export const FILE_STORAGE_BASE_PATH = '/api/v1/storage';
|
|
83
|
+
|
|
84
|
+
/** The stable download URL for a `sys_file` id. */
|
|
85
|
+
export function fileUrlFromId(id: string): string {
|
|
86
|
+
return `${FILE_STORAGE_BASE_PATH}/files/${encodeURIComponent(id)}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Last path segment of a URL, used as a display name of last resort. */
|
|
90
|
+
function nameFromUrl(url: string): string {
|
|
91
|
+
const path = url.split(/[?#]/)[0] ?? url;
|
|
92
|
+
const seg = path.split('/').filter(Boolean).pop();
|
|
93
|
+
return seg ? decodeURIComponent(seg) : url;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Normalise one file value for display.
|
|
98
|
+
*
|
|
99
|
+
* @param fallbackName shown when the value carries no usable name — pass a
|
|
100
|
+
* translated string so the widget stays localised.
|
|
101
|
+
*/
|
|
102
|
+
export function readFileValue(value: unknown, fallbackName = 'File'): FileValueView {
|
|
103
|
+
if (value == null) return { name: fallbackName, raw: value };
|
|
104
|
+
|
|
105
|
+
if (typeof value === 'string') {
|
|
106
|
+
// A bare id carries no name of its own (the backend expands it on read),
|
|
107
|
+
// but its URL is derivable from the stable download endpoint — so a value
|
|
108
|
+
// the read path left as a bare reference still renders a thumbnail instead
|
|
109
|
+
// of a broken `<img src="">`.
|
|
110
|
+
if (isFileIdToken(value)) return { id: value, name: fallbackName, url: fileUrlFromId(value), raw: value };
|
|
111
|
+
// Otherwise it is a URL (legacy external link, data:, blob:).
|
|
112
|
+
return { url: value, name: nameFromUrl(value), raw: value };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (typeof value === 'object') {
|
|
116
|
+
const o = value as Record<string, unknown>;
|
|
117
|
+
const str = (v: unknown) => (typeof v === 'string' && v ? v : undefined);
|
|
118
|
+
const id = fileIdOf(o);
|
|
119
|
+
// An object shape that carries an id but not its URL (a partially-expanded
|
|
120
|
+
// reference) still resolves to the stable endpoint, same as a bare id.
|
|
121
|
+
const url = str(o.url) ?? (id ? fileUrlFromId(id) : undefined);
|
|
122
|
+
const name =
|
|
123
|
+
str(o.name) ??
|
|
124
|
+
str(o.original_name) ??
|
|
125
|
+
(str(o.url) ? nameFromUrl(str(o.url) as string) : undefined) ??
|
|
126
|
+
fallbackName;
|
|
127
|
+
return {
|
|
128
|
+
id,
|
|
129
|
+
name,
|
|
130
|
+
url,
|
|
131
|
+
size: typeof o.size === 'number' ? o.size : undefined,
|
|
132
|
+
// Expanded form is camelCase, legacy blob is snake_case — accept both.
|
|
133
|
+
mimeType: str(o.mimeType) ?? str(o.mime_type),
|
|
134
|
+
raw: value,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { name: fallbackName, raw: value };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Normalise a field value (single or `multiple`) to an array of views. */
|
|
142
|
+
export function readFileValues(value: unknown, fallbackName = 'File'): FileValueView[] {
|
|
143
|
+
if (value == null) return [];
|
|
144
|
+
const items = Array.isArray(value) ? value : [value];
|
|
145
|
+
return items.filter((v) => v != null).map((v) => readFileValue(v, fallbackName));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Does this value look like an image, by MIME type? */
|
|
149
|
+
export function isImageValue(view: FileValueView): boolean {
|
|
150
|
+
return (view.mimeType ?? '').startsWith('image/');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The subset of an upload result these helpers read. */
|
|
154
|
+
export interface UploadResultLike {
|
|
155
|
+
url: string;
|
|
156
|
+
name: string;
|
|
157
|
+
size: number;
|
|
158
|
+
mimeType: string;
|
|
159
|
+
meta?: Record<string, unknown>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* What to store in the field for a completed upload.
|
|
164
|
+
*
|
|
165
|
+
* Returns the bare `sys_file` id when the adapter surfaced one — the reference
|
|
166
|
+
* form — and the legacy inline blob when it did not, so a deployment whose
|
|
167
|
+
* upload adapter or backend predates file-as-reference keeps working unchanged.
|
|
168
|
+
*/
|
|
169
|
+
export function fileValueForSubmit(
|
|
170
|
+
result: UploadResultLike,
|
|
171
|
+
originalName?: string,
|
|
172
|
+
): string | Record<string, unknown> {
|
|
173
|
+
const fileId = (result.meta as { fileId?: unknown } | undefined)?.fileId;
|
|
174
|
+
if (isFileIdToken(fileId)) return fileId;
|
|
175
|
+
return {
|
|
176
|
+
name: result.name,
|
|
177
|
+
original_name: originalName ?? result.name,
|
|
178
|
+
size: result.size,
|
|
179
|
+
mime_type: result.mimeType,
|
|
180
|
+
url: result.url,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The display view of a just-completed upload.
|
|
186
|
+
*
|
|
187
|
+
* Submitting a bare id means the field value no longer carries the name, size
|
|
188
|
+
* or URL needed to render it, and the enriched form only comes back on the next
|
|
189
|
+
* read. Widgets keep these views keyed by id so an upload appears immediately
|
|
190
|
+
* instead of showing a bare token until a refetch.
|
|
191
|
+
*/
|
|
192
|
+
export function uploadResultView(result: UploadResultLike, originalName?: string): FileValueView {
|
|
193
|
+
const id = isFileIdToken((result.meta as { fileId?: unknown } | undefined)?.fileId)
|
|
194
|
+
? ((result.meta as { fileId?: string }).fileId as string)
|
|
195
|
+
: undefined;
|
|
196
|
+
return {
|
|
197
|
+
id,
|
|
198
|
+
name: result.name || originalName || 'File',
|
|
199
|
+
url: result.url,
|
|
200
|
+
size: result.size,
|
|
201
|
+
mimeType: result.mimeType,
|
|
202
|
+
raw: result,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Merge locally-known upload views over a field value's own views, matched by
|
|
208
|
+
* id. A value that is still a bare reference picks up the name/URL captured at
|
|
209
|
+
* upload time; anything already enriched by the backend is left alone.
|
|
210
|
+
*/
|
|
211
|
+
export function withRecentUploads(
|
|
212
|
+
views: FileValueView[],
|
|
213
|
+
recent: Record<string, FileValueView>,
|
|
214
|
+
): FileValueView[] {
|
|
215
|
+
if (Object.keys(recent).length === 0) return views;
|
|
216
|
+
return views.map((v) => {
|
|
217
|
+
const known = v.id ? recent[v.id] : undefined;
|
|
218
|
+
if (!known) return v;
|
|
219
|
+
// A value the backend already expanded carries its own URL on the raw
|
|
220
|
+
// object — leave that alone. A bare reference (string id, or an id-only
|
|
221
|
+
// object) has only the synthetic download URL `readFileValue` derived, so
|
|
222
|
+
// the just-uploaded name/mimeType still fills in.
|
|
223
|
+
const rawHasOwnUrl =
|
|
224
|
+
!!v.raw &&
|
|
225
|
+
typeof v.raw === 'object' &&
|
|
226
|
+
typeof (v.raw as Record<string, unknown>).url === 'string' &&
|
|
227
|
+
!!(v.raw as Record<string, unknown>).url;
|
|
228
|
+
return rawHasOwnUrl ? v : { ...known, raw: v.raw };
|
|
229
|
+
});
|
|
230
|
+
}
|
|
@@ -39,6 +39,36 @@ const FIELD_DEFAULTS: Record<string, string> = {
|
|
|
39
39
|
'lookup.nextPage': 'Next page',
|
|
40
40
|
'lookup.jumpToPage': 'Jump to page',
|
|
41
41
|
'lookup.retry': 'Retry',
|
|
42
|
+
// objectstack#3821 — sharing-rule authoring widgets (object-ref /
|
|
43
|
+
// recipient-picker / filter-condition). The recipient placeholder is keyed
|
|
44
|
+
// PER TYPE rather than interpolating the enum value into an English
|
|
45
|
+
// sentence, which no locale could translate.
|
|
46
|
+
'fields.objectRef.loading': 'Loading objects…',
|
|
47
|
+
'fields.objectRef.placeholder': 'Select an object',
|
|
48
|
+
'fields.objectRef.search': 'Search objects…',
|
|
49
|
+
'fields.objectRef.empty': 'No objects found',
|
|
50
|
+
'fields.recipient.selectTypeFirst': 'Select a recipient type first.',
|
|
51
|
+
'fields.recipient.loading': 'Loading…',
|
|
52
|
+
'fields.recipient.search': 'Search…',
|
|
53
|
+
'fields.recipient.empty': 'No matches',
|
|
54
|
+
'fields.recipient.select': 'Select a recipient',
|
|
55
|
+
'fields.recipient.selectUser': 'Select a user',
|
|
56
|
+
'fields.recipient.selectTeam': 'Select a team',
|
|
57
|
+
'fields.recipient.selectBusinessUnit': 'Select a business unit',
|
|
58
|
+
'fields.recipient.selectPosition': 'Select a position',
|
|
59
|
+
'fields.recipient.selectUnitAndSubordinates': 'Select a business unit',
|
|
60
|
+
'fields.filterCondition.selectObjectFirst': 'Select an object first.',
|
|
61
|
+
// objectstack#3896 — this used to be 'All records'. An empty criteria never
|
|
62
|
+
// meant "share everything"; it meant the predicate was missing, and the
|
|
63
|
+
// sharing evaluator failed open on it. Such a rule is now refused on save
|
|
64
|
+
// and shares nothing, so say that rather than advertise the old bug.
|
|
65
|
+
'fields.filterCondition.noCriteria': 'No criteria — this rule shares nothing',
|
|
66
|
+
'fields.filterCondition.criteriaRequired':
|
|
67
|
+
'Add at least one condition. A rule with no criteria would share every record, so it cannot be saved.',
|
|
68
|
+
'fields.filterCondition.invalidJson': 'Invalid JSON — the rule will match no records until fixed.',
|
|
69
|
+
'fields.filterCondition.jsonOnly': 'This criteria can only be edited as JSON',
|
|
70
|
+
'fields.filterCondition.editAsJson': 'Edit as JSON',
|
|
71
|
+
'fields.filterCondition.useVisualBuilder': 'Use visual builder',
|
|
42
72
|
// objectui#2600 B5 — capability picker scope group headers.
|
|
43
73
|
'capability.group.platform': 'Platform',
|
|
44
74
|
'capability.group.org': 'Organization',
|