Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

feat: enable dynamic validation #510

Merged
merged 2 commits into from
Oct 7, 2022
Merged

feat: enable dynamic validation #510

merged 2 commits into from
Oct 7, 2022

Conversation

baptisteArno
Copy link
Contributor

Summary

Enable dynamic validation. For example, if we need a field to change its required prop based on other fields value.

I created a useField wrapper that changes its data property (to trigger a field reload) if the validate function has changed. We have to do this ourselves because the useField hook from react-final-form doesn't "reload" the field if the validate function changes (See this comment in the source code)

Also, now this new useField hook is calling pickValidators and useValidation itself. It results in a cleaner code IMO 👌

Relevant logs and/or screenshots

Before

CleanShot.2022-09-27.at.13.28.55.mp4

After

CleanShot.2022-09-27.at.13.29.23.mp4

@baptisteArno baptisteArno added the enhancement New feature or request label Sep 27, 2022
@baptisteArno baptisteArno self-assigned this Sep 27, 2022
Copy link
Contributor

@chambo-e chambo-e left a comment

Choose a reason for hiding this comment

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

Worried about our own useField and final-form useField conflict in DX

Wouldn't useOnFieldChange be able to handle this kind of issue ?

What are we trying to implement here ? :)

@baptisteArno
Copy link
Contributor Author

Worried about our own useField and final-form useField conflict in DX

Yeah, that may be concerning, but we do the same with Form. useField isn't publicly exported, it's only used in scw-form code base. I don't think it would impact DX that much!

Wouldn't useOnFieldChange be able to handle this kind of issue ?

From what I understand, useOnFieldChange (hook from our internal code) is just a value change subscriber that triggers a callback when the value change? That's not really the same topic here.

The whole point of this PR is to update input metadata when the validation props are changing. react-final-form explain why they don't do it here

@baptisteArno baptisteArno marked this pull request as draft October 3, 2022 07:58
@baptisteArno
Copy link
Contributor Author

@chambo-e

@DorianMaliszewski suggested renaming the custom useField hook to useFormField to avoid the confusion.

We also discussed other potential solutions. This is the best one we could find.

@baptisteArno baptisteArno requested a review from chambo-e October 5, 2022 09:07
@baptisteArno baptisteArno marked this pull request as ready for review October 6, 2022 08:06
@baptisteArno baptisteArno enabled auto-merge (squash) October 7, 2022 08:54
@baptisteArno baptisteArno merged commit aac01bd into main Oct 7, 2022
@baptisteArno baptisteArno deleted the fix/dynamic-required branch October 7, 2022 08:57
scaleway-bot pushed a commit that referenced this pull request Oct 7, 2022
## [1.32.0](v1.31.1...v1.32.0) (2022-10-07)

### 📦 Chore

* correct storybook theme ([#525](#525)) ([1f71c99](1f71c99))

### 🔁 CI

* use caddy to serve storybook ([#528](#528)) ([2f54878](2f54878))

### ⚙️ Features

* enable dynamic validation ([#510](#510)) ([aac01bd](aac01bd))
@scaleway-bot
Copy link
Collaborator

🎉 This PR is included in version 1.32.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants