Skip to content

Commit

Permalink
Adding Dusk selector support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThompsonTLDR committed May 28, 2020
1 parent d13e13c commit ef84d62
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
'after-scripts' => 'after-scripts',

'after-styles' => 'after-styles',

'dusk' => env('app.env') !== 'production', // whether or not to display dusk selectors: https://laravel.com/docs/7.x/dusk#dusk-selectors
];
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ If using the `Form` class, the second parameter is optional.
{{ Form::bs('first_name', 'text', ['required']) }}
```

# Dusk

[Dusk selectors](https://laravel.com/docs/7.x/dusk#dusk-selectors) are enabled by default in any environment other than `production`. This can by changed in `config/form.php`

# Slots

Expand Down
19 changes: 14 additions & 5 deletions src/View/Components/Bs.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Bs extends Component
* @param array $options
* @return void
*/
public function __construct($name, $type = 'text', $options = [], $required = null, $label = null, $selected = null, $selectOptions = null, $checked = null, $placeholder = null, $formGroup = null, $groupClass = null, $labelClass = null)
public function __construct($name, $type = 'text', $options = [], $required = null, $label = null, $selected = null, $selectOptions = null, $checked = null, $placeholder = null, $formGroup = null, $groupClass = null, $labelClass = null, $dusk = null)
{
// convert from dot syntax to HTML syntax
$name = str_replace('.', '[', $name) . ((Str::of($name)->contains('.')) ? ']' : '');
Expand All @@ -235,15 +235,15 @@ public function __construct($name, $type = 'text', $options = [], $required = nu
// build the label
$this->label();

// build the id
$this->id();

$this->booleans();

$this->classes();

$this->livewireModel();

// build the id
$this->id();

// build the value
$this->value();

Expand Down Expand Up @@ -309,6 +309,11 @@ public function render($component = false)
$this->labelClass = implode(' ', $this->labelClasses);
$this->groupClass = implode(' ', $this->groupClasses);

// remove dusk selectors
if (!config('form.dusk')) {
unset($this->options['dusk']);
}

if ($component) {
return view('form::bs.' . $blade, get_object_vars($this));
}
Expand Down Expand Up @@ -371,7 +376,7 @@ private function classes()
*/
private function booleans()
{
foreach (['required', 'checked', 'selected', 'placeholder'] as $key) {
foreach (['required', 'checked', 'selected', 'placeholder', 'dusk'] as $key) {
// check if 'required' is a value, convert it to a key => true
if (in_array($key, $this->options, true)) {
$this->options[$key] = true;
Expand All @@ -388,6 +393,10 @@ private function booleans()
if ($this->options['placeholder'] === true) {
$this->options['placeholder'] = (($this->type === 'select') ? 'Select ' : '') . $this->label;
}

if ($this->options['dusk'] === true) {
$this->options['dusk'] = $this->options['id'] . '-' . $this->type;
}
}

/**
Expand Down

0 comments on commit ef84d62

Please sign in to comment.