-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add isVisible
option to fields within DataForm
#65826
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
/** | ||
* Callback used to decide if a field should be displayed. | ||
*/ | ||
isVisible?: ( item: Item ) => boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both of these options may not make sense in the DataViews
context? In which case we should create a specific DataFormField
type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have the same with isValid
or sort
. It's tempting to create separate "field types" for DataViews and DataForm but I don't think it's worth it right now. The consumers of this code will declare fields only once for both.
@youknowriad and @oandregal here is the initial proposal for the |
packages/dataviews/src/components/dataform-field-visibility/index.tsx
Outdated
Show resolved
Hide resolved
packages/dataviews/src/components/dataform/stories/index.story.tsx
Outdated
Show resolved
Hide resolved
@louwie17 I've left some minor comments and the PR needs rebasing. Other than that, I think this is good! |
I'm a bit skeptical about this personally. I'm not sure if this should be an isVisible API for a field or some kind of "rules" definition passed to the DataForm. I think isVisible might not be flexible enough and it might also be not a generic enough API for field registration. That said, I'm ok iterating if you feel like this is a good first step. |
Yeah I like the "rule" definition approach, and was thinking we should add support for the rule parser when that is finalized that Nadir is working on. |
f013b70
to
4803094
Compare
Thanks for the review and feedback @oandregal, I updated the PR with your feedback. It should be ready for a re-review for when you have time :) |
Co-authored-by: louwie17 <louwie17@git.wordpress.org> Co-authored-by: oandregal <oandregal@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
What?
This adds the
isVisible
callback to the field type for determining if a field should be visible within DataForms.It also adds adependency
array option that gets used by theisVisible
callback. By default it only listens to the field value for changes.Why?
This solves the problem for dynamically showing/hiding fields within the form that depend specific field values. For example we want to hide the password field when the post status is set to
private
, but display it otherwise. Instead of filtering theform.fields
list at the top level, this allows us to handle this on a per field level.How?
It adds a
isVisible
callback that passes the data being edited. It should return a boolean.In addition, there is a dependency array, that works similar as the dependency array passed touseMemo
oruseEffect
calls. It takes a list of keys that theisVisible
function may depend on.In the case of the
password
field, it depends on thestatus
field. So it would takedependency: [ 'status' ]
and theisVisible
function will only be run ifstatus
changes.Testing Instructions
npm run storybook:dev
private
in the sample data form and notice how the password field disappearsTesting Instructions for Keyboard
Screenshots or screencast