v1.0.0 initial release
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Eops\StarterKit\View\Components\Form;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Eops\StarterKit\View\Components\Form\Concerns\InteractsWithFormField;
|
||||
|
||||
class Checkbox extends Component
|
||||
{
|
||||
use InteractsWithFormField;
|
||||
|
||||
public bool $checked;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string|null $label
|
||||
* @param string|null $placeholder
|
||||
* @param string|null $help
|
||||
* @param bool $required
|
||||
* @param bool $disabled
|
||||
* @param bool $readonly Will act as disabled, but keep an HTML-sendable value
|
||||
* @param mixed|null $value
|
||||
* @param string $gridLayout
|
||||
* @param mixed|null $model
|
||||
* @param string|null $prefix
|
||||
* @param string|null $toggleLook Whether the checkbox is going to look like a toggle button
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public ?string $label = null,
|
||||
public ?string $placeholder = null,
|
||||
public ?string $help = null,
|
||||
public bool $required = false,
|
||||
public bool $disabled = false,
|
||||
public bool $readonly = false,
|
||||
public mixed $value = null,
|
||||
public string $gridLayout = '',
|
||||
public mixed $model = null,
|
||||
public ?string $prefix = null,
|
||||
public ?string $toggleLook = 'false',
|
||||
) {
|
||||
$toggleLook = filter_var($toggleLook, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
|
||||
$baseClass = $toggleLook ?
|
||||
'toggle toggle-primary' : 'checkbox';
|
||||
|
||||
$this->initFormField(
|
||||
name: $name,
|
||||
label: $label,
|
||||
placeholder: $placeholder,
|
||||
help: $help,
|
||||
required: $required,
|
||||
disabled: $disabled,
|
||||
readonly: $readonly,
|
||||
gridLayout: $gridLayout,
|
||||
value: $value,
|
||||
model: $model,
|
||||
prefix: $prefix,
|
||||
baseClass: $baseClass,
|
||||
);
|
||||
|
||||
$this->checked = $this->isChecked();
|
||||
$this->view = 'sk::components.form.checkbox';
|
||||
}
|
||||
|
||||
public function isChecked(): bool
|
||||
{
|
||||
return filter_var($this->resolvedValue, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Eops\StarterKit\View\Components\Form\Concerns;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\View\ComponentAttributeBag;
|
||||
use Illuminate\View\View;
|
||||
|
||||
trait InteractsWithFormField
|
||||
{
|
||||
public string $id;
|
||||
public ?string $resolvedValue;
|
||||
public string $inputClasses;
|
||||
public string $view;
|
||||
|
||||
public function initFormField(
|
||||
string $name,
|
||||
?string $label = null,
|
||||
?string $placeholder = null,
|
||||
?string $help = null,
|
||||
bool $required = false,
|
||||
bool $disabled = false,
|
||||
bool $readonly = false,
|
||||
string $gridLayout = '',
|
||||
mixed $value = null,
|
||||
mixed $model = null,
|
||||
?string $prefix = null,
|
||||
string $baseClass = '',
|
||||
): void {
|
||||
$this->id = Str::slug($prefix ? "{$prefix}-{$name}" : $name);
|
||||
|
||||
$modelValue = is_object($model) && isset($model->{$name}) ? $model->{$name} : null;
|
||||
$this->resolvedValue = old($name, $value ?? $modelValue);
|
||||
|
||||
$this->inputClasses = $baseClass;
|
||||
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
|
||||
if (session('errors')?->has($this->name)) {
|
||||
$this->inputClasses .= ' input-error';
|
||||
}
|
||||
if($this->readonly)
|
||||
{
|
||||
$this->inputClasses .= ' pointer-events-none opacity-50';
|
||||
}
|
||||
|
||||
return view($this->view);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Eops\StarterKit\View\Components\Form;
|
||||
|
||||
use Eops\StarterKit\View\Components\Form\Concerns\InteractsWithFormField;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
|
||||
class Input extends Component
|
||||
{
|
||||
use InteractsWithFormField;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $type
|
||||
* @param string|null $label
|
||||
* @param string|null $placeholder
|
||||
* @param string|null $help
|
||||
* @param bool $required
|
||||
* @param bool $disabled
|
||||
* @param bool $readonly Will act as disabled, but keep an HTML-sendable value
|
||||
* @param string $gridLayout The form section component offers a grid of 12 columns. Adjust this parameter as needed.
|
||||
* @param mixed|null $value
|
||||
* @param mixed|null $model If $model->$name exists, you can just set the model instead of a value.
|
||||
* @param string|null $prefix Used in loops to differentiate Ids
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $type = 'text',
|
||||
public ?string $label = null,
|
||||
public ?string $placeholder = null,
|
||||
public ?string $help = null,
|
||||
public bool $required = false,
|
||||
public bool $disabled = false,
|
||||
public bool $readonly = false,
|
||||
public string $gridLayout = '',
|
||||
public mixed $value = null,
|
||||
public mixed $model = null,
|
||||
public ?string $prefix = null,
|
||||
) {
|
||||
|
||||
$baseClass = $this->type === 'file' ? 'file-input file-input-bordered w-full' : 'input input-bordered w-full';
|
||||
|
||||
$this->initFormField(
|
||||
name: $name,
|
||||
label: $label,
|
||||
placeholder: $placeholder,
|
||||
help: $help,
|
||||
required: $required,
|
||||
disabled: $disabled,
|
||||
readonly: $readonly,
|
||||
gridLayout: $gridLayout,
|
||||
value: $value,
|
||||
model: $model,
|
||||
prefix: $prefix,
|
||||
baseClass: $baseClass,
|
||||
);
|
||||
|
||||
$this->view = 'sk::components.form.input';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Eops\StarterKit\View\Components\Form;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Eops\StarterKit\View\Components\Form\Concerns\InteractsWithFormField;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Radio extends Component
|
||||
{
|
||||
use InteractsWithFormField;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $options An associative array of $value => $label
|
||||
* @param string $type
|
||||
* @param string|null $label
|
||||
* @param string|null $help
|
||||
* @param string|null $title
|
||||
* @param bool $inline
|
||||
* @param bool $required
|
||||
* @param bool $disabled
|
||||
* @param bool $readonly Will act as disabled, but keep an HTML-sendable value
|
||||
* @param string $gridLayout
|
||||
* @param mixed|null $value
|
||||
* @param mixed|null $model
|
||||
* @param string|null $prefix
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public array $options = [],
|
||||
public string $type = 'text',
|
||||
public ?string $label = null,
|
||||
public ?string $help = null,
|
||||
public ?string $title = null,
|
||||
public bool $inline = false,
|
||||
public bool $required = false,
|
||||
public bool $disabled = false,
|
||||
public bool $readonly = false,
|
||||
public string $gridLayout = 'col-span-12 md:col-span-6',
|
||||
public mixed $value = null,
|
||||
public mixed $model = null,
|
||||
public ?string $prefix = null,
|
||||
) {
|
||||
|
||||
$this->initFormField(
|
||||
name: $name,
|
||||
label: $label,
|
||||
help: $help,
|
||||
required: $required,
|
||||
disabled: $disabled,
|
||||
readonly: $readonly,
|
||||
gridLayout: $gridLayout,
|
||||
value: $value,
|
||||
model: $model,
|
||||
prefix: $prefix,
|
||||
baseClass: 'input input-bordered w-full'
|
||||
);
|
||||
|
||||
$this->view = 'sk::components.form.radio';
|
||||
}
|
||||
|
||||
public function getOptionId(string $value): string
|
||||
{
|
||||
return Str::slug($this->name . '-' . $value);
|
||||
}
|
||||
|
||||
public function isSelected(mixed $option): bool
|
||||
{
|
||||
return (string) $this->resolvedValue === (string) $option;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Eops\StarterKit\View\Components\Form;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Eops\StarterKit\View\Components\Form\Concerns\InteractsWithFormField;
|
||||
|
||||
class Select extends Component
|
||||
{
|
||||
use InteractsWithFormField;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $options An associative array of $value => $label
|
||||
* @param string|null $label
|
||||
* @param string|null $placeholder
|
||||
* @param string|null $help
|
||||
* @param bool $required
|
||||
* @param bool $disabled
|
||||
* @param bool $readonly Will act as disabled, but keep an HTML-sendable value
|
||||
* @param string $gridLayout
|
||||
* @param mixed|null $value
|
||||
* @param mixed|null $model
|
||||
* @param string|null $prefix
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public array $options = [],
|
||||
public ?string $label = null,
|
||||
public ?string $placeholder = null,
|
||||
public ?string $help = null,
|
||||
public bool $required = false,
|
||||
public bool $disabled = false,
|
||||
public bool $readonly = false,
|
||||
public string $gridLayout = '',
|
||||
public mixed $value = null,
|
||||
public mixed $model = null,
|
||||
public ?string $prefix = null,
|
||||
) {
|
||||
$this->initFormField(
|
||||
name: $name,
|
||||
label: $label,
|
||||
placeholder: $placeholder,
|
||||
help: $help,
|
||||
required: $required,
|
||||
disabled: $disabled,
|
||||
readonly: $readonly,
|
||||
gridLayout: $gridLayout,
|
||||
value: $value,
|
||||
model: $model,
|
||||
prefix: $prefix,
|
||||
baseClass: 'select select-bordered w-full'
|
||||
);
|
||||
|
||||
$this->view = 'sk::components.form.select';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Eops\StarterKit\View\Components\Form;
|
||||
|
||||
use Eops\StarterKit\View\Components\Form\Concerns\InteractsWithFormField;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
class Textarea extends Component
|
||||
{
|
||||
use InteractsWithFormField;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string|null $label
|
||||
* @param string|null $placeholder
|
||||
* @param string|null $help
|
||||
* @param bool $required
|
||||
* @param bool $disabled
|
||||
* @param bool $readonly Will act as disabled, but keep an HTML-sendable value
|
||||
* @param string $gridLayout
|
||||
* @param mixed|null $value
|
||||
* @param mixed|null $model
|
||||
* @param string|null $prefix
|
||||
* @param int $rows
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public ?string $label = null,
|
||||
public ?string $placeholder = null,
|
||||
public ?string $help = null,
|
||||
public bool $required = false,
|
||||
public bool $disabled = false,
|
||||
public bool $readonly = false,
|
||||
public string $gridLayout = '',
|
||||
public mixed $value = null,
|
||||
public mixed $model = null,
|
||||
public ?string $prefix = null,
|
||||
public int $rows = 4,
|
||||
) {
|
||||
|
||||
$this->initFormField(
|
||||
name: $name,
|
||||
label: $label,
|
||||
placeholder: $placeholder,
|
||||
help: $help,
|
||||
required: $required,
|
||||
disabled: $disabled,
|
||||
readonly: $readonly,
|
||||
gridLayout: $gridLayout,
|
||||
value: $value,
|
||||
model: $model,
|
||||
prefix: $prefix,
|
||||
baseClass: 'textarea textarea-bordered w-full'
|
||||
);
|
||||
|
||||
$this->view = 'sk::components.form.textarea';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user