whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
778 stars 230 forks source link

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

Open majdghithan opened 9 months ago

wize-wiz commented 2 months ago

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.