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

Angular: Allow usage of all component valid selectors #14230

Merged
merged 2 commits into from
Mar 19, 2021
Merged

Angular: Allow usage of all component valid selectors #14230

merged 2 commits into from
Mar 19, 2021

Conversation

dexster
Copy link
Contributor

@dexster dexster commented Mar 13, 2021

Issue: #14079

What I did

Added checks for all valid angular directive/component selectors to allow templates to be generated correctly.

How to test

  • Is this testable with Jest or Chromatic screenshots? Yes
    Tests added

  • Does this need a new example in the kitchen sink apps?
    Possibly. Examples added but they don't show much. Could be removed?

  • Does this need an update to the documentation?
    No

@dexster dexster requested review from alterx and igor-dv as code owners March 13, 2021 19:42
@shilman shilman changed the title #14079 - Allow usage of all component valid selectors Angular: Allow usage of all component valid selectors Mar 14, 2021
@dexster
Copy link
Contributor Author

dexster commented Mar 14, 2021

Could I have some feedback on the unsuccessful checks please so I can ensure future PR's are successful. Thanks

@shilman
Copy link
Member

shilman commented Mar 14, 2021

@dexster don't worry about the unsuccessful checks 👍

Copy link
Contributor

@ThibaudAV ThibaudAV left a comment

Choose a reason for hiding this comment

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

Very nice work :) with tests and examples ❤️ I added some remarks but nothing blocking. just questions and suggestions

const buildTemplate = (
selector: string
): {
openTag?: string;
Copy link
Contributor

@ThibaudAV ThibaudAV Mar 14, 2021

Choose a reason for hiding this comment

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

Do you think it would be possible to have openTag and closeTag not optional ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

N/A with new commit using the regex you mentioned

};
}
return acc;
}, {});
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think about starting with a default value?
like storybook-default-selector ? (or something like that)
In case of an error it could be a workaround?

or we could display a clearer error if the open/close tag is not found ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

N/A with new commit using the regex you mentioned which should always return a selector.

const templates = [
{
// Match element selectors with optional chained attributes or classes
re: /^([\w\d-_]+)(?:(?:\[([\w\d-_]+)(?:=(.+))?\])|\.([\w\d-_]+))?/,
Copy link
Contributor

@ThibaudAV ThibaudAV Mar 14, 2021

Choose a reason for hiding this comment

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

a long time ago when I was doing Jquery I used this solution (regex) : https://codegolf.stackexchange.com/a/123281
I don't know if you made them yourself but maybe this could improve or simplify 🤷‍♂️ (not sure)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done with modifications


@Component({
selector: 'storybook-attribute-selector[foo]',
template: '<p>Attribute selector</p>',
Copy link
Contributor

@ThibaudAV ThibaudAV Mar 14, 2021

Choose a reason for hiding this comment

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

I find these examples very interesting.

We could display the html output expected in template 🤔 like :

template: `
<h3>Attribute selector</h3>
Selector: "storybook-attribute-selector[foo]" <br>
Generated template: "<storybook-attribute-selector foo></storybook-attribute-selector>"
`,

but maybe it's too much 🤷‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@manuelmeister
Copy link
Contributor

In angular it is valid to have multiple selectors for the same directive/component. How do we handle that? In that case we probably want to be able to choose which selector we want to use.

@dexster
Copy link
Contributor Author

dexster commented Mar 15, 2021

For the purposes of Storybook auto-generated templates I simply use the first selector. If someone wanted to use one of the other selectors from their component I would expect them to use the template property and specify the required selector.

@dexster
Copy link
Contributor Author

dexster commented Mar 16, 2021

Changes made as per feedback

@shilman
Copy link
Member

shilman commented Mar 19, 2021

@dexster @ThibaudAV is this good to merge? if we're going to include it in 6.2 we should probably merge it today. (doesn't mean we can't get it in later, or as a patch, but ideally we'd wrap everything ASAP). thanks!

@dexster
Copy link
Contributor Author

dexster commented Mar 19, 2021

I've made all the updates as per @ThibaudAV recommendations so happy from my side.

@shilman shilman merged commit 21f5032 into storybookjs:next Mar 19, 2021
@shilman
Copy link
Member

shilman commented Mar 19, 2021

Thanks @dexster 🙏 merging and will release later today!

@ThibaudAV
Copy link
Contributor

Coool thanks @dexster 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants