-
Notifications
You must be signed in to change notification settings - Fork 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
feat: conditional tabs #8406
feat: conditional tabs #8406
Conversation
a90abfd
to
31463e4
Compare
Yeesssssssss Thank you! We are going to review this as soon as possible! |
packages/ui/src/forms/buildStateFromSchema/addFieldStatePromise.ts
Outdated
Show resolved
Hide resolved
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.
Wonderful PR, I pulled it down and it works great!
I just have one simplification idea that we should try and handle instead of relying on the config.
@@ -267,6 +268,11 @@ export const sanitizeFields = async ({ | |||
if (field.type === 'tabs') { | |||
for (let j = 0; j < field.tabs.length; j++) { | |||
const tab = field.tabs[j] | |||
|
|||
if ('admin' in tab && tab.admin?.condition && !field.id) { | |||
throw new MissingTabsId() |
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.
Shouldn't we just generate some kind of id
for them? I don't think you should have to make your own id
in your config to use this feature. What do you think?
Just to update you here, reviewing again and I'll make a commit against that ^ ID change, there's a way to do the form-state for a field even without a set name or ID, similar to collapsible fields |
Weird, I thought gh would let me push to your PR directly but I guess not #8720 Your commits are still saved in here though, you can cherry pick my commits into here, or we can merge that one and you should get co-authorship. Whichever way you prefer When adding in the field index-based field state, we found a regression where collapsible fields were not correctly mapping either, so my commits there will fix that as well |
Awesome @paulpopus. Sorry for the delay in response my side. @DanRibbens, I introduced the id property because I wasn't sure whether index-based field state would be suitable. My main concern being that if open states were to be stored either in the db via user preferences or in localstorage, changes to the Payload config could alter the desired state. However, if that's not a use case, then I'm delighted to see the API is simplified. I've checked out #8720 and it works nicely. Thanks guys! |
Alright, gonna close this one then in favour of the other one, thanks again! |
This is a POC for conditional tabs as per #1840.
Example
payload-conditional-tabs.mov
Learnings
I thought it'd be an easy win, as it seems quite a simple client-side feature. But from implementing this POC, I learned that:
passesCondition
prop, which is held in form state server sideadmin.condition
functions can't be serialised to client form componentsProposal
With the learnings in mind, my implementation:
id
property${id}.${i}
passesCondition
, rather than introducing more code uniquely for the tab fieldHere's how a tab field with conditional tabs would now error if an admin condition were passed to it:
But if an
id
is provided, then it's all gravy:Further Notes
As per my implementation:
fieldState
, similar to other 'non state affecting' fields such as theui
field.admin.condition
without giving the tabs field an idid
.TabField
client component keeps track of all of its tabs' visibility viauseFormFields
and is smart enough to shift the active index to a visible tab if the current tab is hidden.id
is unique to Payload's field config, however I would suggest that using eithername
orlabel
would be confusing DX. As a long-time Payload user,name
indicates to me that a field is tied to a collection's data andlabel
is a visible string displayed within the UI.