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

Allow non-enum Input in components #2093

Closed
wants to merge 4 commits into from
Closed

Conversation

MortenGregersen
Copy link
Contributor

Which issue does this PR close?

This PR closes #1985

What is the new behavior?

Everywhere I could find a component with an enum-based Input, the type declaration is changed so it now also takes one of the cases of the enum as string. One exception is ButtonSize, where the name and value of the enum cases aren't the identical. In that case, the enum cases as strings are added to the union directly.

This change should not be a breaking change, as it just adds to the list of types/values that can be used on Input properties.

Does this PR introduce a breaking change?

  • Yes
  • No

Checklist:

The following tasks should be carried out in sequence in order to follow the process of contributing correctly.

Reminders

  • Make sure you have implemented tests following the guidelines in: "The good: Test".
  • Make sure you have updated the cookbook with examples and showcases (for bug fixes, enhancements & new components).

Review

  • Do a self-review.
  • Request that the changes are code-reviewed
  • Request that the changes are UX reviewed (only necessary if your PR introduces visual changes)

When the pull request has been approved it will be automatically merged to master via automerge.

@MortenGregersen MortenGregersen self-assigned this Mar 7, 2022
@github-actions github-actions bot temporarily deployed to pr-non-enum-input March 7, 2022 12:44 Inactive
Copy link
Contributor

@MadsBuchmann MadsBuchmann left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! 🎉
Unfortunately I can't approve it yet - see comment.

@@ -21,7 +21,7 @@ export class AvatarComponent {
@Input() shadow: boolean;
@Input() text: string;
@Input() overlay: boolean;
@Input() size: AvatarSize = AvatarSize.SM;
@Input() size: AvatarSize | keyof AvatarSize = AvatarSize.SM;
Copy link
Contributor

@MadsBuchmann MadsBuchmann Mar 8, 2022

Choose a reason for hiding this comment

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

Unfortunately keyof AvatarSize returns all of the keys for a string object (i think it is a string object - not 100% sure).

I've created an example here: link to typescript playground.

Notice the type of TestType
Screenshot 2022-03-08 at 14 52 58

I think we need something like valueof AvatarSize (unfortunately i do not think valueof exists...).

This comment goes for all of the places where keyof has been used.

Copy link
Contributor Author

@MortenGregersen MortenGregersen Mar 9, 2022

Choose a reason for hiding this comment

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

It is only the case where the case in the enum maps to a string. I already fixed it for ButtonSize, but didn't see it for AvatarSize. I found 2 others.

@MadsBuchmann MadsBuchmann self-assigned this Mar 8, 2022
Copy link
Contributor

@MadsBuchmann MadsBuchmann left a comment

Choose a reason for hiding this comment

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

I've marked some values which resolves wrongly.

But in general i do not think creating a union type by explicitly writing out the string values of the enum is a good solution as it introduces duplication.

I've however discovered that we might be able to use template literal types to solve this!

I've taken the liberty of using your branch as the base branch for a new PR resolving your issue using those.

Unfortunately our version of Prettier does not support template literal types as it is quite old (1.19.1)! I will discuss with the rest of the team tomorrow if we should look into upgrading prettier.

@@ -38,7 +38,7 @@ export class InputComponent implements OnChanges {

@HostBinding('class')
@Input()
size: InputSize = InputSize.large;
size: InputSize | keyof InputSize = InputSize.large;
Copy link
Contributor

Choose a reason for hiding this comment

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

Resolves to:

(property) InputComponent.size: number | InputSize | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | ... 29 more ... | "padEnd"

@@ -42,7 +42,7 @@ export class PopoverComponent implements AfterViewInit, OnDestroy {
wrapperElement: ElementRef<HTMLDivElement>;

@Input()
popout: HorizontalDirection = HorizontalDirection.right;
popout: HorizontalDirection | keyof HorizontalDirection = HorizontalDirection.right;
Copy link
Contributor

Choose a reason for hiding this comment

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

Resolves to:

(property) PopoverComponent.popout: number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | ... 28 more ... | HorizontalDirection

@@ -28,7 +28,7 @@ export class SegmentedControlComponent {
}
}

@Input() mode: SegmentedControlMode = SegmentedControlMode.default;
@Input() mode: SegmentedControlMode | keyof SegmentedControlMode = SegmentedControlMode.default;
Copy link
Contributor

Choose a reason for hiding this comment

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

Resolves to:

(property) SegmentedControlComponent.mode: number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | ... 28 more ... | SegmentedControlMode

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

Successfully merging this pull request may close these issues.

[Enhancement] Enable strictTemplate checks
3 participants