@byline/admin 3.14.0 → 3.15.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.
@@ -0,0 +1,28 @@
1
+ /**
2
+ * This Source Code is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
+ *
6
+ * Copyright (c) Infonomic Company Limited
7
+ */
8
+ import type { CollectionDefinition, ColumnDefinition, ColumnFormatter, RelationField } from '@byline/core';
9
+ /**
10
+ * Pure text projection of a relation cell: the target's title for a single
11
+ * relation, or "A, B, +N more" for `hasMany`. Unresolved targets are skipped;
12
+ * an all-empty cell yields `null` (the formatter renders a muted dash).
13
+ *
14
+ * `displayField` is the resolved chain
15
+ * (`field.displayField` → target `useAsTitle` → first text field).
16
+ */
17
+ export declare function formatRelationCellText(field: Pick<RelationField, 'hasMany'>, value: unknown, displayField: string | null): string | null;
18
+ /**
19
+ * The built-in `{ component }` column formatter for a `relation` field. Resolves
20
+ * the target collection's `useAsTitle` at render time and renders the cell text.
21
+ */
22
+ export declare function relationColumnFormatter(field: RelationField): ColumnFormatter;
23
+ /**
24
+ * Return a copy of `columns` with {@link relationColumnFormatter} applied to any
25
+ * `relation`-typed column that doesn't already declare its own formatter, so
26
+ * relation cells render target titles by default. Other columns are untouched.
27
+ */
28
+ export declare function applyRelationColumnFormatters(columns: ColumnDefinition[], definition: CollectionDefinition): ColumnDefinition[];
@@ -0,0 +1,47 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { getCollectionDefinition } from "@byline/core";
3
+ import { resolveFallbackDisplayField, resolveRowLabel } from "./relation-display.js";
4
+ const MAX_SHOWN = 3;
5
+ function labelFor(env, displayField) {
6
+ if (null == env || false === env._resolved || null == env.document) return null;
7
+ return resolveRowLabel(env.document, displayField);
8
+ }
9
+ function formatRelationCellText(field, value, displayField) {
10
+ const envelopes = field.hasMany ? Array.isArray(value) ? value : [] : value ? [
11
+ value
12
+ ] : [];
13
+ const labels = envelopes.map((env)=>labelFor(env, displayField)).filter((l)=>null != l && l.length > 0);
14
+ if (0 === labels.length) return null;
15
+ if (!field.hasMany) return labels[0] ?? null;
16
+ const shown = labels.slice(0, MAX_SHOWN);
17
+ const extra = labels.length - shown.length;
18
+ return extra > 0 ? `${shown.join(', ')}, +${extra} more` : shown.join(', ');
19
+ }
20
+ function relationColumnFormatter(field) {
21
+ return {
22
+ component: ({ value })=>{
23
+ const targetDef = getCollectionDefinition(field.targetCollection);
24
+ const displayField = field.displayField ?? targetDef?.useAsTitle ?? resolveFallbackDisplayField(targetDef);
25
+ const text = formatRelationCellText(field, value, displayField);
26
+ return null != text ? /*#__PURE__*/ jsx("span", {
27
+ className: "byline-relation-column",
28
+ children: text
29
+ }) : /*#__PURE__*/ jsx("span", {
30
+ className: "byline-relation-column-empty muted",
31
+ children: "—"
32
+ });
33
+ }
34
+ };
35
+ }
36
+ function applyRelationColumnFormatters(columns, definition) {
37
+ return columns.map((col)=>{
38
+ if (col.formatter) return col;
39
+ const field = definition.fields.find((f)=>f.name === String(col.fieldName));
40
+ if (field?.type === 'relation') return {
41
+ ...col,
42
+ formatter: relationColumnFormatter(field)
43
+ };
44
+ return col;
45
+ });
46
+ }
47
+ export { applyRelationColumnFormatters, formatRelationCellText, relationColumnFormatter };
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useCallback, useEffect, useState } from "react";
3
- import { getCollectionAdminConfig } from "@byline/core";
3
+ import { getCollectionAdminConfig, resolveItemViewColumns } from "@byline/core";
4
4
  import { useTranslation } from "@byline/i18n/react";
