reactive_forms icon indicating copy to clipboard operation
reactive_forms copied to clipboard

[Suggestion] ReactiveTouchListenableBuilder

Open naamapps opened this issue 4 years ago • 10 comments

Hey, Just like there is a ReactiveValueListenableBuilder and ReactiveStatusListenableBuilder, I think you should add ReactiveTouchListenableBuilder because there is an option to listen to the stream events when the input is touched an untouched. Thanks

naamapps avatar Apr 25 '21 13:04 naamapps

Hi @naamapps,

What you are telling us makes complete sense. I have always been adding new features progressively while they are needed. So if you have some experience or use a case in which you were needed this feature, just let me know with examples, etc. And we can find the best solution.

Best regards

joanpablo avatar Apr 28 '21 06:04 joanpablo

Hey @joanpablo, Sorry I didn't write an example. My use case for this is rebuilding a widget when a form control has errors. So when touching the form control I check in the builder if the form control has errors or not.

naamapps avatar Apr 28 '21 06:04 naamapps

Hi @naamapps,

Well in that particular case, if you want to rebuild a widget if the control has errors, then you can use the ReactiveStatusListenableBuilder. Let me explain myself. When a control has errors (or not) then the status changes to ControlStatus.invalid or ControlStatus.valid. So if the control is INVALID to not you just update the view with the information you require. I mean you can check the status of the control to know if the control has errors or not, of course, you can combine the status with the hasErrors property.

Let me know your thoughts.

joanpablo avatar Apr 28 '21 10:04 joanpablo

@joanpablo , What I mean is that I want to know when the error message is on screen. Because the control has an error from the start (required control - null from the start). I need to know when the error is shown, so the error is shown once the user touched the input and the control has errors. That is why I need to listen to touched events.

naamapps avatar Apr 28 '21 10:04 naamapps

Hi @naamapps,

I understand now, you want to know if the control is touched because you want to know if the error is displaying in the view. But what if you just listen for state with the ReactiveStatusListenableBuilder and ask if the control is touched calling the FormControl.touched property?

joanpablo avatar Apr 28 '21 11:04 joanpablo

@joanpablo, I tried it. It is not working because the control is already invalid and its not rebuilding when unfocusing from code for some reason.

I wrote the ReactiveTouchListenableBuilder by myself - it is working as I would like it to be. It is just like the other listeners, just replaced the name and the stream we listen to.

And also, it can't harm adding it to the package :)

naamapps avatar Apr 28 '21 11:04 naamapps

@naamapps I understand. Actually, if the state of the control doesn't change (the control is always invalid) then the widget will not rebuild, but as far as I understand you only need to know if the control is invalid|valid and touched (even if the control is touched you need to ask for both properties Status|HasErrors and if it is touched).

So if I was you I will definitely try with ReactiveStatusListenableBuilder and ask if the control is touched or hasErrors. If you remove the focus programmatically it will not trigger the rebuild because theoretically, the status states the same, no matter if the control has focus or not, the control is valid or invalid (of course you can also listen for valueChanges instead for statusChanges, and you can ask for changes of the control or changes in the complete FormGroup, it depends on your specific use case).

As I always say, don't make me wrong I'm not rejecting to include e new feature. I just want to help you while using Reactive Forms and find the best possible solution.

:)

joanpablo avatar Apr 28 '21 11:04 joanpablo

I'm very happy that you and many other developers are using Reactive Forms and are helping me to improve it with all these issues and suggestions.

joanpablo avatar Apr 28 '21 11:04 joanpablo

@joanpablo I really appreciate your dedication and hard work!

I did try with ReactiveStatusListenableBuilder and even with ReactiveValueListenableBuilder, neither of the worked. I created ReactiveTouchListenableBuilder locally and it did work. So I figured it will be best to include it in the package.

I Just suggested you to include it. Either way, I think the solution for me is to listen to touch events.

naamapps avatar Apr 28 '21 14:04 naamapps