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

[BUG] Using the Select component with a form control, the component ignores the initial value #551

Open
2 tasks
silviu4444 opened this issue Jan 10, 2025 · 8 comments
Assignees
Labels
bug Something isn't working needs reproduction Need a way to reproduce this

Comments

@silviu4444
Copy link

Please provide the environment you discovered this bug in.

Angular v19
Node v22.11.00
@spartan-ng/cli 0.0.1-alpha.387

Which area/package is the issue in?

select

Description

The select component does not take into consideration the initial value of the form.
A workaround would be to set the form value in the ngAfterViewInit hook.

image

image

image

Please provide the exception or error you saw

No response

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@silviu4444 silviu4444 added the bug Something isn't working label Jan 10, 2025
@silviu4444 silviu4444 changed the title Using the Select component with a form control, the component ignores the initial value [BUG] Using the Select component with a form control, the component ignores the initial value Jan 11, 2025
@goetzrobin
Copy link
Collaborator

@silviu4444 can you create a reproduction for this? This should be working and is actually covered by our e2e tests, so there must be something we are missing

export const ReactiveFormControlWithForAndInitialValue: Story = {

@goetzrobin goetzrobin added the needs reproduction Need a way to reproduce this label Feb 6, 2025
@silviu4444
Copy link
Author

@goetzrobin have u made changes to the hlm select component recently?
I updated the brain package to version 400 and the issue's still there.

If you haven't made updated to the hlm component the issue should reproduce when you initialize the form before the component is mounted (constructor, onInit).

If you made updates lately to the select component than my code might be outdated.

@dev-franco
Copy link

I believe the described issue refers to the visual display of the placeholder when a value is pre-set.

When using formControl, the value is indeed set but the placeholder does not reflect the selected value.
In this case I have a FormGroup with 2 controls emails and role. In the screen below, the role value is set to 'admin', but the placeholder reflects the placeholder I've manually defined as Select Placeholder.

Image

Clicking on the dropdown correctly reflects that the value is indeed set via the checkmark (I guess this is where the tests pass):

Image

Using ngModel works as expected though (as not only it reflects the value, but also updates the placeholder to show the selected value:

Image

Image

@hillin
Copy link
Contributor

hillin commented Feb 13, 2025

I'm pretty sure this problem exists since or before version 0.0.1-alpha.356. We did not went too deep into it, but our workaround is to reset the form group a little bit later after it has been created:

const formGroup = createFormGroup();
setTimeout(() => {
  // at this time, the form group should have been bound to the view
  formGroup.reset();
}, 1);

@thatsamsonkid
Copy link
Collaborator

thatsamsonkid commented Feb 16, 2025

I was able to reproduce the issue in our storybook, I think the issue was 2 fold then we have some mix of static and for-looped options for that story and it's oddly defaulting to the first static option for some reason instead of what's defined in formcontrol. I guess we haven't noticed since it was still initializing with a value but it's a wrong value and i guess the test needs to be looked at because it should be checking visually that both the button and dropdown option are properly displaying the initial value. Also thanks for the example @dev-franco I did see it only happens with reactive form, ng-model is working fine

@thatsamsonkid thatsamsonkid removed the needs reproduction Need a way to reproduce this label Feb 16, 2025
@thatsamsonkid thatsamsonkid added this to the 1.0 Stable Release milestone Feb 16, 2025
@thatsamsonkid thatsamsonkid self-assigned this Feb 16, 2025
@thatsamsonkid
Copy link
Collaborator

Ok so actually maybe I spoke too soon, we do have an odd issue with mixed static and dynamic options, but seems in story itself we had also configured initial value arg incorrectly so that's why it wasn't taking the value. Once I updated the story I see it working fine.

I also created this Stackblitz showing reactive form with initial value working both in single and multi-mode. Now this is initial value defined on form control creation, but is anyone seeing the issue maybe while patching the value or something maybe some time after form control creation? If someone could create a stackblitz with the issue that would be great also

@thatsamsonkid thatsamsonkid added the needs reproduction Need a way to reproduce this label Feb 16, 2025
@silviu4444
Copy link
Author

I believe the described issue refers to the visual display of the placeholder when a value is pre-set.

When using formControl, the value is indeed set but the placeholder does not reflect the selected value. In this case I have a FormGroup with 2 controls emails and role. In the screen below, the role value is set to 'admin', but the placeholder reflects the placeholder I've manually defined as Select Placeholder.

Image

Clicking on the dropdown correctly reflects that the value is indeed set via the checkmark (I guess this is where the tests pass):

Image

Using ngModel works as expected though (as not only it reflects the value, but also updates the placeholder to show the selected value:

Image

Image

This is exactly what I meant

@ashley-hunter
Copy link
Collaborator

ashley-hunter commented Feb 20, 2025

Its interesting, as part of updating the repo to Angular 19 (#612) unit tests for this seem to be failing now, so I wonder is this picking up the issue we are seeing here - now that I have a reproduction I will see if I can find a solution as part of that work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs reproduction Need a way to reproduce this
Projects
Status: No status
Development

No branches or pull requests

6 participants