Skip to content
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

Is there a way to hide the add button on certian conditions? #481

Open
majdghithan opened this issue Oct 3, 2023 · 1 comment
Open

Is there a way to hide the add button on certian conditions? #481

majdghithan opened this issue Oct 3, 2023 · 1 comment

Comments

@majdghithan
Copy link

No description provided.

@wize-wiz
Copy link

wize-wiz commented Apr 22, 2024

It depends if the button needs to be set in a static or variable condition.

You can start by adding a computed field to FormField.vue:

computed: {
  disableButton() {
      return this.currentField.disableButton || false;
  },
  // ...
}

Add a v-if to the last <component> in FormField.vue at the bottom:

<component
    v-if="!disableButton"
    :layouts="layouts"
    :is="currentField.menu.component"
    // ...
    @addGroup="addGroup($event)"
/>

And last, add meta to the field by adding a method to the Whitecube\NovaFlexibleContent\Flexible's to fill disableButton:


namespace Whitecube\NovaFlexibleContent;

// ..

class Flexible extends Field
{
    use SupportsDependentFields;

    /**
     * Disable button
     *
     * @return mixed
     */
    public function disableButton($disable) {
        $disabled = false;
        if(is_bool($disable)) {
            $disabled = $disable;
        }
        if(is_callable($disable)) {
            $disabled = $disable();
        }

        $this->withMeta(['disableButton' => $disabled]);
        return $this;
    }
}

Use this method like all other public methods to control the field.

Flexible::make('Flexible')
   ->addLayout(SomeLayout::class)
   ->resolver(SomeResolver::class)
   ->disableButton(true);

Or use a function

   ->disableButton(function() use ($whatever) {
      return $whatever === 'something-to-disable' ? 
          true : false;
   });

Not tested, should work though.

If it needs to be rendered from javascript's point of view, I'm not going to give you a quick example out of the top my head because that is not as simple as the above, but possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants