Skip to content

Commit

Permalink
Merge pull request #2 from codions/develop
Browse files Browse the repository at this point in the history
Added fieldset component and minor improvements
  • Loading branch information
fabioassuncao authored May 29, 2023
2 parents 47f2ebe + 2091e80 commit e15d59c
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 16 deletions.
2 changes: 1 addition & 1 deletion resources/views/column.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{{ $field->render()->with($field->data()) }}
@else
<div class="{{ $field->getColClass() ?? 'col-md-6'}} mb-3">
{{ $field->prefix(rtrim($name, "."))->render()->with($field->data()) }}
{{ $field->render()->with($field->data()) }}
</div>
@endif
@endforeach
Expand Down
24 changes: 24 additions & 0 deletions resources/views/fieldset.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@php
if ( !empty($props['prefix']) ) {
$name = $props['prefix'];
} else {
$name = '';
}
@endphp

<fieldset class="{{ $row_class }}">
@isset($props['label'])
<legend>
{{ __($props['label']) }}
</legend>
@endisset
@foreach($props['fields'] as $field)
@if ( !method_exists($field, 'prefix') )
{{ $field->render()->with($field->data()) }}
@else
<div class="{{ $field->getColClass() ?? 'col-md-6'}} mb-3">
{{ $field->render()->with($field->data()) }}
</div>
@endif
@endforeach
</fieldset>
2 changes: 1 addition & 1 deletion resources/views/row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{{ $field->render()->with($field->data()) }}
@else
<div class="{{ $field->getColClass() ?? 'col-md-6'}} mb-3">
{{ $field->prefix(rtrim($name, "."))->render()->with($field->data()) }}
{{ $field->render()->with($field->data()) }}
</div>
@endif
@endforeach
Expand Down
65 changes: 65 additions & 0 deletions src/Components/Fieldset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Codions\LaravelLivewireForms\Components;

use Codions\LaravelLivewireForms\Traits\WithHelp;
use Codions\LaravelLivewireForms\Traits\WithPrefix;
use Illuminate\View\Component;

class Fieldset extends Component
{
use WithHelp;
use WithPrefix;

public $props = [];

public $attrs = [];

public $column_class = 'col-md-6 mb-2 mb-md-0';

public $row_class = '';

public static function make($label = null)
{
$component = new static;

$component->props = [
// 'name' => $name,
'label' => $label,
'fields' => [],
'help' => null,
];

$component->attrs = [
'disabled' => false,
];

return $component;
}

public function fields($fields = [])
{
$this->props['fields'] = $fields;

return $this;
}

public function isColumn($field)
{
return Column::class == get_class($field);
}

public function colSize($col = 'mb-2')
{
$this->column_class = "$col mb-2 mb-md-0";

return $this;
}

public function render()
{
$fieldset = $this;

return view('laravel-livewire-forms::fieldset', compact('fieldset'));
}
}
39 changes: 25 additions & 14 deletions src/Components/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Input extends Component
public static function make($name, $label = null)
{
$component = new static;

$component->props = [
'name' => $name,
'label' => $label,
Expand All @@ -41,7 +40,6 @@ public static function make($name, $label = null)
'help' => null,
'model' => '.defer',
];

$component->attrs = [
'type' => 'text',
'inputmode' => 'text',
Expand All @@ -56,18 +54,31 @@ public function type($type)
{
$this->attrs['type'] = $type;

if ($type == 'text') {
$this->attrs['inputmode'] = 'text';
} elseif ($type == 'number') {
$this->attrs['inputmode'] = 'numeric';
} elseif ($type == 'tel') {
$this->attrs['inputmode'] = 'tel';
} elseif ($type == 'search') {
$this->attrs['inputmode'] = 'search';
} elseif ($type == 'email') {
$this->attrs['inputmode'] = 'email';
} elseif ($type == 'url') {
$this->attrs['inputmode'] = 'url';
switch ($type) {
case 'text':
$this->attrs['inputmode'] = 'text';

break;
case 'number':
$this->attrs['inputmode'] = 'numeric';

break;
case 'tel':
$this->attrs['inputmode'] = 'tel';

break;
case 'search':
$this->attrs['inputmode'] = 'search';

break;
case 'email':
$this->attrs['inputmode'] = 'email';

break;
case 'url':
$this->attrs['inputmode'] = 'url';

break;
}

return $this;
Expand Down

0 comments on commit e15d59c

Please sign in to comment.