Skip to content

Commit fa75521

Browse files
committed
Better handling of options for select/radio/checkbox groups
1 parent d7135bd commit fa75521

24 files changed

+349
-40
lines changed

src/Aire.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
// TODO: Aire::scaffold($user) -> generate update form
1515

1616
/**
17+
* @method static \Galahad\Aire\Elements\Form route(string $route_name, $parameters = [], bool $absolute = true)
1718
* @method static \Galahad\Aire\Elements\Label label(string $label)
1819
* @method static \Galahad\Aire\Elements\Button button(string $label = null)
1920
* @method static \Galahad\Aire\Elements\Button submit(string $label = 'Submit')
2021
* @method static \Galahad\Aire\Elements\Input input($name = null, $label = null)
21-
* @method static \Galahad\Aire\Elements\Select select(array $options, $name = null, $label = null)
22+
* @method static \Galahad\Aire\Elements\Select select(array|\Illuminate\Support\Collection|\Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\JsonSerializable|\Traversable $options, $name = null, $label = null)
2223
* @method static \Galahad\Aire\Elements\Textarea textArea($name = null, $label = null)
23-
* @method static \Galahad\Aire\Elements\Summary summary()
24+
* @method static \Galahad\Aire\Elements\Summary summary(bool $verbose = true)
2425
* @method static \Galahad\Aire\Elements\Checkbox checkbox($name = null, $label = null)
25-
* @method static \Galahad\Aire\Elements\CheckboxGroup checkboxGroup(array $options, $name, $label = null)
26-
* @method static \Galahad\Aire\Elements\RadioGroup radioGroup(array $options, $name, $label = null)
26+
* @method static \Galahad\Aire\Elements\CheckboxGroup checkboxGroup(array|\Illuminate\Support\Collection|\Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\JsonSerializable|\Traversable $options, $name, $label = null)
27+
* @method static \Galahad\Aire\Elements\RadioGroup radioGroup(array|\Illuminate\Support\Collection|\Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\JsonSerializable|\Traversable $options, $name, $label = null)
2728
* @method static \Galahad\Aire\Elements\Input hidden($name = null, $value = null)
2829
* @method static \Galahad\Aire\Elements\Input color($name = null, $label = null)
2930
* @method static \Galahad\Aire\Elements\Input date($name = null, $label = null)
@@ -167,6 +168,22 @@ public function open($action = null, $bound_data = null) : Form
167168
return $this->form;
168169
}
169170

171+
/**
172+
* Close a new Form.
173+
*
174+
* @return \Galahad\Aire\Elements\Form
175+
*/
176+
public function close() : Form
177+
{
178+
if (!($this->form instanceof Form)) {
179+
throw new BadMethodCallException('Trying to close a form before opening one.');
180+
}
181+
182+
$this->form->close();
183+
184+
return $this->form;
185+
}
186+
170187
/**
171188
* Get a configuration value
172189
*
@@ -211,6 +228,11 @@ public function render($view, array $data = [], array $merge_data = []) : string
211228
return $this->view_factory->make($this->applyTheme($view), $data, $merge_data)->render();
212229
}
213230

231+
/**
232+
* Get the next globally unique element ID
233+
*
234+
* @return int
235+
*/
214236
public function generateElementId() : int
215237
{
216238
return $this->next_element_id++;

src/Contracts/SelectableEntity.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Galahad\Aire\Contracts;
4+
5+
interface SelectableEntity
6+
{
7+
/**
8+
* Get the value that should be included if this entity is selected from a list
9+
*
10+
* @return mixed
11+
*/
12+
public function getSelectableId();
13+
14+
/**
15+
* Get the label to be shown in a list for selection
16+
*
17+
* @return string|\Illuminate\Contracts\Support\Htmlable
18+
*/
19+
public function getSelectableLabel();
20+
}

src/DTD/Select.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030

3131
namespace Galahad\Aire\DTD;
3232

33+
use Galahad\Aire\Aire;
3334
use Galahad\Aire\Elements\Element;
35+
use Galahad\Aire\Elements\Form;
36+
use Galahad\Aire\Support\OptionsCollection;
3437

3538
/**
3639
* Represents a control that provides a menu of options:
@@ -40,10 +43,13 @@ class Select extends Element
4043
{
4144
public $name = 'select';
4245

43-
protected $view_data = [
44-
'options' => [],
45-
];
46-
46+
public function __construct(Aire $aire, Form $form = null)
47+
{
48+
$this->view_data['options'] = new OptionsCollection();
49+
50+
parent::__construct($aire, $form);
51+
}
52+
4753
/**
4854
* Set the 'autofocus' flag
4955
*

src/Elements/CheckboxGroup.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CheckboxGroup extends \Galahad\Aire\DTD\Input
1717
'type' => 'checkbox',
1818
];
1919

20-
public function __construct(Aire $aire, array $options, Form $form = null)
20+
public function __construct(Aire $aire, $options, Form $form = null)
2121
{
2222
parent::__construct($aire, $form);
2323

src/Elements/Concerns/AutoId.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
trait AutoId
66
{
7-
protected function registerAutoId()
7+
protected function registerAutoId() : void
88
{
99
if (false === $this->aire->config('auto_id', true)) {
1010
return;

src/Elements/Concerns/CreatesElements.php

+91-10
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,23 @@
1616

1717
trait CreatesElements
1818
{
19+
/**
20+
* Create a <label> element
21+
*
22+
* @param string $label
23+
* @return \Galahad\Aire\Elements\Label
24+
*/
1925
public function label(string $label) : Label
2026
{
2127
return (new Label($this->aire))->text($label);
2228
}
2329

30+
/**
31+
* Create a <button> element
32+
*
33+
* @param string|null $label
34+
* @return \Galahad\Aire\Elements\Button
35+
*/
2436
public function button(string $label = null) : Button
2537
{
2638
$button = new Button($this->aire, $this);
@@ -32,27 +44,53 @@ public function button(string $label = null) : Button
3244
return $button;
3345
}
3446

47+
/**
48+
* Create a <button type="submit"> element
49+
*
50+
* @param string $label
51+
* @return \Galahad\Aire\Elements\Button
52+
*/
3553
public function submit(string $label = 'Submit') : Button
3654
{
3755
return $this->button($label)->type('submit');
3856
}
3957

40-
public function input($name = null, $label = null) : Input
58+
/**
59+
* Create an <input>
60+
*
61+
* @param string|null $name
62+
* @param string|\Illuminate\Contracts\Support\Htmlable|null $label
63+
* @param string|null $type
64+
* @return \Galahad\Aire\Elements\Input
65+
*/
66+
public function input($name = null, $label = null, $type = null) : Input
4167
{
4268
$input = new Input($this->aire, $this);
4369

4470
if ($name) {
45-
$input->name($name);
71+
$input->name((string) $name);
4672
}
4773

4874
if ($label) {
4975
$input->label($label);
5076
}
5177

78+
if ($type) {
79+
$input->type((string) $type);
80+
}
81+
5282
return $input;
5383
}
5484

55-
public function select(array $options, $name = null, $label = null) : Select
85+
/**
86+
* Create a <select> element
87+
*
88+
* @param array|\Illuminate\Support\Collection|\Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\JsonSerializable|\Traversable $options
89+
* @param string|null $name
90+
* @param string|\Illuminate\Contracts\Support\Htmlable|null $label
91+
* @return \Galahad\Aire\Elements\Select
92+
*/
93+
public function select($options, $name = null, $label = null) : Select
5694
{
5795
$select = new Select($this->aire, $options, $this);
5896

@@ -67,12 +105,19 @@ public function select(array $options, $name = null, $label = null) : Select
67105
return $select;
68106
}
69107

108+
/**
109+
* Create a <textarea> element
110+
*
111+
* @param string|null $name
112+
* @param string|\Illuminate\Contracts\Support\Htmlable|null $label
113+
* @return \Galahad\Aire\Elements\Textarea
114+
*/
70115
public function textArea($name = null, $label = null) : Textarea
71116
{
72117
$textarea = new Textarea($this->aire, $this);
73118

74119
if ($name) {
75-
$textarea->name($name);
120+
$textarea->name((string) $name);
76121
}
77122

78123
if ($label) {
@@ -82,12 +127,19 @@ public function textArea($name = null, $label = null) : Textarea
82127
return $textarea;
83128
}
84129

130+
/**
131+
* Create a <textarea> element meant for WYSIWYG use (using JavaScript)
132+
*
133+
* @param string|null $name
134+
* @param string|\Illuminate\Contracts\Support\Htmlable|null $label
135+
* @return \Galahad\Aire\Elements\Textarea
136+
*/
85137
public function wysiwyg($name = null, $label = null) : Textarea
86138
{
87139
$textarea = new Wysiwyg($this->aire, $this);
88140

89141
if ($name) {
90-
$textarea->name($name);
142+
$textarea->name((string) $name);
91143
}
92144

93145
if ($label) {
@@ -97,17 +149,30 @@ public function wysiwyg($name = null, $label = null) : Textarea
97149
return $textarea;
98150
}
99151

100-
public function summary() : Summary
152+
/**
153+
* Create a summary view, which will show if there are errors
154+
*
155+
* @param bool $verbose
156+
* @return \Galahad\Aire\Elements\Summary
157+
*/
158+
public function summary(bool $verbose = true) : Summary
101159
{
102-
return new Summary($this->aire);
160+
return (new Summary($this->aire, $this))->verbose($verbose);
103161
}
104162

163+
/**
164+
* Create a single <input type="checkbox"> element
165+
*
166+
* @param string|null $name
167+
* @param string|\Illuminate\Contracts\Support\Htmlable|null $label
168+
* @return \Galahad\Aire\Elements\Checkbox
169+
*/
105170
public function checkbox($name = null, $label = null) : Checkbox
106171
{
107172
$checkbox = new Checkbox($this->aire, $this);
108173

109174
if ($name) {
110-
$checkbox->name($name);
175+
$checkbox->name((string) $name);
111176
}
112177

113178
if ($label) {
@@ -117,7 +182,15 @@ public function checkbox($name = null, $label = null) : Checkbox
117182
return $checkbox;
118183
}
119184

120-
public function checkboxGroup(array $options, $name, $label = null) : CheckboxGroup
185+
/**
186+
* Create a group of <input type="checkbox"> elements
187+
*
188+
* @param array|\Illuminate\Support\Collection|\Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\JsonSerializable|\Traversable $options
189+
* @param string $name
190+
* @param string|\Illuminate\Contracts\Support\Htmlable|null $label
191+
* @return \Galahad\Aire\Elements\CheckboxGroup
192+
*/
193+
public function checkboxGroup($options, $name, $label = null) : CheckboxGroup
121194
{
122195
$checkbox_group = new CheckboxGroup($this->aire, $options, $this);
123196

@@ -130,7 +203,15 @@ public function checkboxGroup(array $options, $name, $label = null) : CheckboxGr
130203
return $checkbox_group;
131204
}
132205

133-
public function radioGroup(array $options, $name, $label = null) : RadioGroup
206+
/**
207+
* Create a group of <input type="radio"> elements
208+
*
209+
* @param array|\Illuminate\Support\Collection|\Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\JsonSerializable|\Traversable $options
210+
* @param string $name
211+
* @param string|\Illuminate\Contracts\Support\Htmlable|null $label
212+
* @return \Galahad\Aire\Elements\RadioGroup
213+
*/
214+
public function radioGroup($options, $name, $label = null) : RadioGroup
134215
{
135216
$radio_group = new RadioGroup($this->aire, $options, $this);
136217

src/Elements/Concerns/CreatesInputTypes.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66

77
trait CreatesInputTypes
88
{
9-
abstract public function input($name = null, $label = null) : Input;
10-
9+
abstract public function input($name = null, $label = null, $type = null) : Input;
10+
11+
/**
12+
* Create <input type="hidden"> element
13+
*
14+
* @param string|null $name
15+
* @param mixed $value
16+
* @return \Galahad\Aire\Elements\Input
17+
*/
1118
public function hidden($name = null, $value = null) : Input
1219
{
1320
$input = $this->input($name);

src/Elements/Concerns/Groupable.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use BadMethodCallException;
66
use Galahad\Aire\Elements\Group;
7+
use Illuminate\Contracts\Support\Htmlable;
78

89
/**
9-
* @method \Galahad\Aire\Elements\Group label(string $text)
10+
* @method \Galahad\Aire\Elements\Group label(string|Htmlable $text)
1011
* @method \Galahad\Aire\Elements\Group helpText(string $text)
1112
* @method \Galahad\Aire\Elements\Group validated($validation_state = 'valid')
1213
* @method \Galahad\Aire\Elements\Group valid()

src/Elements/Concerns/HasOptions.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
namespace Galahad\Aire\Elements\Concerns;
44

5+
use Galahad\Aire\Support\OptionsCollection;
6+
57
trait HasOptions
68
{
79
/**
810
* Set options from a key -> value associative array
911
*
10-
* @param array $options
12+
* @param array|\Illuminate\Support\Collection|\Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\JsonSerializable|\Traversable $options
1113
* @return $this
1214
*/
13-
public function setOptions(array $options) : self
15+
public function setOptions($options) : self
1416
{
15-
$this->view_data['options'] = $options;
17+
$this->view_data['options'] = new OptionsCollection($options);
1618

1719
return $this;
1820
}
@@ -25,7 +27,19 @@ public function setOptions(array $options) : self
2527
*/
2628
public function setOptionList(array $values) : self
2729
{
28-
$this->view_data['options'] = array_combine($values, $values);
30+
return $this->setOptions(array_combine($values, $values));
31+
}
32+
33+
/**
34+
* Push an option to the beginning of the list for "no value"
35+
*
36+
* @param string|\Illuminate\Contracts\Support\Htmlable $label
37+
* @param mixed $empty_value
38+
* @return \Galahad\Aire\Elements\Concerns\HasOptions
39+
*/
40+
public function prependEmptyOption($label, $empty_value = '') : self
41+
{
42+
$this->view_data['options']->prepend($label, $empty_value);
2943

3044
return $this;
3145
}

0 commit comments

Comments
 (0)