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

feat: prop get set new #6050

Merged
merged 11 commits into from
Nov 26, 2024

Conversation

johnjenkins
Copy link
Contributor

@johnjenkins johnjenkins commented Nov 20, 2024

Allowing the @Prop decorator to use get / set provides many quality of life improvements for developers and can improve the api of their components (see linked issues for many valid, uncontested use cases).

Validation & Guards. Transforming data. Read-only properties. All things that may be achievable at the moment but can be extremely cumbersome to implement.

What is the current behavior?

@Prop() decorated attributes cannot be used with get() or set()

GitHub Issue Number: Fixes #1359

What is the new behavior?

/**
 * This prop will be readonly 
 * (and will automatically get '@readonly' added to the docs description)
*/
@Prop()
get myReadonlyProp() {
  return 'a string';
}
// getter & setter with validation
private _agent: 'xxx' | 'James Pond Robocod' |  'James Bond' = 'James Bond'; 
@Prop()
get agent() {
  return this._agent;
}
set agent(agent: string) {
  if (
       typeof agent === 'string' && 
       (agent === 'xxx' || agent === 'James Pond Robocod')
  ) this._agent = agent;
}

Documentation

@Prop decorator attributes: attribute and reflect work as expected.

Prop setters work as expected in relation to @Watch decorated methods (the Watch method will only be called after the setter has actually changed the property value).

get() only properties will be marked as @readonly in the JSDoc comment.

Known limitations

When using the dist / 'lazy' bundle, If an initial value is set (via an attribute for example) in-order to 'go through' the class' set() method, Stencil can only do so after lazily fetching / loading the class component. This means one initial render / tick before being able to set the incoming, initial value.

This limitation does not exist on the dist-custom-elements/ non-lazy bundle

ionic-team/stencil-site#1496

Does this introduce a breaking change?

  • Yes
  • No

Testing

  • Linked to local projects with the instructions found in the Contributing guidelines
  • Stencil unit / jest tests
  • Stencil e2e / wdio tests

Other information

@johnjenkins johnjenkins marked this pull request as ready for review November 21, 2024 17:37
@johnjenkins johnjenkins requested a review from a team as a code owner November 21, 2024 17:37
@johnjenkins johnjenkins mentioned this pull request Nov 21, 2024
12 tasks
@christian-bromann
Copy link
Member

@johnjenkins great work 👏 do you think this feature needs to amendments to the Stencil docs?

@johnjenkins
Copy link
Contributor Author

@christian-bromann done :)

ionic-team/stencil-site#1496

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

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

LGTM 👍 great work 👏

@christian-bromann christian-bromann added this pull request to the merge queue Nov 26, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 26, 2024
@christian-bromann christian-bromann added this pull request to the merge queue Nov 26, 2024
Merged via the queue into ionic-team:main with commit 7ecb599 Nov 26, 2024
88 checks passed
@dogoku
Copy link

dogoku commented Nov 26, 2024

@johnjenkins you legend! 🎉

@johnjenkins johnjenkins deleted the feat-prop-get-set-new branch November 26, 2024 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@Prop decorator does not work on a getter/setter
3 participants