Skip to content

Commit

Permalink
Many things
Browse files Browse the repository at this point in the history
- Removed CRSF token on form with GET method
- Removed textarea rows attribute
- Enhanced strict type declarations
  • Loading branch information
forxer committed Aug 19, 2023
1 parent 42008a2 commit c6f121a
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 154 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CHANGELOG
=========

0.8.3 (2023-08-19)
------------------

- Removed CRSF token on form with GET method
- Removed textarea rows attribute
- Enhanced strict type declarations


0.8.2 (2023-08-14)
------------------

Expand Down
45 changes: 30 additions & 15 deletions config/blade-ui-kit-bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

use BladeUIKitBootstrap\Components\Forms\Inputs\Date;
use BladeUIKitBootstrap\Components\Forms\Inputs\Email;
use BladeUIKitBootstrap\Components\Forms\Error;
use BladeUIKitBootstrap\Components\Forms\Form;
use BladeUIKitBootstrap\Components\Buttons\FormButton;
use BladeUIKitBootstrap\Components\Forms\Inputs\Hidden;
use BladeUIKitBootstrap\Components\Forms\Inputs\Input;
use BladeUIKitBootstrap\Components\Forms\Label;
use BladeUIKitBootstrap\Components\Buttons\Logout;
use BladeUIKitBootstrap\Components\Modals\Modal;
use BladeUIKitBootstrap\Components\Modals\Confirm;
use BladeUIKitBootstrap\Components\Forms\Inputs\Password;
use BladeUIKitBootstrap\Components\Forms\Inputs\Select;
use BladeUIKitBootstrap\Components\Forms\Inputs\Textarea;
use BladeUIKitBootstrap\Components\Forms\Inputs\Time;
use BladeUIKitBootstrap\Components;

