Skip to content

Commit

Permalink
Support for radio and checkbox groups
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Jan 21, 2019
1 parent 76da5a2 commit ac63d7d
Show file tree
Hide file tree
Showing 18 changed files with 402 additions and 34 deletions.
137 changes: 130 additions & 7 deletions docs/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h1>
Basic Demo
</h1>


<pre><code class="language-php">{{ Aire::open() }}

{{ Aire::input()
Expand All @@ -37,17 +38,24 @@ <h1>
{{ Aire::select(['Option 1', 'Option 2'])
->label('Demo Select') }}

{{ Aire::textarea()->value('Demo text area') }}
{{ Aire::textarea()
->value('Demo text area') }}

{{ Aire::checkbox()
->label('Demo check box') }}

{{ Aire::checkbox()->label('Demo check box') }}
// Radio groups must have a name
{{ Aire::radioGroup('demo_radios', ['one' => 'Option 1', 'two' => 'Option 2'])
->defaultValue('two')
->label('Demo radio group') }}

{{ Aire::button('Demo Button') }}
{{ Aire::submit('Demo Submit Button') }}

{{ Aire::close() }}</code></pre>

<h2>Resulting Form</h2>

<form action="" method="POST" class="" data-aire-id="1">
<form action="" method="POST" class="" data-aire-id="1" enctype="multipart/form-data">



Expand Down Expand Up @@ -129,14 +137,129 @@ <h2>Resulting Form</h2>

<div class="">

<label >
<label class="flex items-center" >
<input
type="checkbox" value="1" data-validate="true"
class="pr-2"
/>
Demo check box
<span class="ml-2 flex-1">
Demo check box
</span>
</label>


</div>


</div>


<div data-validate="true" class="mb-6" data-aire-group>
<label
class="inline-block mb-2 ">

Demo radio group
</label>


<div class="">

<div value="two" data-validate="true">

<label class="flex items-baseline mb-2 ml-2 border-transparent border-l">
<input
type="radio" class="" name="foo" data-validate="true"
value="one"

/>
<span class="flex-1 ml-2">
Option 1
</span>
</label>


<label class="flex items-baseline mb-2 ml-2 border-transparent border-l">
<input
type="radio" class="" name="foo" data-validate="true"
value="two"
checked
/>
<span class="flex-1 ml-2">
Option 2
</span>
</label>

</div>


</div>


</div>


<div data-validate="true" class="mb-6" data-aire-group>
<label
class="inline-block mb-2 ">

Demo checkbox group
</label>


<div class="">

<div data-validate="true">


<label class="flex items-baseline mb-2 ml-2 border-transparent border-l">
<input
type="checkbox" class="" name="bar[]" data-validate="true"
value="one"

/>
<span class="flex-1 ml-2">
one
</span>
</label>


<label class="flex items-baseline mb-2 ml-2 border-transparent border-l">
<input
type="checkbox" class="" name="bar[]" data-validate="true"
value="two"
checked
/>
<span class="flex-1 ml-2">
two
</span>
</label>


<label class="flex items-baseline mb-2 ml-2 border-transparent border-l">
<input
type="checkbox" class="" name="bar[]" data-validate="true"
value="three"
checked
/>
<span class="flex-1 ml-2">
three
</span>
</label>


<label class="flex items-baseline mb-2 ml-2 border-transparent border-l">
<input
type="checkbox" class="" name="bar[]" data-validate="true"
value="four"

/>
<span class="flex-1 ml-2">
four
</span>
</label>

</div>


</div>

Expand All @@ -145,7 +268,7 @@ <h2>Resulting Form</h2>


<button
data-validate="true"
type="submit" data-validate="true"
class="inline-block font-normal text-center whitespace-no-wrap align-middle select-none border
rounded font-normal leading-normal text-white bg-blue-dark border-blue-darker hover:bg-blue-darker
hover:border-blue-darkest p-2 px-4"
Expand Down
11 changes: 9 additions & 2 deletions docs/src/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ public function map()
? '/'
: $view;

Route::get($path, function() use ($view, $path) {
Route::any($path, function() use ($view, $path) {
if (request()->isMethod('post')) {
session()->flashInput(request()->all());
}
return view($view, [
'current_path' => $path,
'readme' => new Readme(),
]);
});
})->middleware([
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);
}
}
}
5 changes: 5 additions & 0 deletions docs/src/views/api.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
<li><code>week</code></li>
</ul>

<p>
Aire also supports special <code>RadioGroup</code> and <code>CheckboxGroup</code> faus-elements
that abstract away managing the values of individual radio buttons or multi-value checkboxes.
</p>

<p>
Each element and input type can be instantiated directly from the
<code>Aire</code> facade or from the current <code>Form</code>
Expand Down
29 changes: 24 additions & 5 deletions docs/src/views/basic.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
Basic Demo
</h1>

