Skip to content

feat(forms): Allow a FormControl to use initial value as default.#44434

Closed
dylhunn wants to merge 1 commit intoangular:masterfrom
dylhunn:reset-to-initial
Closed

feat(forms): Allow a FormControl to use initial value as default.#44434
dylhunn wants to merge 1 commit intoangular:masterfrom
dylhunn:reset-to-initial

Conversation

@dylhunn
Copy link
Contributor

@dylhunn dylhunn commented Dec 10, 2021

Allow a FormControl to be reset to its initial value. Provide this feature via a new option in a FormControlOptions interface, based on AbstractControlOptions.

Also, expose the default value as part of the public API. This is part of a feature that has been requested elsewhere (e.g. in #19747).

This was originally proposed as part of typed forms. As discussed in the GDE session (and after with akushnir/alxhub), it is likely better to just reuse the initial value rather than accepting an additional default.

It is desirable to land this separately in order to reduce the scope of the typed forms PR, and make it a types-only change.

Edit: This change will also help us in a long-term shift to reduce the use of null in Forms, and adopt safer defaults.

Pertains to issue #13721.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

It is impossible to specify the value to which a control will reset ahead of time.

Issue Number: #13721

What is the new behavior?

It is now possible to cause a control to reset to its initial value. This will be useful for simplifying the types in strongly typed reactive forms.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@dylhunn dylhunn added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews area: forms cross-cutting: types target: minor This PR is targeted for the next minor release labels Dec 10, 2021
@ngbot ngbot bot modified the milestone: Backlog Dec 10, 2021
@dylhunn dylhunn force-pushed the reset-to-initial branch 3 times, most recently from 276172d to 2a1062e Compare December 10, 2021 22:12
@dylhunn dylhunn marked this pull request as ready for review December 10, 2021 22:13
@dylhunn dylhunn added action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Dec 10, 2021
@dylhunn dylhunn force-pushed the reset-to-initial branch 3 times, most recently from a10c14e to 6f2c319 Compare December 10, 2021 22:17
@dylhunn
Copy link
Contributor Author

dylhunn commented Dec 11, 2021

Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

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

LGTM 🍪

reviewed-for: public-api

@dylhunn dylhunn changed the title feat(forms): Allow a FormControl to be reset to its initial value. feat(forms): Allow a FormControl to use initial value as default. Dec 13, 2021
@pullapprove pullapprove bot requested a review from AndrewKushnir December 13, 2021 19:37
@dylhunn
Copy link
Contributor Author

dylhunn commented Dec 13, 2021

@alxhub I renamed the key to initialValueIsDefault as you suggested on our call, as well as exporting the defaultValue field for public usage.

@dylhunn dylhunn force-pushed the reset-to-initial branch 2 times, most recently from df3a1e7 to 685b8af Compare December 14, 2021 20:34
@mary-poppins

This comment has been minimized.

Comment on lines 1295 to 1304
Copy link
Contributor

Choose a reason for hiding this comment

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

Great catch 👍 I didn't mention this since there is a handling for the "boxed value" in the reset function (within _applyFormState code), but it would also update the disabled status, which is something we need to avoid (so special handling makes sense here).

@dylhunn dylhunn force-pushed the reset-to-initial branch 2 times, most recently from 33335fe to 6940b5e Compare December 14, 2021 21:00
alxhub pushed a commit that referenced this pull request Dec 14, 2021
The aio application expects `FormControl` to have no properties for the purposes of its own internal tests, but this is no longer true after #44434.

PR Close #44479
alxhub pushed a commit that referenced this pull request Dec 14, 2021
The aio application expects `FormControl` to have no properties for the purposes of its own internal tests, but this is no longer true after #44434.

PR Close #44479
@dylhunn
Copy link
Contributor Author

dylhunn commented Dec 14, 2021

(Rebased and all comments addressed)

@dylhunn
Copy link
Contributor Author

dylhunn commented Dec 14, 2021

@alxhub I just did one last push with all the comments addressed!

@mary-poppins
Copy link

You can preview 2a1cd13 at https://pr44434-2a1cd13.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 3ab141c at https://pr44434-3ab141c.ngbuilds.io/.

@dylhunn dylhunn added the action: merge The PR is ready for merge by the caretaker label Dec 14, 2021
@angular angular deleted a comment from ngbot bot Dec 14, 2021
@dylhunn dylhunn dismissed alxhub’s stale review December 14, 2021 23:33

All changes are applied.

Allow a FormControl to be reset to its initial value. Provide this feature via a new option in a FormControlOptions interface, based on AbstractControlOptions.

Also, expose the default value as part of the public API. This is part of a feature that has been requested elsewhere (e.g. in angular#19747).

This was originally proposed as part of typed forms. As discussed in the GDE session (and after with akushnir/alxhub), it is likely better to just reuse the initial value rather than accepting an additional default.

It is desirable to land this separately in order to reduce the scope of the typed forms PR, and make it a types-only change.

Pertains to issue angular#13721.
@mary-poppins
Copy link

You can preview 831a87f at https://pr44434-831a87f.ngbuilds.io/.

@alxhub
Copy link
Member

alxhub commented Dec 15, 2021

This PR was merged into the repository by commit 72092eb.

@alxhub alxhub closed this in 72092eb Dec 15, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jan 15, 2022
@dylhunn dylhunn deleted the reset-to-initial branch April 12, 2022 17:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker action: review The PR is still awaiting reviews from at least one requested reviewer area: forms cross-cutting: types target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants