Skip to content

Commit

Permalink
👀 check for visibility before adding live() for all components
Browse files Browse the repository at this point in the history
  • Loading branch information
atmonshi committed Jan 8, 2024
1 parent 461e589 commit 1006ef9
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 47 deletions.
33 changes: 17 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/Concerns/Designer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ trait Designer
public static function ui(Form $zeusForm, bool $inline = false): array
{
$sections = self::drawExt($zeusForm);
$hasSectionVisibility = $zeusForm->sections->pluck('options')->where('visibility.active', true)->isNotEmpty();

foreach ($zeusForm->sections->sortBy('ordering') as $section) {
$sections[] = self::drawSections(
$zeusForm,
$section,
self::drawFields($section, $inline)
self::drawFields($section, $inline, $hasSectionVisibility),
);
}

if (optional($zeusForm->options)['show-as'] === 'tabs') {
return [Tabs::make('tabs')->live()->tabs($sections)];
return [Tabs::make('tabs')->live(condition: $hasSectionVisibility)->tabs($sections)];
}

if (optional($zeusForm->options)['show-as'] === 'wizard') {
return [
Wizard::make($sections)
->live(),
->live(condition: $hasSectionVisibility),
//->skippable() // todo still not working
];
}
Expand Down Expand Up @@ -64,8 +65,10 @@ private static function drawExt(Form $zeusForm): array
];
}

private static function drawFields(ZeusSection $section, bool $inline): array
private static function drawFields(ZeusSection $section, bool $inline, bool $hasSectionVisibility = false): array
{
$hasVisibility = $hasSectionVisibility || $section->fields->pluck('options')->where('visibility.active', true)->isNotEmpty();

$fields = [];

if (! $inline) {
Expand All @@ -80,7 +83,7 @@ private static function drawFields(ZeusSection $section, bool $inline): array
$fieldClass = new $zeusField->type;
$component = $fieldClass->renderClass::make('zeusData.' . $zeusField->id);

$fields[] = $fieldClass->appendFilamentComponentsOptions($component, $zeusField);
$fields[] = $fieldClass->appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

if (! $inline) {
$fields[] = Bolt::renderHook('zeus-form-field.after');
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/HasHiddenOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait HasHiddenOptions
public static function hiddenVisibility(): array
{
return [
Hidden::make('options.visibility.active'),
Hidden::make('options.visibility.active')->default(false),
Hidden::make('options.visibility.fieldID'),
Hidden::make('options.visibility.values'),
];
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function getResponse(Field $field, FieldResponse $resp): string
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

$options = FieldsContract::getFieldCollectionItemsList($zeusField);

Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/ColorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public static function getOptionsHidden(): array
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

if (! empty($zeusField['options']['colorType'])) {
call_user_func([$component, $zeusField['options']['colorType']]);
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function getResponse(Field $field, FieldResponse $resp): string
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

$component->disk(config('zeus-bolt.uploadDisk'))
->directory(config('zeus-bolt.uploadDirectory'));
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function getResponse(Field $field, FieldResponse $resp): string
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

$options = FieldsContract::getFieldCollectionItemsList($zeusField);

Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function getResponse(Field $field, FieldResponse $resp): string
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

$options = FieldsContract::getFieldCollectionItemsList($zeusField);

Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ public static function getOptionsHidden(): array
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

if (! empty($zeusField['options']['dateType'])) {
call_user_func([$component, $zeusField['options']['dateType']]);
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static function getOptionsHidden(): array
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

if (filled($zeusField['options']['maxLength'])) {
$component->maxLength($zeusField['options']['maxLength']);
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Classes/Toggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public static function getOptionsHidden(): array
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
parent::appendFilamentComponentsOptions($component, $zeusField);
parent::appendFilamentComponentsOptions($component, $zeusField, $hasVisibility);

if (optional($zeusField->options)['on-icon']) {
$component = $component->onIcon($zeusField->options['on-icon']);
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getResponse(Field $field, FieldResponse $resp): string
}

// @phpstan-ignore-next-line
public function appendFilamentComponentsOptions($component, $zeusField)
public function appendFilamentComponentsOptions($component, $zeusField, bool $hasVisibility = false)
{
if (is_string($zeusField->options)) {
$zeusField->options = json_decode($zeusField->options, true);
Expand Down Expand Up @@ -118,7 +118,7 @@ public function appendFilamentComponentsOptions($component, $zeusField)
return $relatedFieldValues === $get('zeusData.' . $relatedField);
});

return $component->live(onBlur: true);
return $component->live(onBlur: $hasVisibility, condition: $hasVisibility);
}

public function getCollectionsValuesForResponse(Field $field, FieldResponse $resp): string
Expand Down
9 changes: 9 additions & 0 deletions stubs/ZeusField.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,14 @@ class {{ class }} extends FieldsContract
self::columnSpanFull(),
];
}

public static function getOptionsHidden(): array
{
return [
self::hiddenHtmlID(),
self::hiddenRequired(),
self::hiddenColumnSpanFull(),
];
}
}

7 changes: 0 additions & 7 deletions tests/ResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use LaraZeus\Bolt\Filament\Resources\CategoryResource;
use LaraZeus\Bolt\Filament\Resources\CollectionResource;
use LaraZeus\Bolt\Filament\Resources\FormResource;
use LaraZeus\Bolt\Models\Form;

use function Pest\Laravel\get;

Expand All @@ -27,9 +26,3 @@
get(FormResource::getUrl())
->assertSuccessful();
});

/*it('can render Response List', function () {
$form = Form::factory()->create();
get(ResponseResource::getUrl('index', ['form_id' => $form->id]))
->assertSuccessful();
});*/

0 comments on commit 1006ef9

Please sign in to comment.