@if(request()->request->count())
<?php dump(request()->all()); ?>
@endif

<pre><code class="language-php">@verbatim{{ Aire::open() }}

{{ Aire::input()
Expand All @@ -16,17 +20,24 @@
{{ Aire::select(['Option 1', 'Option 2'])
->label('Demo Select') }}

{{ Aire::textarea()->value('Demo text area') }}
{{ Aire::textarea()
->value('Demo text area') }}

{{ Aire::checkbox()
->label('Demo check box') }}

{{ Aire::checkbox()->label('Demo check box') }}
// Radio groups must have a name
{{ Aire::radioGroup('demo_radios', ['one' => 'Option 1', 'two' => 'Option 2'])
->defaultValue('two')
->label('Demo radio group') }}

{{ Aire::button('Demo Button') }}
{{ Aire::submit('Demo Submit Button') }}

{{ Aire::close() }}@endverbatim</code></pre>

<h2>Resulting Form</h2>

{{ Aire::open() }}
{{ Aire::open()->post()->multipart() }}

{{ Aire::input()
->label('Demo Input')
Expand All @@ -40,7 +51,15 @@

{{ Aire::checkbox()->label('Demo check box') }}

{{ Aire::button('Demo Button') }}
{{ Aire::radioGroup('foo', ['one' => 'Option 1', 'two' => 'Option 2'])
->defaultValue('two')
->label('Demo radio group') }}

{{ Aire::checkboxGroup('bar', ['one', 'two', 'three', 'four'])
->defaultValue(['two', 'three'])
->label('Demo checkbox group') }}

{{ Aire::submit('Demo Button') }}

{{ Aire::close() }}

Expand Down
48 changes: 44 additions & 4 deletions src/Elements/Attributes/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class Attributes implements Htmlable, ArrayAccess, Arrayable
Expand Down Expand Up @@ -69,7 +70,7 @@ public function registerMutator(string $attribute, callable $mutator) : self
*/
public function get($key, $default = null)
{
if ($this->offsetExists($key)) {
if (isset($this->defaults[$key]) || $this->offsetExists($key)) {
return $this->offsetGet($key);
}

Expand Down Expand Up @@ -183,6 +184,17 @@ public function setDefault(string $key, $value) : self
return $this;
}

public function isValue($check_value) : bool
{
$current_value = $this->get('value');

if (is_array($current_value)) {
return in_array($check_value, $current_value);
}

return $current_value === $check_value;
}

/**
* Exclude certain keys from being included when rendering to HTML
*
Expand All @@ -191,9 +203,27 @@ public function setDefault(string $key, $value) : self
*/
public function except(...$keys) : self
{
$this->except = array_merge($this->except, $keys);
$filtered_attributes = new static(Arr::except($this->items, $keys));
$filtered_attributes->except = $keys; // To exclude defaults
$filtered_attributes->defaults = $this->defaults;
$filtered_attributes->mutators = $this->mutators;

return $this;
return $filtered_attributes;
}

/**
* Only use certain keys when rendering to HTML
*
* @param mixed ...$keys
* @return \Galahad\Aire\Elements\Attributes\Attributes
*/
public function only(...$keys) : self
{
$filtered_attributes = new static(Arr::only($this->items, $keys));
$filtered_attributes->defaults = $this->defaults;
$filtered_attributes->mutators = $this->mutators;

return $filtered_attributes;
}

/**
Expand All @@ -206,7 +236,9 @@ public function toHtml() : string
return $this->toCollection()
->except($this->except)
->filter(function($value) {
return false !== $value && null !== $value;
return false !== $value
&& null !== $value
&& !is_array($value); // Array values have to be handled in associated component
})
->map(function($value, $name) {
if (true === $value) {
Expand Down Expand Up @@ -240,6 +272,14 @@ public function toArray() : array
{
$array = $this->items;

// Apply defaults
foreach (array_keys($this->defaults) as $key) {
if (!isset($array[$key])) {
$array[$key] = $this->offsetGet($key);
}
}

// Apply mutators
foreach (array_keys($this->mutators) as $key) {
$array[$key] = $this->offsetGet($key);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Elements/CheckboxGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Galahad\Aire\Elements;

use Galahad\Aire\Aire;
use Galahad\Aire\Elements\Concerns\HasValue;
use Illuminate\Support\Arr;

class CheckboxGroup extends \Galahad\Aire\DTD\Input
{
use HasValue;

public $name = 'checkbox-group';

protected $default_attributes = [
'type' => 'checkbox',
];

public function __construct(Aire $aire, array $options, Form $form = null)
{
parent::__construct($aire, $form);

$this->setOptions($options);
}

public function setOptions(array $options) : self
{
$this->view_data['options'] = Arr::isAssoc($options)
? $options
: array_combine($options, $options);

return $this;
}
}

0 comments on commit ac63d7d

Please sign in to comment.