How to tell a field in rendered inside a drawer/modal form or not?

default discord avatar
Stupidism
10 months ago
1 1

I have a slug field auto tracking file name in my Media collection.
I used documentInfo to tell if I'm creating a new media or editing an existing one and disabled edit for the slug when editing. (Modifying slug is not allowed

    const documentInfo = useDocumentInfo();

    const isUpdateRef = useRef(!!documentInfo.id);

But now it's not working when I open the modal form inside an article.

I'd like to know is there any way to get the documentInfo of current form instead of current page?

  • default discord avatar
    Stupidism
    10 months ago

    For now, what I can find is to use the formRef to find the closest dialog element.

    
        const form = useForm();
        const documentInfo = useDocumentInfo();
    
        const isUpdateRef = useRef(
          !!documentInfo.id && !form.formRef.current.closest('dialog')
        );
    
    3 replies
  • discord user avatar
    DanRibbens
    Payload Team
    10 months ago

    Nice work figuring out a solution. I'd like to see Payload have a built-in solution for handling multiple forms from the provider. I'm adding the "enhancement" label so we can revisit this problem later.

  • discord user avatar
    jmikrut
    Payload Team
    10 months ago

    You can use useEditDepth to determine how deeply nested the form is 👍

    It's not "officially" exported yet but you can access it directly from dist in the meantime if you'd like. Also, @Stupidism we would be happy to accept a PR that adds useEditDepth to our /components/forms export!

  • default discord avatar
    Stupidism
    10 months ago

    @jmikrut I'm not sure if I should export it from forms or utilities. The current PR respects the code instead of your suggestion. Tell me if you want it changed. #1350

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.