Skip to content

Commit

Permalink
Deal with select field with value/label options to format entry summary
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Oct 13, 2023
1 parent f600678 commit 399000e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
export let currentValue = undefined;
$: ({ options, multiple } = fieldConfig);
$: isObjectArray = Array.isArray(options) && isObject(options[0]);
$: isObjectArray = Array.isArray(options) && options.every((o) => isObject(o));
$: listFormatter = new Intl.ListFormat(locale, { style: 'narrow', type: 'conjunction' });
/**
Expand Down
16 changes: 14 additions & 2 deletions src/lib/services/contents/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getPropertyValue } from '$lib/services/contents/entry';
import { prefs } from '$lib/services/prefs';
import { getDateTimeParts } from '$lib/services/utils/datetime';
import LocalStorage from '$lib/services/utils/local-storage';
import { isObject } from '$lib/services/utils/misc';
import { stripSlashes } from '$lib/services/utils/strings';

const storageKey = 'sveltia-cms.contents-view';
Expand Down Expand Up @@ -154,9 +155,20 @@ export const formatSummary = (collection, entry, content) => {
const relFieldConfig = /** @type {RelationField} */ (fieldConfig);
const refEntries = getEntriesByCollection(relFieldConfig.collection);
const refOptions = getOptions(defaultLocale, relFieldConfig, refEntries);
const label = refOptions.find((option) => option.value === fieldValue)?.label;

return label || fieldValue;
return refOptions.find((o) => o.value === fieldValue)?.label || fieldValue;
}

// Use the label for a select field with value/label options
if (fieldConfig?.widget === 'select') {
const { options } = /** @type {SelectField} */ (fieldConfig);
const isObjectArray = Array.isArray(options) && options.every((o) => isObject(o));

if (isObjectArray) {
const _options = /** @type {{ label: string, value: string }[]} */ (options);

return _options.find((o) => o.value === fieldValue)?.label || fieldValue;
}
}

return fieldValue;
Expand Down

0 comments on commit 399000e

Please sign in to comment.