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

Dropdown::ListItem::Interactive enhancements MAIN #2347

Merged
merged 11 commits into from
Aug 30, 2024
13 changes: 13 additions & 0 deletions .changeset/nice-drinks-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@hashicorp/design-system-codemods": minor
"@hashicorp/design-system-components": minor
---

`Dropdown::ListItem::Interactive`
zamoore marked this conversation as resolved.
Show resolved Hide resolved
- Adds a yielded block.
- Yields the `Hds::Badge` component.
- Deprecates the `@text` argument.
Copy link
Contributor

@MelSumner MelSumner Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: why deprecate @text? Doesn't that make this component different from the same named components not in the dropdown?

FWIW, I realize there can never be an "icon-only" version of a dropdown::listitem, so we don't have to worry about aria-label in this case. So pragmatically, this change is fine from that perspective. However, one of our rather enforced rules was to make components the same as much as possible, and this is a departure from that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you asking about if it differs from Hds::Interactive? That component doesn't accept @text either, so one of the motivators was to make the API the same afaik.


New codemod: `v4/dropdown-list-item-interactive`
- Converts `Dropdown::ListItem::Interactive` `@text` arguments to content within a yielded block.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# v4/icon

## Usage

To run this codemod in your project using `npx`, you would run the following:

```bash
npx @hashicorp/design-system-codemods v4/dropdown-list-item-interactive path/to/some/glob/**/*.hbs
```

## Local usage

To run this codemod in this repository (even before publishing it), you would run the following from the root directory of this repository:

```bash
node ./packages/codemods/bin/cli.js v4/dropdown-list-item-interactive path/to/some/glob/**/*.hbs
```

## Input
zamoore marked this conversation as resolved.
Show resolved Hide resolved

```hbs
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#" @text="Edit" />
</Hds::Dropdown>
```

## Output

```hbs
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#">Edit</dd.Interactive>
</Hds::Dropdown>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text='Title text' />
<dd.Interactive @href='#' @text={{or @text (t "components.generate-service-principal-key-button.default-text")}} />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text='Title text' />
<dd.Interactive @href='#'>{{or @text (t "components.generate-service-principal-key-button.default-text")}}</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text='Title text' />
<dd.Interactive @href='#' @text={{if
this.isRunningStatement
(t status.text date=(format-utc-date this.dateToBeCharged))
(t status.text)
}} />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text='Title text' />
<dd.Interactive @href='#'>{{if this.isRunningStatement (t status.text date=(format-utc-date this.dateToBeCharged)) (t status.text)}}</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#" @text={{if this.conditional this.testValue1 this.testValue2}} />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#">{{if this.conditional this.testValue1 this.testValue2}}</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#" @text={{test-helper this.value this.value2}} />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#">{{test-helper this.value this.value2}}</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#" @text={{123}} />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#">{{123}}</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#" @text={{"123"}} />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#">{{"123"}}</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#" @text={{this.testValue.val}} />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Title @text="Title text" />
<dd.Interactive @href="#">{{this.testValue.val}}</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Interactive @href="#" @text="Edit" />
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Dropdown as |dd|>
<dd.Interactive @href="#">Edit</dd.Interactive>
</Hds::Dropdown>
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

const CODEMOD_ANALYSIS = process.env.CODEMOD_ANALYSIS;

module.exports = function ({ source /*, path*/ }, { parse, visit }) {
const ast = parse(source);

return visit(ast, (env) => {
let { builders: b } = env.syntax;

return {
ElementNode(node) {
if (node.type === 'ElementNode' && node.tag === 'Hds::Dropdown') {
let hasUpdatedChildren = false;
let processedChildren;

if (node.blockParams && node.blockParams.length > 0) {
const asPrefix = node.blockParams[0];

processedChildren = [];

if (node.children) {
node.children.forEach((child) => {
let updatedChild;
let isProcessed = false;

if (child.type === 'ElementNode' && child.tag === `${asPrefix}.Interactive`) {
const textAttr = child.attributes.find((a) => a.name === '@text');

if (textAttr) {
const childOutputAttributes = child.attributes.filter(
(a) => a.name !== '@text'
);

const isHandlebarsAttr = textAttr.value.type === 'MustacheStatement';

let children = [];

if (isHandlebarsAttr) {
if (textAttr.value.path.type === 'NumberLiteral') {
children = [b.mustache(b.number(textAttr.value.path.value))];
} else if (textAttr.value.path.type === 'StringLiteral') {
children = [b.mustache(b.string(textAttr.value.path.value))];
} else {
children = [
b.mustache(
textAttr.value.path.original,
[...textAttr.value.params],
textAttr.value.hash
),
];
}
} else {
children = [b.text(textAttr.value.chars)];
}

updatedChild = b.element(
{ name: child.tag, selfClosing: false },
{
children,
attrs: childOutputAttributes,
modifiers: child.modifiers,
blockParams: child.blockParams,
}
);

isProcessed = true;
} else {
updatedChild = child;
}
}

processedChildren.push(updatedChild || child);
hasUpdatedChildren = hasUpdatedChildren || isProcessed;
});
}
}

if (hasUpdatedChildren && !CODEMOD_ANALYSIS) {
return [
b.element(
{ name: node.tag, selfClosing: false },
{
attrs: node.attributes,
children: processedChildren || node.children,
modifiers: node.modifiers,
blockParams: node.blockParams,
}
),
];
}
}
},
};
});
};

module.exports.type = 'hbs';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

'use strict';

const { runTransformTest } = require('codemod-cli');

runTransformTest({
name: 'v4/dropdown-list-item-interactive',
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
<Hds::Icon @name="loading" />
</div>
<Hds::Text::Body @tag="div" @size="100" @weight="regular" class="hds-dropdown-list-item__interactive-text">
{{this.text}}
{{#if (has-block)}}
{{yield (hash Badge=(component "hds/badge" size="small"))}}
{{else}}
{{this.text}}
{{/if}}
</Hds::Text::Body>
</div>
{{else}}
Expand All @@ -30,7 +34,11 @@
</span>
{{/if}}
<Hds::Text::Body class="hds-dropdown-list-item__interactive-text" @tag="span" @size="200" @weight="medium">
{{this.text}}
{{#if (has-block)}}
{{yield (hash Badge=(component "hds/badge" size="small"))}}
{{else}}
{{this.text}}
{{/if}}
</Hds::Text::Body>
{{#if @trailingIcon}}
<span class="hds-dropdown-list-item__interactive-icon hds-dropdown-list-item__interactive-icon--trailing">
Expand Down
Loading