From 6bc33724f8a9ef4e36d6a78b62b016faefc33622 Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Fri, 24 May 2024 13:27:32 +0800 Subject: [PATCH 1/5] fix value type when null --- packages/core/src/FieldTypes/Toggle.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/core/src/FieldTypes/Toggle.php b/packages/core/src/FieldTypes/Toggle.php index b20ea1aa61..a229b16071 100644 --- a/packages/core/src/FieldTypes/Toggle.php +++ b/packages/core/src/FieldTypes/Toggle.php @@ -9,7 +9,7 @@ class Toggle implements FieldType, JsonSerializable { /** - * @var string + * @var bool */ protected $value; @@ -24,11 +24,11 @@ public function jsonSerialize(): mixed } /** - * Create a new instance of Text field type. + * Create a new instance of Toggle field type. * * @param string $value */ - public function __construct($value = '') + public function __construct($value = false) { $this->setValue($value); } @@ -40,7 +40,7 @@ public function __construct($value = '') */ public function __toString() { - return $this->getValue() ?? ''; + return (int) ($this->getValue() ?? false); } /** @@ -72,11 +72,6 @@ public function setValue($value) */ public function getConfig(): array { - return [ - 'options' => [ - 'on_value' => 'nullable|string', - 'off_value' => 'nullable|string', - ], - ]; + return []; } } From c25913146625ef504f567cd51843540754e55e3a Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Fri, 24 May 2024 13:33:04 +0800 Subject: [PATCH 2/5] use setup to configure Attributes component --- .../Support/Forms/Components/Attributes.php | 119 +++++++++--------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/packages/admin/src/Support/Forms/Components/Attributes.php b/packages/admin/src/Support/Forms/Components/Attributes.php index 5bc09ad502..1ea605743d 100644 --- a/packages/admin/src/Support/Forms/Components/Attributes.php +++ b/packages/admin/src/Support/Forms/Components/Attributes.php @@ -2,7 +2,6 @@ namespace Lunar\Admin\Support\Forms\Components; -use Closure; use Filament\Forms; use Illuminate\Database\Eloquent\Model; use Livewire\Component as Livewire; @@ -15,85 +14,85 @@ class Attributes extends Forms\Components\Group { - public static function make(Closure|array $schema = []): static + protected function setUp(): void { - return app( - static::class, - [ - 'schema' => ! blank($schema) ? $schema : function (\Filament\Forms\Get $get, Livewire $livewire, ?Model $record) { - $modelClass = $livewire::getResource()::getModel(); + parent::setUp(); - $productTypeId = null; + $this->key('attributeData'); - $attributeQuery = Attribute::where('attribute_type', $modelClass); + if (blank($this->childComponents)) { + $this->schema(function (\Filament\Forms\Get $get, Livewire $livewire, ?Model $record) { + $modelClass = $livewire::getResource()::getModel(); - // Products are unique in that they use product types to map attributes, so we need - // to try and find the product type ID - if ($modelClass == Product::class) { - $productTypeId = $record?->product_type_id ?: ProductType::first()->id; + $productTypeId = null; - // If we have a product type, the attributes should be based off that. - if ($productTypeId) { - $attributeQuery = ProductType::find($productTypeId)->productAttributes(); - } - } + $attributeQuery = Attribute::where('attribute_type', $modelClass); - if ($modelClass == ProductVariant::class) { - $productTypeId = $record->product?->product_type_id ?: ProductType::first()->id; + // Products are unique in that they use product types to map attributes, so we need + // to try and find the product type ID + if ($modelClass == Product::class) { + $productTypeId = $record?->product_type_id ?: ProductType::first()->id; - // If we have a product type, the attributes should be based off that. - if ($productTypeId) { - $attributeQuery = ProductType::find($productTypeId)->variantAttributes(); - } + // If we have a product type, the attributes should be based off that. + if ($productTypeId) { + $attributeQuery = ProductType::find($productTypeId)->productAttributes(); } + } + + if ($modelClass == ProductVariant::class) { + $productTypeId = $record->product?->product_type_id ?: ProductType::first()->id; - $attributes = $attributeQuery->orderBy('position')->get(); + // If we have a product type, the attributes should be based off that. + if ($productTypeId) { + $attributeQuery = ProductType::find($productTypeId)->variantAttributes(); + } + } - $groups = AttributeGroup::where( - 'attributable_type', - $modelClass - )->orderBy('position', 'asc') - ->get() - ->map(function ($group) use ($attributes) { - return [ - 'model' => $group, - 'fields' => $attributes->groupBy('attribute_group_id')->get($group->id, []), - ]; - }); + $attributes = $attributeQuery->orderBy('position')->get(); - $groupComponents = []; + $groups = AttributeGroup::where( + 'attributable_type', + $modelClass + )->orderBy('position', 'asc') + ->get() + ->map(function ($group) use ($attributes) { + return [ + 'model' => $group, + 'fields' => $attributes->groupBy('attribute_group_id')->get($group->id, []), + ]; + }); - foreach ($groups as $group) { - $sectionFields = []; + $groupComponents = []; - foreach ($group['fields'] as $field) { - $sectionFields[] = AttributeData::getFilamentComponent($field); - } + foreach ($groups as $group) { + $sectionFields = []; - $groupComponents[] = Forms\Components\Section::make($group['model']->translate('name')) - ->schema($sectionFields); + foreach ($group['fields'] as $field) { + $sectionFields[] = AttributeData::getFilamentComponent($field); } - return $groupComponents; - }, - ] - ) - ->configure() - ->key('attributeData') - ->mutateStateForValidationUsing(function ($state) { - if (! is_array($state)) { - return $state; + $groupComponents[] = Forms\Components\Section::make($group['model']->translate('name')) + ->schema($sectionFields); } - foreach ($state as $key => $value) { - if (! $value instanceof \Lunar\Base\Fieldtype) { - continue; - } + return $groupComponents; + }); + } + + $this->mutateStateForValidationUsing(function ($state) { + if (! is_array($state)) { + return $state; + } - $state[$key] = $value->getValue(); + foreach ($state as $key => $value) { + if (! $value instanceof \Lunar\Base\Fieldtype) { + continue; } - return $state; - }); + $state[$key] = $value->getValue(); + } + + return $state; + }); } } From bc92a62459bb50c425ae355cc8675159a762fd8b Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Fri, 24 May 2024 13:34:24 +0800 Subject: [PATCH 3/5] update toggle field --- packages/admin/src/Support/FieldTypes/Toggle.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/admin/src/Support/FieldTypes/Toggle.php b/packages/admin/src/Support/FieldTypes/Toggle.php index 87316a407b..6b682b2cc4 100644 --- a/packages/admin/src/Support/FieldTypes/Toggle.php +++ b/packages/admin/src/Support/FieldTypes/Toggle.php @@ -13,10 +13,12 @@ class Toggle extends BaseFieldType public static function getFilamentComponent(Attribute $attribute): Component { - return ToggleInput::make($attribute->handle)->default('true') + return ToggleInput::make($attribute->handle) ->helperText( $attribute->translate('description') ) + ->default(false) + ->rule('boolean') ->live(); } } From 7b19a0e4c213c065f6520209074c4375d06bcfc0 Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Fri, 24 May 2024 15:29:15 +0800 Subject: [PATCH 4/5] fix attribute is null --- packages/admin/src/Support/Forms/AttributeData.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/admin/src/Support/Forms/AttributeData.php b/packages/admin/src/Support/Forms/AttributeData.php index 68f7aab574..231b420d87 100644 --- a/packages/admin/src/Support/Forms/AttributeData.php +++ b/packages/admin/src/Support/Forms/AttributeData.php @@ -44,10 +44,15 @@ public function getFilamentComponent(Attribute $attribute): Component $attribute->type ] ?? TextField::class; - return $fieldType::getFilamentComponent($attribute)->label( - $attribute->translate('name') - ) + /** @var Component $component */ + $component = $fieldType::getFilamentComponent($attribute); + + return $component + ->label( + $attribute->translate('name') + ) ->formatStateUsing(fn ($state) => ($state ?: new $attribute->type)) + ->mutateDehydratedStateUsing(fn ($state) => ($state ?: new $attribute->type)) ->required($attribute->required) ->default($attribute->default_value); } From 919575f51de07c27b23abb6e6564d191a960caa3 Mon Sep 17 00:00:00 2001 From: wychoong <67364036+wychoong@users.noreply.github.com> Date: Fri, 24 May 2024 15:29:33 +0800 Subject: [PATCH 5/5] remove `->live` on toggle field --- packages/admin/src/Support/FieldTypes/Toggle.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/admin/src/Support/FieldTypes/Toggle.php b/packages/admin/src/Support/FieldTypes/Toggle.php index 6b682b2cc4..3e3ec4a8d3 100644 --- a/packages/admin/src/Support/FieldTypes/Toggle.php +++ b/packages/admin/src/Support/FieldTypes/Toggle.php @@ -18,7 +18,6 @@ public static function getFilamentComponent(Attribute $attribute): Component $attribute->translate('description') ) ->default(false) - ->rule('boolean') - ->live(); + ->rule('boolean'); } }