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

refactor section and field options to a slide over #225

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/Concerns/HasHiddenOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace LaraZeus\Bolt\Concerns;

use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Get;
use Filament\Forms\Set;
use LaraZeus\Bolt\BoltPlugin;
use LaraZeus\Bolt\Facades\Bolt;

trait HasHiddenOptions
{
public static function hiddenVisibility(): array
{
return [
Hidden::make('options.visibility.active'),
Hidden::make('options.visibility.fieldID'),
Hidden::make('options.visibility.values'),
];
}

public static function hiddenRequired(): array
{
return [
Hidden::make('options.is_required')->default(false),
];
}

public static function hiddenHintOptions(): array
{
return [
Hidden::make('options.hint.text'),
Hidden::make('options.hint.icon'),
Hidden::make('options.hint.color'),
];
}

public static function hiddenColumnSpanFull(): array
{
return [
Hidden::make('options.column_span_full')->default(false),
];
}

public static function hiddenDataSource(): array
{
$dataSources = BoltPlugin::getModel('Collection')::get()
->mapWithKeys(function ($item, $key) {
return [
$key => [
'title' => $item['name'],
'class' => $item['id'],
],
];
})
->merge(
Bolt::availableDataSource()
->mapWithKeys(function ($item, $key) {
return [
$key => [
'title' => $item['title'],
'class' => $item['class'],
],
];
})
)
->pluck('title', 'class');

return Grid::make()
->schema([
Select::make('options.dataSource')
->required()
->createOptionForm([
TextInput::make('name')
->live(onBlur: true)
->label(__('Collections Name'))->required()->maxLength(255)->columnSpan(2),
Repeater::make('values')
->grid([
'default' => 1,
'md' => 2,
'lg' => 3,
])
->label(__('Collections Values'))
->columnSpan(2)
->columns(1)
->schema([
TextInput::make('itemValue')
->live(onBlur: true)
->afterStateUpdated(function (Set $set, Get $get, string $operation) {
$set('itemKey', $get('itemValue'));
})
->required()->label(__('Value'))->hint(__('what the user will see')),
TextInput::make('itemKey')
->live(onBlur: true)
->required()->label(__('Key'))->hint(__('what store in the form')),
Toggle::make('itemIsDefault')->label(__('selected by default')),
]),
])
->createOptionUsing(function (array $data) {
$collectionModel = BoltPlugin::getModel('Collection');
$collection = new $collectionModel;
$collection->fill($data);
$collection->save();

return $collection->id;
})
->options($dataSources)
->label(__('Data Source')),
])
->columns(1);
}

public static function hiddenHtmlID(): array
{
return [
Hidden::make('options.htmlId')->default(str()->random(6)),
];
}
}
43 changes: 16 additions & 27 deletions src/Concerns/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@

trait HasOptions
{
public static function visibility(string $type = 'field'): Grid
public static function visibility(string $type = 'field', ?array $getFields = null): Grid
{
$fieldsList = [];
if (filled($getFields)) {
$fieldsList = collect($getFields)
->pluck('fields')
->mapWithKeys(function (array $item) {
return $item;
});
}

return Grid::make()
->schema([
Toggle::make('options.visibility.active')
Expand All @@ -30,48 +39,28 @@ public static function visibility(string $type = 'field'): Grid
->live()
->visible(fn (Get $get): bool => ! empty($get('options.visibility.active')))
->required(fn (Get $get): bool => ! empty($get('options.visibility.active')))
->options(function ($livewire, $record) use ($type) {
if ($record === null) {
return [];
}

return $livewire->record
->fields()
->when($type === 'field', function ($query) use ($record) {
return $query->where($record->getTable() . '.id', '!=', $record->id);
})
->when($type === 'section', function ($query) use ($record) {
return $query->where('section_id', '!=', $record->id);
})
->where(function ($query) use ($record) {
$query->whereNotNull($record->getTable() . '.options->dataSource');
$query->orWhere('type', '\LaraZeus\Bolt\Fields\Classes\Toggle');
})
->get()
->pluck('name', 'id');
}),
->options(optional($fieldsList)->pluck('name', 'id')),

Select::make('options.visibility.values')
->label(__('has the value:'))
->live()
->required(fn (Get $get): bool => ! empty($get('options.visibility.fieldID')))
->visible(fn (Get $get): bool => ! empty($get('options.visibility.fieldID')))
->options(function (Get $get, $livewire, $record) {
->options(function (Get $get) use ($fieldsList) {
$getRelated = $fieldsList->where('id', $get('options.visibility.fieldID'))->first();

if ($get('options.visibility.fieldID') === null) {
return [];
}
$getRelated = $livewire->getRecord()->fields()
->where($record->getTable() . '.id', $get('options.visibility.fieldID'))
->first();

if ($getRelated->type === '\LaraZeus\Bolt\Fields\Classes\Toggle') {
if ($getRelated['type'] === '\LaraZeus\Bolt\Fields\Classes\Toggle') {
return [
'true' => __('checked'),
'false' => __('not checked'),
];
}

if (! isset($getRelated->options['dataSource'])) {
if (! isset($getRelated['options']['dataSource'])) {
return [];
}

Expand Down
Loading
Loading