timdeschryver / ng-signal-forms

105 stars 7 forks source link

Setting values #26

Open timdeschryver opened 8 months ago

timdeschryver commented 8 months ago

We should support an API to update/set/patch the current value and/or controls of a form. This is already supported for form fields, but not for form groups.

michael-small commented 3 months ago

Do you have an idea for the API surface, of a set for starters? That is what I would think would be the first method to base things off of. Control values as WritableSignal<Value> naturally can be .set(...), but with form groups being Signal<UnwrappedFormGroup<Fields>>, then I imagine some method would be exposed on the group itself.

Provided I am correct in that assumption, A) What would you name the setter? B) Would the setter essentially just map the field values with whatever nesting level of the controls?

In my head it would be like

  form = createFormGroup<{ name: string; age: number | null }>({
    name: 'Alice',
    age: null,
  });

  someMethodThatSetsValue() {
    // Verbose name, but I imagine `.set()` would be confusing since it isn't a `WritableSignal`
    this.form.setFormGroupValue({ name: 'Jerry', age: 20 })
  }
timdeschryver commented 3 months ago

That seems good to me @michael-small ! For naming, I like the simplicity of set to be honest, but I'm also fine with setFormGroupValue.

michael-small commented 3 months ago

That seems good to me @michael-small !

I'll take a look into this approach then.

For naming, I like the simplicity of set to be honest, but I'm also fine with setFormGroupValue.

For the time being with my attempt I'll just start with the more verbose phrasing and if I actually get an implementation going then I suppose we would re-evaluate. Interested in hearing from anyone else reading this, since we are 1 vs 1 leaning on one solution but open to the other (minus the fact you are the owner and I am just some random person lol).

Edit: as an aside, it was nice to hear about this library referenced at different times at ng-conf this year. @timdeschryver did you hear some of the news/buzz on forms from the conference? Edit 2: or, were you there and I didn't see you?

timdeschryver commented 3 months ago

Oh cool, that's nice to hear. I wasn't at ng-conf and I wasn't aware that it was talked about in the hallway.

michael-small commented 3 months ago

Update on this issue: I have a somewhat working version of the group setter going. I think it is rather naive and missing some things, but it is coming along. I am comparing it with a reactive form on the same page with the form group filler and individual control prefills. I'm sure of course the assumptions are a bit different, I'll probably have questions on that later.

Aside about ng-conf: Yeah, ng-conf was nice. There was a lot of discussion about forms. It seems like all of the attempts to integrate forms with signals, a mix of template and reactive forms like this library, is what it sounds like to me that some people are speculating is the future. That's what I got from random chatter and gossip, and probably a lot of my bias in interpreting it. One of my favorite questions from the Q&A was about the future of forms. It sounds like the more immediate plan is to "bolt on" signals to the existing API, and then the future of the forms API is quite up for discussion and under wraps until then.