Skip to content

Commit

Permalink
Fix merging classes in field attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tanthammar committed Dec 15, 2020
1 parent 32af742 commit 6d04b26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/BaseField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tanthammar\TallForms;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Tanthammar\TallForms\Traits\HasAttributes;
use Tanthammar\TallForms\Traits\HasDesign;
use Tanthammar\TallForms\Traits\HasLabels;
Expand Down Expand Up @@ -37,7 +38,7 @@ class BaseField
public function __construct($label, $key)
{
$this->label = $label;
$this->name = $key ?? \Str::snake(\Str::lower($label));
$this->name = $key ?? Str::snake(Str::lower($label));
$this->key = 'form_data.' . $this->name;
$this->wire = config('tall-forms.field-attributes.wire');
$this->setAttr();
Expand Down
44 changes: 28 additions & 16 deletions src/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Tanthammar\TallForms\Traits;


use Illuminate\Support\Arr;

trait HasAttributes
{
public array $attributes = [];
Expand All @@ -22,54 +24,64 @@ public function setAttr()
data_set($this->attributes, 'label', 'tf-label');
}

public function rootAttr(array $attributes, bool $merge = true): self
protected function mergeClasses(string $key, array $custom)
{
$merged = array_merge_recursive($this->attributes[$key], $custom);
if(Arr::has($merged, 'class')) {
$merged['class'] = implode(" ", $merged['class']);
}
$this->attributes[$key] = $merged;
}

public function rootAttr(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['root'], $attributes) : $this->attributes['root'] = $attributes;
$mergeClass ? $this->mergeClasses('root', $attributes) : $this->attributes['root'] = $attributes;
return $this;
}

public function beforeAttr(array $attributes, bool $merge = true): self
public function beforeAttr(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['before'], $attributes) : $this->attributes['before'] = $attributes;
$mergeClass ? $this->mergeClasses('before', $attributes) : $this->attributes['before'] = $attributes;
return $this;
}

public function beforeText(array $attributes, bool $merge = true): self
public function beforeText(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['before-text'], $attributes) : $this->attributes['before-text'] = $attributes;
$mergeClass ? $this->mergeClasses('before-text', $attributes) : $this->attributes['before-text'] = $attributes;
return $this;
}

public function aboveAttr(array $attributes, bool $merge = true): self
public function aboveAttr(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['above'], $attributes) : $this->attributes['above'] = $attributes;
$mergeClass ? $this->mergeClasses('above', $attributes) : $this->attributes['above'] = $attributes;
return $this;
}

public function belowAttr(array $attributes, bool $merge = true): self
public function belowAttr(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['below'], $attributes) : $this->attributes['below'] = $attributes;
$mergeClass ? $this->mergeClasses('below', $attributes) : $this->attributes['below'] = $attributes;
return $this;
}

public function belowWrapperAttr(array $attributes, bool $merge = true): self
public function belowWrapperAttr(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['below-wrapper'], $attributes) : $this->attributes['below-wrapper'] = $attributes;
$mergeClass ? $this->mergeClasses('below-wrapper', $attributes) : $this->attributes['below-wrapper'] = $attributes;
return $this;
}

public function afterAttr(array $attributes, bool $merge = true): self
public function afterAttr(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['after'], $attributes) : $this->attributes['after'] = $attributes;
$mergeClass ? $this->mergeClasses('after', $attributes) : $this->attributes['after'] = $attributes;
return $this;
}

public function afterText(array $attributes, bool $merge = true): self
public function afterText(array $attributes, bool $mergeClass = true): self
{
$merge ? array_merge($this->attributes['after-text'], $attributes) : $this->attributes['after-text'] = $attributes;
$mergeClass ? $this->mergeClasses('after-text', $attributes) : $this->attributes['after-text'] = $attributes;
return $this;
}

//observe, not an array
public function labelClass(string $classes, bool $merge = true): self
{
$merge
Expand Down

0 comments on commit 6d04b26

Please sign in to comment.