Skip to content

Commit

Permalink
Replace $dotName property with Str::dot helper
Browse files Browse the repository at this point in the history
  • Loading branch information
imacrayon committed Sep 10, 2020
1 parent 8eaf506 commit dcb4f21
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/editors/easy-mde.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
name="{{ $name }}"
id="{{ $id }}"
{{ $attributes }}
>{{ old($dotName, $slot) }}</textarea>
>{{ old(Str::dot($name), $slot) }}</textarea>
2 changes: 1 addition & 1 deletion resources/views/components/editors/trix.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div {{ $attributes }}>
<input name="{{ $name }}" id="{{ $id }}" value="{{ old($dotName, $slot) }}" type="hidden">
<input name="{{ $name }}" id="{{ $id }}" value="{{ old(Str::dot($name), $slot) }}" type="hidden">
<trix-editor input="{{ $id }}" class="{{ $styling }}"></trix-editor>
</div>
2 changes: 1 addition & 1 deletion resources/views/components/forms/inputs/textarea.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
id="{{ $id }}"
rows="{{ $rows }}"
{{ $attributes }}
>{{ old($dotName, $slot) }}</textarea>
>{{ old(Str::dot($name), $slot) }}</textarea>
8 changes: 8 additions & 0 deletions src/BladeUIKitServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function boot(): void
$this->bootBladeComponents();
$this->bootLivewireComponents();
$this->bootDirectives();
$this->bootHelpers();
$this->bootPublishing();
}

Expand Down Expand Up @@ -105,6 +106,13 @@ private function bootDirectives(): void
});
}

private function bootHelpers(): void
{
Str::macro('dot', function ($subject) {
return trim(str_replace(['[', ']', '..'], '.', $subject), '.');
});
}

