Skip to content

Commit c3f131e

Browse files
authored
Merge pull request #4 from NicksonJoe/master
edit: base component
2 parents 66d1abe + c4eda65 commit c3f131e

9 files changed

+43
-45
lines changed

src/Components/Base/UikitBaseComponent.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44

55
abstract class UikitBaseComponent implements UikitBaseComponentInterface
66
{
7-
protected string $name;
87
protected ?string $id = null;
98
protected ?string $value = null;
109
protected ?string $label = null;
1110
protected ?string $class = null;
1211
protected string $attributes = '';
1312

13+
public function __construct(protected string $name)
14+
{
15+
if (empty($this->label)) {
16+
$this->label = ucfirst(str_replace('_', ' ', $this->name));
17+
}
18+
}
19+
1420
abstract public function render(): string;
1521

1622
public function __toString(): string
1723
{
1824
return $this->render();
1925
}
2026

21-
public function name(string $name): self
22-
{
23-
$this->name = $name;
24-
return $this;
25-
}
26-
2727
public function id(?string $id): self
2828
{
2929
$this->id = $id ?? $this->name;

src/Components/Base/UikitBaseComponentInterface.php

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
interface UikitBaseComponentInterface
66
{
7-
public function name(string $name): self;
8-
97
public function label(string $label): self;
108

119
public function id(?string $id): self;

src/Components/UikitCheckbox.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
class UikitCheckbox extends UikitBaseComponent
99
{
10-
protected bool $checked = false;
10+
protected ?bool $checked = false;
1111

12-
public static function create(): self
12+
public static function create(string $name): self
1313
{
14-
return new self();
14+
return new self($name);
1515
}
1616

17-
public function checked(bool $checked): self
17+
public function checked(?bool $checked): self
1818
{
19-
$this->checked = $checked;
19+
$this->checked = $checked ?? false;
2020
return $this;
2121
}
2222

src/Components/UikitInput.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
class UikitInput extends UikitBaseComponent
99
{
10-
public static function create(): self
10+
public static function create(string $name): self
1111
{
12-
return new self();
12+
return new self($name);
1313
}
1414

1515
/**
@@ -22,7 +22,7 @@ public function render(): string
2222
'label' => $this->label,
2323
'name' => $this->name,
2424
'value' => $this->value,
25-
'class' => $this->class,
25+
'class' => $this->class ?? 'uk-input',
2626
'attributes' => $this->attributes,
2727
])->render();
2828
}

src/Components/UikitRadio.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
class UikitRadio extends UikitBaseComponent
99
{
10-
protected bool $checked = false;
10+
protected ?bool $checked = false;
1111

12-
public static function create(): self
12+
public static function create(string $name): self
1313
{
14-
return new self();
14+
return new self($name);
1515
}
1616

17-
public function checked(bool $checked): self
17+
public function checked(?bool $checked): self
1818
{
19-
$this->checked = $checked;
19+
$this->checked = $checked ?? false;
2020
return $this;
2121
}
2222

src/Components/UikitSelect.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
namespace WDDA\LaravelUikitForm\Components;
44

55
use Throwable;
6+
use Illuminate\Support\Collection;
67
use WDDA\LaravelUikitForm\Components\Base\UikitBaseComponent;
78

89
class UikitSelect extends UikitBaseComponent
910
{
10-
protected array $options = [];
11+
protected Collection|array $options = [];
1112
protected array $attributesArray = [];
1213

13-
public static function create(): self
14+
public static function create(string $name): self
1415
{
15-
return new self();
16+
return new self($name);
1617
}
1718

18-
public function options(array $options): self
19+
public function options(Collection|array $options): self
1920
{
2021
$this->options = $options;
2122
return $this;

src/Components/UikitTextarea.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ class UikitTextarea extends UikitBaseComponent
99
{
1010
protected int $rows = 6;
1111

12-
public static function create(): self
12+
public static function create(string $name): self
1313
{
14-
return new self();
14+
return new self($name);
1515
}
1616

17-
public function rows($rows): self
17+
public function rows(int $rows): self
1818
{
1919
$this->rows = $rows;
2020
return $this;

src/helpers.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@
33
use WDDA\LaravelUikitForm\Components\{UikitCheckbox, UikitInput, UikitRadio, UikitSelect, UikitTextarea};
44

55
if (!function_exists('formRadio')) {
6-
function formRadio(): UikitRadio
6+
function formRadio(string $name): UikitRadio
77
{
8-
return UikitRadio::create();
8+
return UikitRadio::create($name);
99
}
1010
}
1111

1212
if (!function_exists('formInput')) {
13-
function formInput()
13+
function formInput(string $name)
1414
{
15-
return UikitInput::create();
15+
return UikitInput::create($name);
1616
}
1717
}
1818

1919
if (!function_exists('formTextarea')) {
20-
function formTextarea()
20+
function formTextarea(string $name)
2121
{
22-
return UikitTextarea::create();
22+
return UikitTextarea::create($name);
2323
}
2424
}
2525

2626
if (!function_exists('formSelect')) {
27-
function formSelect()
27+
function formSelect(string $name)
2828
{
29-
return UikitSelect::create();
29+
return UikitSelect::create($name);
3030
}
3131
}
3232

3333
if (!function_exists('formCheckbox')) {
34-
function formCheckbox()
34+
function formCheckbox(string $name)
3535
{
36-
return UikitCheckbox::create();
36+
return UikitCheckbox::create($name);
3737
}
3838
}

src/views/input.blade.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<div class="uk-margin-small-bottom">
2-
<label @if($id)for="{{ $id }}"@endif>{{ $label }}</label>
2+
<label @if($id) for="{{ $id }}" @endif>{{ $label }}</label>
33
<div class="uk-form-controls">
4-
<input @if(!$class)class="uk-input" @endif
5-
@if($id)id="{{ $id }}" @endif
6-
@if($name)name="{{ $name }}" @endif
7-
value="{{ $value }}"{!! $attributes !!}>
4+
<input class="{{ $class }}"
5+
@if($id) id="{{ $id }}" @endif
6+
@if($name) name="{{ $name }}" @endif
7+
value="{{ $value }}" {!! $attributes !!}>
88
</div>
99
</div>
10-

0 commit comments

Comments
 (0)