5
5
  import { Button, LoaderRing, Modal, Search } from "@byline/ui/react";
6
6
  import classnames from "classnames";
@@ -20,7 +20,7 @@ const RelationPicker = ({ targetCollectionPath, targetDefinition, displayField,
20
20
  const [collectionId, setCollectionId] = useState(null);
21
21
  const { getCollectionDocuments } = useBylineFieldServices();
22
22
  const targetAdminConfig = getCollectionAdminConfig(targetCollectionPath);
23
- const pickerColumns = targetAdminConfig?.picker;
23
+ const pickerColumns = resolveItemViewColumns(targetAdminConfig);
24
24
  useEffect(()=>{
25
25
  if (isOpen) {
26
26
  setQuery('');
@@ -1,9 +1,10 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { resolveItemViewColumns } from "@byline/core";
2
3
  import classnames from "classnames";
3
4
  import { PickerCell, resolveFallbackDisplayField, resolveRowLabel } from "./relation-display.js";
4
5
  import relation_summary_module from "./relation-summary.module.js";
5
6
  function RelationSummary({ targetDefinition, targetAdminConfig, displayField, value, cachedRecord }) {
6
- const pickerColumns = targetAdminConfig?.picker;
7
+ const pickerColumns = resolveItemViewColumns(targetAdminConfig);
7
8
  if (false === value._resolved) return /*#__PURE__*/ jsxs("div", {
8
9
  className: classnames('byline-relation-summary-stack', relation_summary_module.stack),
9
10
  children: [
package/dist/react.d.ts CHANGED
@@ -45,6 +45,7 @@ export * from './fields/image/image-upload-field.js';
45
45
  export * from './fields/local-date-time.js';
46
46
  export * from './fields/locale-badge.js';
47
47
  export * from './fields/numerical/numerical-field.js';
48
+ export * from './fields/relation/relation-column-formatter.js';
48
49
  export * from './fields/relation/relation-field.js';
49
50
  export * from './fields/relation/relation-many-field.js';
50
51
  export * from './fields/relation/relation-picker.js';
package/dist/react.js CHANGED
@@ -16,6 +16,7 @@ export * from "./fields/image/image-upload-field.js";
16
16
  export * from "./fields/local-date-time.js";
17
17
  export * from "./fields/locale-badge.js";
18
18
  export * from "./fields/numerical/numerical-field.js";
19
+ export * from "./fields/relation/relation-column-formatter.js";
19
20
  export * from "./fields/relation/relation-field.js";
20
21
  export * from "./fields/relation/relation-many-field.js";
21
22
  export * from "./fields/relation/relation-picker.js";
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@byline/admin",
3
3
  "private": false,
4
4
  "license": "MPL-2.0",
5
- "version": "3.14.0",
5
+ "version": "3.15.0",
6
6
  "engines": {
7
7
  "node": ">=20.9.0"
8
8
  },
@@ -156,10 +156,10 @@
156
156
  "uuid": "^14.0.0",
157
157
  "zod": "^4.4.3",
158
158
  "zod-form-data": "^3.0.1",
159
- "@byline/auth": "3.14.0",
160
- "@byline/ui": "3.14.0",
161
- "@byline/core": "3.14.0",
162
- "@byline/i18n": "3.14.0"
159
+ "@byline/auth": "3.15.0",
160
+ "@byline/core": "3.15.0",
161
+ "@byline/i18n": "3.15.0",
162
+ "@byline/ui": "3.15.0"
163
163
  },
164
164
  "peerDependencies": {
165
165
  "react": "^19.0.0",
@@ -0,0 +1,119 @@
1
+ /**
2
+ * This Source Code is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
+ *
6
+ * Copyright (c) Infonomic Company Limited
7
+ */
8
+
9
+ import type {
10
+ CollectionDefinition,
11
+ ColumnDefinition,
12
+ ColumnFormatter,
13
+ RelationField,
14
+ } from '@byline/core'
15
+ import { getCollectionDefinition } from '@byline/core'
16
+
17
+ import { resolveFallbackDisplayField, resolveRowLabel } from './relation-display'
18
+
19
+ // ---------------------------------------------------------------------------
20
+ // Built-in list-view column formatter for `relation` fields. Renders the
21
+ // target's title (via the displayField → useAsTitle → first-text chain)
22
+ // instead of a raw `target_document_id`. For `hasMany` relations it renders
23
+ // the first few titles followed by "+N more".
24
+ //
25
+ // Requires the list read to populate relation columns (depth 1) — the list
26
+ // server fn does this for relation columns so each cell value arrives as a
27
+ // populated relation envelope (`{ targetDocumentId, _resolved, document }`)
28
+ // or, for hasMany, an array of them.
29
+ // ---------------------------------------------------------------------------
30
+
31
+ interface RelationEnvelope {
32
+ targetDocumentId?: string
33
+ _resolved?: boolean
34
+ document?: Record<string, any>
35
+ }
36
+
37
+ /** Max titles shown for a hasMany cell before collapsing to "+N more". */
38
+ const MAX_SHOWN = 3
39
+
40
+ function labelFor(
41
+ env: RelationEnvelope | null | undefined,
42
+ displayField: string | null
43
+ ): string | null {
44
+ // Unresolved (deleted / out-of-scope target) or not populated → no title.
45
+ if (env == null || env._resolved === false || env.document == null) return null
46
+ return resolveRowLabel(env.document, displayField)
47
+ }
48
+
49
+ /**
50
+ * Pure text projection of a relation cell: the target's title for a single
51
+ * relation, or "A, B, +N more" for `hasMany`. Unresolved targets are skipped;
52
+ * an all-empty cell yields `null` (the formatter renders a muted dash).
53
+ *
54
+ * `displayField` is the resolved chain
55
+ * (`field.displayField` → target `useAsTitle` → first text field).
56
+ */
57
+ export function formatRelationCellText(
58
+ field: Pick<RelationField, 'hasMany'>,
59
+ value: unknown,
60
+ displayField: string | null
61
+ ): string | null {
62
+ const envelopes: RelationEnvelope[] = field.hasMany
63
+ ? Array.isArray(value)
64
+ ? (value as RelationEnvelope[])
65
+ : []
66
+ : value
67
+ ? [value as RelationEnvelope]
68
+ : []
69
+
70
+ const labels = envelopes
71
+ .map((env) => labelFor(env, displayField))
72
+ .filter((l): l is string => l != null && l.length > 0)
73
+
74
+ if (labels.length === 0) return null
75
+ if (!field.hasMany) return labels[0] ?? null
76
+
77
+ const shown = labels.slice(0, MAX_SHOWN)
78
+ const extra = labels.length - shown.length
79
+ return extra > 0 ? `${shown.join(', ')}, +${extra} more` : shown.join(', ')
80
+ }
81
+
82
+ /**
83
+ * The built-in `{ component }` column formatter for a `relation` field. Resolves
84
+ * the target collection's `useAsTitle` at render time and renders the cell text.
85
+ */
86
+ export function relationColumnFormatter(field: RelationField): ColumnFormatter {
87
+ return {
88
+ component: ({ value }) => {
89
+ const targetDef = getCollectionDefinition(field.targetCollection)
90
+ const displayField =
91
+ field.displayField ?? targetDef?.useAsTitle ?? resolveFallbackDisplayField(targetDef)
92
+ const text = formatRelationCellText(field, value, displayField)
93
+ return text != null ? (
94
+ <span className="byline-relation-column">{text}</span>
95
+ ) : (
96
+ <span className="byline-relation-column-empty muted">—</span>
97
+ )
98
+ },
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Return a copy of `columns` with {@link relationColumnFormatter} applied to any
104
+ * `relation`-typed column that doesn't already declare its own formatter, so
105
+ * relation cells render target titles by default. Other columns are untouched.
106
+ */
107
+ export function applyRelationColumnFormatters(
108
+ columns: ColumnDefinition[],
109
+ definition: CollectionDefinition
110
+ ): ColumnDefinition[] {
111
+ return columns.map((col) => {
112
+ if (col.formatter) return col
113
+ const field = definition.fields.find((f) => f.name === String(col.fieldName))
114
+ if (field?.type === 'relation') {
115
+ return { ...col, formatter: relationColumnFormatter(field as RelationField) }
116
+ }
117
+ return col
118
+ })
119
+ }
@@ -9,7 +9,7 @@
9
9
  import { useCallback, useEffect, useState } from 'react'
10
10
 
11
11
  import type { CollectionAdminConfig, CollectionDefinition } from '@byline/core'
12
- import { getCollectionAdminConfig } from '@byline/core'
12
+ import { getCollectionAdminConfig, resolveItemViewColumns } from '@byline/core'
13
13
  import { useTranslation } from '@byline/i18n/react'
14
14
  import { Button, LoaderRing, Modal, Search } from '@byline/ui/react'
15
15
  import cx from 'classnames'
@@ -102,7 +102,7 @@ export const RelationPicker = ({
102
102
 
103
103
  const targetAdminConfig: CollectionAdminConfig | null =
104
104
  getCollectionAdminConfig(targetCollectionPath)
105
- const pickerColumns = targetAdminConfig?.picker
105
+ const pickerColumns = resolveItemViewColumns(targetAdminConfig)
106
106
 
107
107
  // Reset local state each time the modal opens so prior queries don't leak.
108
108
  useEffect(() => {
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  import type { CollectionAdminConfig, CollectionDefinition } from '@byline/core'
10
+ import { resolveItemViewColumns } from '@byline/core'
10
11
  import cx from 'classnames'
11
12
 
12
13
  import { PickerCell, resolveFallbackDisplayField, resolveRowLabel } from './relation-display'
@@ -17,7 +18,7 @@ import styles from './relation-summary.module.css'
17
18
  //
18
19
  // Rendering priority (mirrors RelationPicker so the tile and picker rows
19
20
  // look identical):
20
- // 1. target `CollectionAdminConfig.picker` columns (full fidelity, with
21
+ // 1. target `CollectionAdminConfig.itemView` columns (full fidelity, with
21
22
  // formatters — e.g. MediaThumbnail + title)
22
23
  // 2. explicit `displayField` prop (from source schema's RelationField)
23
24
  // 3. `CollectionDefinition.useAsTitle`
@@ -62,7 +63,7 @@ export function RelationSummary({
62
63
  value,
63
64
  cachedRecord,
64
65
  }: RelationSummaryProps) {
65
- const pickerColumns = targetAdminConfig?.picker
66
+ const pickerColumns = resolveItemViewColumns(targetAdminConfig)
66
67
 
67
68
  // Unresolved (deleted target).
68
69
  if (value._resolved === false) {
package/src/react.ts CHANGED
@@ -48,6 +48,7 @@ export * from './fields/image/image-upload-field.js'
48
48
  export * from './fields/local-date-time.js'
49
49
  export * from './fields/locale-badge.js'
50
50
  export * from './fields/numerical/numerical-field.js'
51
+ export * from './fields/relation/relation-column-formatter.js'
51
52
  export * from './fields/relation/relation-field.js'
52
53
  export * from './fields/relation/relation-many-field.js'
53
54
  export * from './fields/relation/relation-picker.js'