Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPIKE] Add parent prefixes to nested component options #2892

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions lib/get-macro-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ function addSlugs(option, parent) {
}

function addParentPrefix(option, parent) {
if (option.type === 'array') {
option.name = `${option.name}[]`
}

// Prefix nested option names with parent name to avoid duplicates
// such as summary list row "items" versus card action "items"
if ((!option.isComponent || option.params) && parent?.name) {
option.name = `${parent.name} ${option.name}`
option.name = `${parent.name}.<wbr>${option.name}`
}

if (option.params) {
Expand Down Expand Up @@ -218,25 +222,18 @@ function getMacroOptions(componentName) {
}
]
.concat(
nestedOptions.map(([option]) => {
const names = option.name.split(' ')

// Wrap option name with `<code>` (excluding parents)
names[names.length - 1] = `<code>${names.at(-1)}</code>`

return {
...option,
nestedOptions.map(([option]) => ({
...option,

// Append "objects" to the table caption for arrays of nested objects
// to clarify that the options are for arrays of objects, not arrays
name:
option.type === 'array' && option.params
? `Options for ${names.join(' ')} ${option.type} objects`
: `Options for ${names.join(' ')} ${option.type}`,
// Append "objects" to the table caption for arrays of nested objects
// to clarify that the options are for arrays of objects, not arrays
name:
option.type === 'array' && option.params
? `Options for <code>${option.name}</code> objects`
: `Options for <code>${option.name}</code> ${option.type}`,

options: option.params
}
})
options: option.params
}))
)
.concat(
additionalComponents.map(([option, parent]) => ({
Expand Down
4 changes: 2 additions & 2 deletions views/partials/_example.njk
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
</thead>
<tbody class="govuk-table__body">
{% for option in table.options -%}
{# Option name only, without parent prefix -#}
{% set optionName = option.name.split(" ") | last %}
{# Option name with parent prefix -#}
{% set optionName = option.name.split('.').splice(-2).join('.') %}
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="row">{{ optionName | safe }}</th>
<td class="govuk-table__cell">{{ option.type }}</td>
Expand Down