return [
Expand All @@ -16,21 +31,21 @@
*/

'components' => [
'date' => Components\Forms\Inputs\Date::class,
'email' => Components\Forms\Inputs\Email::class,
'error' => Components\Forms\Error::class,
'form' => Components\Forms\Form::class,
'form-button' => Components\Buttons\FormButton::class,
'hidden' => Components\Forms\Inputs\Hidden::class,
'input' => Components\Forms\Inputs\Input::class,
'label' => Components\Forms\Label::class,
'logout' => Components\Buttons\Logout::class,
'modal' => Components\Modals\Modal::class,
'modal-confirm' => Components\Modals\Confirm::class,
'password' => Components\Forms\Inputs\Password::class,
'select' => Components\Forms\Inputs\Select::class,
'textarea' => Components\Forms\Inputs\Textarea::class,
'time' => Components\Forms\Inputs\Time::class,
'date' => Date::class,
'email' => Email::class,
'error' => Error::class,
'form' => Form::class,
'form-button' => FormButton::class,
'hidden' => Hidden::class,
'input' => Input::class,
'label' => Label::class,
'logout' => Logout::class,
'modal' => Modal::class,
'modal-confirm' => Confirm::class,
'password' => Password::class,
'select' => Select::class,
'textarea' => Textarea::class,
'time' => Time::class,
],

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<textarea
name="{{ $name }}"
id="{{ $id }}"
rows="{{ $rows }}"
@if($hasErrors)aria-describedby="validation-{{ $name }}-feedback"@endif
{{ $attributes->class([
'form-control',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<textarea
name="{{ $name }}"
id="{{ $id }}"
rows="{{ $rows }}"
@if($hasErrors)aria-describedby="validation-{{ $name }}-feedback"@endif
{{ $attributes->class([
'form-control',
Expand Down
24 changes: 11 additions & 13 deletions src/Components/Buttons/FormButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
namespace BladeUIKitBootstrap\Components\Buttons;

use BladeUIKitBootstrap\Components\BladeComponent;
use BladeUIKitBootstrap\Concerns\HasFormMethod;
use Illuminate\Support\Str;

class FormButton extends BladeComponent
{
/** @var string */
public $formId;

/** @var string|null */
public $action;

/** @var string */
public $method;

public function __construct(string $formId = null, string $action = null, string $method = 'POST')
{
use HasFormMethod;

public function __construct(
public ?string $formId = null,
public ?string $action = null,
string $method = 'POST',
public bool $hasFiles = false,
public bool $novalidate = true
){
$this->formId = 'form-button-'.($formId ?? Str::random(32));
$this->action = $action;
$this->method = strtoupper($method);
$this->method = $this->validFormMethod($method);
}

public function viewName(): string
Expand Down
6 changes: 4 additions & 2 deletions src/Components/Buttons/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

class Logout extends FormButton
{
public function __construct(string $formId = null, string $action = null)
{
public function __construct(
?string $formId = null,
?string $action = null
) {
$this->action = $action ?? route('logout');

parent::__construct($formId, $action, 'POST');
Expand Down
12 changes: 4 additions & 8 deletions src/Components/Forms/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ class Error extends BladeComponent
{
use CanHaveErrors;

/** @var string */
public $field;

/** @var string */
public $bag;

public function __construct(string $field, string $bag = 'default')
{
public function __construct(
public string $field,
public string $bag = 'default'
) {
$this->bootCanHaveErrors($field, $bag);
}

Expand Down
28 changes: 10 additions & 18 deletions src/Components/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@
namespace BladeUIKitBootstrap\Components\Forms;

use BladeUIKitBootstrap\Components\BladeComponent;
use BladeUIKitBootstrap\Concerns\HasFormMethod;

class Form extends BladeComponent
{
/** @var string|null */
public $action;

/** @var string */
public $method;

/** @var bool */
public $hasFiles;

/** @var bool */
public bool $novalidate;

public function __construct(string $action = null, string $method = 'POST', bool $hasFiles = false, bool $novalidate = true)
{
$this->action = $action;
$this->method = strtoupper($method);
$this->hasFiles = $hasFiles;
$this->novalidate = $novalidate;
use HasFormMethod;

public function __construct(
public ?string $action = null,
string $method = 'POST',
public bool $hasFiles = false,
public bool $novalidate = true
) {
$this->method = $this->validFormMethod($method);
}

public function viewName(): string
Expand Down
10 changes: 7 additions & 3 deletions src/Components/Forms/Inputs/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

class Date extends Input
{
public function __construct(string $name, string $id = null, ?string $value = '', ?string $errorBag = null)
{
parent::__construct($name, $id, 'date', $value, $errorBag);
public function __construct(
string $name,
?string $value = null,
?string $id = null,
?string $errorBag = null
) {
parent::__construct($name, 'date', $value, $id, $errorBag);
}

public function viewName(): string
Expand Down
10 changes: 7 additions & 3 deletions src/Components/Forms/Inputs/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

class Email extends Input
{
public function __construct(string $name, string $id = null, ?string $value = '', ?string $errorBag = null)
{
parent::__construct($name, $id, 'email', $value, $errorBag);
public function __construct(
string $name,
?string $value = null,
?string $id = null,
?string $errorBag = null
) {
parent::__construct($name, 'email', $value, $id, $errorBag);
}

public function viewName(): string
Expand Down
9 changes: 6 additions & 3 deletions src/Components/Forms/Inputs/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

class Hidden extends Input
{
public function __construct(string $name, string $id = null, ?string $value = '')
{
parent::__construct($name, $id, 'hidden', $value);
public function __construct(
string $name,
?string $value = null,
?string $id = null,
) {
parent::__construct($name, 'hidden', $value, $id);
}

public function viewName(): string
Expand Down
25 changes: 10 additions & 15 deletions src/Components/Forms/Inputs/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,19 @@ class Input extends BladeComponent
{
use CanHaveErrors;

/** @var string */
public $name;
public string $id;

/** @var string */
public $id;
public string $value;

/** @var string */
public $type;

/** @var string */
public $value;

public function __construct(string $name, string $id = null, string $type = 'text', ?string $value = '', ?string $errorBag = null)
{
$this->name = $name;
$this->id = $id ?? $name;
$this->type = $type;
public function __construct(
public string $name,
public string $type = 'text',
?string $value = null,
?string $id = null,
?string $errorBag = null
) {
$this->value = old($name, $value ?? '');
$this->id = $id ?? $name;

$this->bootCanHaveErrors($name, $errorBag);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Components/Forms/Inputs/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

class Password extends Input
{
public function __construct(string $name = 'password', string $id = null, ?string $errorBag = null)
{
parent::__construct($name, $id, 'password', null, $errorBag);
public function __construct(
string $name,
?string $id = null,
?string $errorBag = null
) {
parent::__construct($name, 'password', null, $id, $errorBag);
}

public function viewName(): string
Expand Down
50 changes: 17 additions & 33 deletions src/Components/Forms/Inputs/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,37 @@ class Select extends BladeComponent
{
use CanHaveErrors;

/** @var string */
public $name;

/** @var string|null */
public $id;

/** @var array|Collection */
public $options;

/** @var string */
public $labelAttribute;

/** @var string */
public $valueAttribute;

/** @var string|array|null */
public $selected;

/** @var string */
public $placeholder;

public function __construct(string $name, $options, $selected = null, string $placeholder = '', string $labelAttribute = 'name', string $valueAttribute = 'id', $id = null, ?string $errorBag = null)
{
$this->name = $name;
public string $id;

public function __construct(
public string $name,
public array|Collection $options,
public string|array|null $selected = null,
public ?string $placeholder = null,
string $labelAttribute = 'name',
string $valueAttribute = 'id',
?string $id = null,
?string $errorBag = null
) {
$this->id = $id ?? $name;
$this->labelAttribute = $labelAttribute;
$this->valueAttribute = $valueAttribute;
$this->selected = $selected;
$this->placeholder = $placeholder;

if ($options instanceof Collection) {
$this->options = $options->pluck($this->labelAttribute, $this->valueAttribute);
} elseif (is_array($options)) {
if (is_array($options)) {
$this->options = $options;
} elseif ($options instanceof Collection) {
$this->options = $options->pluck($labelAttribute, $valueAttribute);
} else {
throw new InvalidArgumentException('Invalid options');
}

$this->bootCanHaveErrors($name, $errorBag);
}

public function isSelected($value): bool
public function isSelected(string|array|null $value): bool
{
$selected = old($this->name, $this->selected);

if (is_array($selected)) {
return in_array($value, $selected);
return in_array($value, $selected, true);
}

return $value === $selected;
Expand Down
18 changes: 6 additions & 12 deletions src/Components/Forms/Inputs/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ class Textarea extends BladeComponent
{
use CanHaveErrors;

/** @var string */
public $name;
public string $id;

/** @var string */
public $id;

/** @var int */
public $rows;

public function __construct(string $name, string $id = null, $rows = 3, ?string $errorBag = null)
{
$this->name = $name;
public function __construct(
public string $name,
?string $id = null,
?string $errorBag = null
) {
$this->id = $id ?? $name;
$this->rows = $rows;

$this->bootCanHaveErrors($name, $errorBag);
}
Expand Down
Loading

0 comments on commit c6f121a

Please sign in to comment.