Skip to content

Commit

Permalink
Allow variable types without subfields
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Jan 4, 2025
1 parent 73ea4ad commit 9977708
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@
label={hasVariableTypes ? typeConfig?.label || typeConfig?.name : ''}
controlId="list-{widgetId}-item-{index}-body"
{expanded}
toggleExpanded={() => {
syncExpanderStates({ [expandedKeyPath]: !expanded });
}}
toggleExpanded={typeConfig?.fields?.length
? () => syncExpanderStates({ [expandedKeyPath]: !expanded })
: undefined}
removeButtonVisible={true}
removeButtonDisabled={isDuplicateField}
remove={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@
{@const subFields = subFieldName
? (types?.find(({ name }) => name === subFieldName)?.fields ?? [])
: (fields ?? (field ? [field] : []))}
<section class="subsection" bind:this={wrappers[index]}>
{#await waitForVisibility(wrappers[index]) then}
{#each subFields as subField (subField.name)}
{#await sleep(0) then}
<FieldPreview
keyPath={field ? `${keyPath}.${index}` : `${keyPath}.${index}.${subField.name}`}
{locale}
fieldConfig={subField}
/>
{/await}
{/each}
{/await}
</section>
{#if subFields.length}
<section class="subsection" bind:this={wrappers[index]}>
{#await waitForVisibility(wrappers[index]) then}
{#each subFields as subField (subField.name)}
{#await sleep(0) then}
<FieldPreview
keyPath={field ? `${keyPath}.${index}` : `${keyPath}.${index}.${subField.name}`}
{locale}
fieldConfig={subField}
/>
{/await}
{/each}
{/await}
</section>
{/if}
{/each}
{:else if Array.isArray(currentValue) && currentValue.length}
<p lang={locale} dir="auto">{listFormatter.format(currentValue)}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@
label={hasVariableTypes ? typeConfig?.label || typeConfig?.name : ''}
controlId="object-{widgetId}-item-list"
expanded={parentExpanded}
toggleExpanded={() => {
syncExpanderStates({ [parentExpandedKeyPath]: !parentExpanded });
}}
toggleExpanded={typeConfig?.fields?.length
? () => syncExpanderStates({ [parentExpandedKeyPath]: !parentExpanded })
: undefined}
removeButtonVisible={hasVariableTypes}
removeButtonDisabled={addButtonDisabled}
remove={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
export let expanded;
/**
* @type {() => void}
* @type {(() => void) | undefined}
*/
export let toggleExpanded;
/**
Expand All @@ -41,11 +41,12 @@
<div role="none">
<Button
size="small"
disabled={!toggleExpanded}
aria-label={expanded ? $_('collapse') : $_('expand')}
aria-expanded={expanded}
aria-controls={controlId}
onclick={() => {
toggleExpanded();
toggleExpanded?.();
}}
>
{#snippet startIcon()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
$: subFields = (hasVariableTypes ? typeConfig?.fields : fields) ?? [];
</script>
{#if hasValues}
{#if hasValues && subFields.length}
<section class="subsection" bind:this={wrapper}>
{#await waitForVisibility(wrapper) then}
{#each subFields as subField (subField.name)}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/services/contents/draft/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const parseField = ({ field, keyPath, keyPathList }) => {
keyPathList.push(isList ? `${keyPath}.*.${typeKey}` : `${keyPath}.${typeKey}`);

types.forEach((type) => {
type.fields.forEach((subField) => {
type.fields?.forEach((subField) => {
parseField({
field: subField,
keyPath: isList ? `${keyPath}.*.${subField.name}` : `${keyPath}.${subField.name}`,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@
* @property {string} name - Type name.
* @property {string} [widget] - Widget type. `object` only.
* @property {string} [summary] - Template of the label to be displayed on the collapsed UI.
* @property {Field[]} fields - Nested fields.
* @property {Field[]} [fields] - Nested fields.
* @see https://decapcms.org/docs/variable-type-widgets/
*/

Expand Down

0 comments on commit 9977708

Please sign in to comment.