Skip to content
This repository was archived by the owner on Apr 24, 2020. It is now read-only.

Commit 88e3a25

Browse files
committed
Build HTML using spatie laravel html instead of laravel collective
1 parent c3022e7 commit 88e3a25

9 files changed

+300
-82
lines changed

app/Http/Html/Checkbox.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Http\Html;
4+
5+
use Spatie\Html\Elements\Input;
6+
use Spatie\Html\Elements\Label;
7+
8+
class Checkbox extends Input
9+
{
10+
/**
11+
* The toggle label.
12+
*
13+
* @var Label|null
14+
*/
15+
protected $label;
16+
17+
/**
18+
* The label for the toggle.
19+
*
20+
* @param string $label
21+
*
22+
* @return static
23+
*/
24+
public function label($label)
25+
{
26+
$this->label = form()->label()
27+
->for($this->getAttribute('name'))
28+
->class('custom-control-label')
29+
->text($label);
30+
31+
return $this;
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function toHtml(): string
38+
{
39+
$label = $this->label ? $this->label->toHtml() : null;
40+
41+
return $this->wrap(parent::toHtml().$label);
42+
}
43+
44+
/**
45+
* Wrap the content in a div.
46+
*
47+
* @param string $content
48+
*
49+
* @return \Spatie\Html\Elements\Div
50+
*/
51+
public function wrap($content)
52+
{
53+
return html()->div($content)->class(['custom-control', 'custom-checkbox']);
54+
}
55+
}

app/Http/Html/FormFactory.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace App\Http\Html;
4+
5+
use Spatie\Html\Elements\Span;
6+
use Spatie\Html\Elements\Input;
7+
use Spatie\Html\Elements\Label;
8+
use Spatie\Html\Elements\Select;
9+
10+
class FormFactory
11+
{
12+
/**
13+
* Create a new input element.
14+
*
15+
* @return Input
16+
*/
17+
public function input()
18+
{
19+
return Input::create()->class('form-control');
20+
}
21+
22+
/**
23+
* Create a new password element.
24+
*
25+
* @return Input
26+
*/
27+
public function password()
28+
{
29+
return $this->input()->type('password');
30+
}
31+
32+
/**
33+
* Create a new number element.
34+
*
35+
* @return Input
36+
*/
37+
public function number()
38+
{
39+
return $this->input()->type('number');
40+
}
41+
42+
/**
43+
* Create a new search element.
44+
*
45+
* @return Input
46+
*/
47+
public function search()
48+
{
49+
return $this->input()->type('search');
50+
}
51+
52+
/**
53+
* Create a new email element.
54+
*
55+
* @return Input
56+
*/
57+
public function email()
58+
{
59+
return $this->input()->type('email');
60+
}
61+
62+
/**
63+
* Create a new trix editor.
64+
*
65+
* @return TrixEditor
66+
*/
67+
public function editor()
68+
{
69+
return TrixEditor::create();
70+
}
71+
72+
/**
73+
* Create a new select element.
74+
*
75+
* @return Select
76+
*/
77+
public function select()
78+
{
79+
return Select::create()->class('custom-select');
80+
}
81+
82+
/**
83+
* Create a new checkbox element.
84+
*
85+
* @return Checkbox
86+
*/
87+
public function checkbox()
88+
{
89+
return Checkbox::create()
90+
->type('checkbox')
91+
->class('custom-control-input');
92+
}
93+
94+
/**
95+
* Create a new radio element.
96+
*
97+
* @return Radio
98+
*/
99+
public function radio()
100+
{
101+
return Radio::create()
102+
->type('radio')
103+
->class('custom-control-input');
104+
}
105+
106+
/**
107+
* Create a new toggler element.
108+
*
109+
* @return Toggle
110+
*/
111+
public function toggle()
112+
{
113+
return Toggle::create()
114+
->type('checkbox')
115+
->class('custom-control-input');
116+
}
117+
118+
/**
119+
* Create a new label element.
120+
*
121+
* @return Label
122+
*/
123+
public function label()
124+
{
125+
return Label::create()->class('font-weight-bold');
126+
}
127+
128+
/**
129+
* Create a new error element.
130+
*
131+
* @return Span
132+
*/
133+
public function error()
134+
{
135+
return Span::create()->class('invalid-feedback');
136+
}
137+
}

app/Http/Html/Radio.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Http\Html;
4+
5+
class Radio extends Checkbox
6+
{
7+
//
8+
}

app/Http/Html/Toggle.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Html;
4+
5+
class Toggle extends Checkbox
6+
{
7+
/**
8+
* Wrap the content in a div.
9+
*
10+
* @param string $content
11+
*
12+
* @return \Spatie\Html\Elements\Div
13+
*/
14+
public function wrap($content)
15+
{
16+
return html()->div($content)->class(['custom-control', 'custom-switch']);
17+
}
18+
}

app/Http/Html/TrixEditor.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Http\Html;
4+
5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
7+
use Spatie\Html\Attributes;
8+
use Spatie\Html\Elements\Input;
9+
10+
class TrixEditor extends Input
11+
{
12+
/**
13+
* The trix editor tag.
14+
*
15+
* @var string
16+
*/
17+
protected $tag = 'trix-editor';
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function toHtml() : string
23+
{
24+
// Trix requires a unique ID set for each instance. In case
25+
// we do not have an ID, we will generate a unique one.
26+
$id = $this->getTrixId();
27+
28+
// For proper rendering of the trix editor, we must
29+
// add a hidden attribute with the HTML content.
30+
$hidden = html()->hidden()->id($id)->attributes($this->getHiddenAttributes());
31+
32+
// Clear all attributes.
33+
$this->attributes = (new Attributes())->setAttributes($this->getTrixAttributes());
34+
$this->attributes->setAttribute('input', $id);
35+
36+
// Prepend the hidden input element.
37+
return $hidden->toHtml().parent::toHtml();
38+
}
39+
40+
/**
41+
* Get the attributes for the hidden input element.
42+
*
43+
* @return array
44+
*/
45+
protected function getHiddenAttributes()
46+
{
47+
return Arr::only($this->attributes->toArray(), ['value', 'name', 'id']);
48+
}
49+
50+
/**
51+
* Get the trix element ID, or generate a unique one.
52+
*
53+
* @return string
54+
*/
55+
protected function getTrixId()
56+
{
57+
return $this->getAttribute('id', 'trix-'.Str::random(5));
58+
}
59+
60+
/**
61+
* Get the valid trix editor attributes.
62+
*
63+
* @return array
64+
*/
65+
protected function getTrixAttributes()
66+
{
67+
return Arr::only($this->attributes->toArray(), ['placeholder', 'autofocus']);
68+
}
69+
}

app/Providers/FormServiceProvider.php

-80
This file was deleted.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"laravel/framework": "^6.0",
2020
"laravel/tinker": "^1.0",
2121
"laravel/ui": "^1.0",
22-
"laravelcollective/html": "^6.0",
2322
"spatie/laravel-activitylog": "^3.8",
23+
"spatie/laravel-html": "^2.24",
2424
"spatie/valuestore": "^1.2"
2525
},
2626
"require-dev": {

config/app.php

-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@
171171
App\Providers\RouteServiceProvider::class,
172172
App\Providers\LdapConnectionProvider::class,
173173
App\Providers\InstallationServiceProvider::class,
174-
App\Providers\FormServiceProvider::class,
175174
],
176175

177176
/*

0 commit comments

Comments
 (0)