private function bootPublishing(): void
{
if ($this->app->runningInConsole()) {
Expand Down
7 changes: 2 additions & 5 deletions src/Components/Editors/EasyMDE.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use BladeUIKit\Components\BladeComponent;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;

class EasyMDE extends BladeComponent
{
/** @var string */
public $name;

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

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

Expand All @@ -26,9 +24,8 @@ class EasyMDE extends BladeComponent
public function __construct(string $name, string $id = null, array $options = [])
{
$this->name = $name;
$this->id = $id ?? $name;
$this->id = $id ?? str_replace('.', '_', Str::dot($name));
$this->options = $options;
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
}

public function options(): array
Expand Down
7 changes: 2 additions & 5 deletions src/Components/Editors/Trix.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use BladeUIKit\Components\BladeComponent;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;

class Trix extends BladeComponent
{
/** @var string */
public $name;

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

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

Expand All @@ -26,9 +24,8 @@ class Trix extends BladeComponent
public function __construct(string $name, string $id = null, string $styling = 'trix-content')
{
$this->name = $name;
$this->id = $id ?? $name;
$this->id = $id ?? str_replace('.', '_', Str::dot($name));
$this->styling = $styling;
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
}

public function render(): View
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Forms/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BladeUIKit\Components\Forms;

use BladeUIKit\Components\BladeComponent;
use Illuminate\Support\Str;
use Illuminate\Support\ViewErrorBag;
use Illuminate\View\View;

Expand All @@ -18,7 +19,7 @@ class Error extends BladeComponent

public function __construct(string $field, string $bag = 'default')
{
$this->field = trim(str_replace(['[', ']', '..'], '.', $field), '.');
$this->field = Str::dot($field);
$this->bag = $bag;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Components/Forms/Inputs/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BladeUIKit\Components\Forms\Inputs;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;

class Checkbox extends Input
{
Expand All @@ -15,7 +16,7 @@ public function __construct(string $name, string $id = null, bool $checked = fal
{
parent::__construct($name, $id, 'checkbox');

$this->checked = (bool) old($this->dotName, $checked);
$this->checked = (bool) old(Str::dot($name), $checked);
}

public function render(): View
Expand Down
9 changes: 3 additions & 6 deletions src/Components/Forms/Inputs/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use BladeUIKit\Components\BladeComponent;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;

class Input extends BladeComponent
{
/** @var string */
public $name;

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

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

Expand All @@ -27,10 +25,9 @@ class Input extends BladeComponent
public function __construct(string $name, string $id = null, string $type = 'text', ?string $value = '')
{
$this->name = $name;
$this->id = $id ?? $name;
$this->id = $id ?? str_replace('.', '_', Str::dot($name));
$this->type = $type;
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
$this->value = old($this->dotName, $value ?? '');
$this->value = old(Str::dot($name), $value ?? '');
}

public function render(): View
Expand Down
7 changes: 2 additions & 5 deletions src/Components/Forms/Inputs/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use BladeUIKit\Components\BladeComponent;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;

class Textarea extends BladeComponent
{
/** @var string */
public $name;

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

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

Expand All @@ -24,9 +22,8 @@ class Textarea extends BladeComponent
public function __construct(string $name, string $id = null, $rows = 3)
{
$this->name = $name;
$this->id = $id ?? $name;
$this->id = $id ?? str_replace('.', '_', Str::dot($name));
$this->rows = $rows;
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
}

public function render(): View
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/Editors/EasyMDETest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function nested_editor_can_have_old_values()
$this->flashOld(['profile.about' => 'About me text']);

$expected = <<<HTML
<textarea x-data x-init="new EasyMDE({ element: \$el , ...{&quot;forceSync&quot;:true} })" name="profile[about]" id="profile[about]">About me text</textarea>
<textarea x-data x-init="new EasyMDE({ element: \$el , ...{&quot;forceSync&quot;:true} })" name="profile[about]" id="profile_about">About me text</textarea>
HTML;

$this->assertComponentRenders($expected, '<x-easy-mde name="profile[about]"/>');
Expand Down
4 changes: 2 additions & 2 deletions tests/Components/Editors/TrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function nested_editor_can_have_old_values()

$expected = <<<HTML
<div>
<input name="profile[about]" id="profile[about]" value="About me text" type="hidden">
<trix-editor input="profile[about]" class="trix-content">
<input name="profile[about]" id="profile_about" value="About me text" type="hidden">
<trix-editor input="profile_about" class="trix-content">
</trix-editor>
</div>
HTML;
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/Forms/Inputs/CheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function nested_inputs_can_have_old_values()
$this->flashOld(['login.remember_me' => true]);

$this->assertComponentRenders(
'<input name="login[remember_me]" type="checkbox" id="login[remember_me]" checked />',
'<input name="login[remember_me]" type="checkbox" id="login_remember_me" checked />',
'<x-checkbox name="login[remember_me]"/>'
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/Forms/Inputs/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function nested_inputs_can_have_old_values()
$this->flashOld(['profile.email' => 'Eloquent']);

$this->assertComponentRenders(
'<input name="profile[email]" type="email" id="profile[email]" value="Eloquent" />',
'<input name="profile[email]" type="email" id="profile_email" value="Eloquent" />',
'<x-email name="profile[email]"/>'
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/Forms/Inputs/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function nested_inputs_can_have_old_values()
$this->flashOld(['filters.search' => 'Eloquent']);

$this->assertComponentRenders(
'<input name="filters[search]" type="text" id="filters[search]" value="Eloquent" />',
'<input name="filters[search]" type="text" id="filters_search" value="Eloquent" />',
'<x-input name="filters[search]" />'
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/Forms/Inputs/PasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function nested_inputs_cannot_have_old_values()
$this->flashOld(['login.password' => 'secret']);

$this->assertComponentRenders(
'<input name="login[password]" type="password" id="login[password]" />',
'<input name="login[password]" type="password" id="login_password" />',
'<x-password name="login[password]"/>'
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/Forms/Inputs/PikadayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function nested_pikaday_can_have_old_values()
$this->flashOld(['profile.birthday' => '23/03/1989']);

$expected = <<<HTML
<input x-data x-init="new Pikaday({ field: \$el , ...{&quot;format&quot;:&quot;DD\/MM\/YYYY&quot;} })" name="profile[birthday]" type="text" id="profile[birthday]" placeholder="DD/MM/YYYY" value="23/03/1989" />
<input x-data x-init="new Pikaday({ field: \$el , ...{&quot;format&quot;:&quot;DD\/MM\/YYYY&quot;} })" name="profile[birthday]" type="text" id="profile_birthday" placeholder="DD/MM/YYYY" value="23/03/1989" />
HTML;

$this->assertComponentRenders($expected, '<x-pikaday name="profile[birthday]"/>');
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/Forms/Inputs/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function nested_inputs_can_have_old_values()
$this->flashOld(['profile.about' => 'About me text']);

$this->assertComponentRenders(
'<textarea name="profile[about]" id="profile[about]" rows="3">About me text</textarea>',
'<textarea name="profile[about]" id="profile_about" rows="3">About me text</textarea>',
'<x-textarea name="profile[about]"/>'
);
}
Expand Down

0 comments on commit dcb4f21

Please sign in to comment.