Skip to content

Commit c50e7dd

Browse files
committed
Fix #120: BS4 Custom Controls Enhancements: Switch and File
1 parent a19cdaf commit c50e7dd

File tree

8 files changed

+221
-53
lines changed

8 files changed

+221
-53
lines changed

.github/FUNDING.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
# github: [kartik-v]
4+
open_collective: yii2-widgets

CHANGE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Change Log: `yii2-widget-activeform`
55

66
**Date**: _under development_
77

8+
- (enh #120): BS4 Custom Controls Enhancements: Switch and File.
9+
810
## Version 1.5.8
911

1012
**Date**: 24-Feb-2019

src/ActiveField.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ public function init()
499499
* @param array $options the tag options in terms of name-value pairs. The following options are specially
500500
* handled:
501501
*
502-
* - `custom`: _bool_, whether to render bootstrap 4.x custom checkbox/radio styled control. Defaults to `false`.
502+
* - `custom`: _bool_, whether to render bootstrap 4.x custom checkbox styled control. Defaults to `false`.
503+
* This is applicable only for Bootstrap 4.x forms.
504+
* - `switch`: _bool_, whether to render bootstrap 4.x custom switch styled control. Defaults to `false`.
503505
* This is applicable only for Bootstrap 4.x forms.
504506
* @see https://getbootstrap.com/docs/4.1/components/forms/#checkboxes-and-radios-1
505507
* - `uncheck`: _string_, the value associated with the uncheck state of the checkbox. If not set, it will take
@@ -608,6 +610,9 @@ protected function isCustomControl(&$options)
608610
public function fileInput($options = [])
609611
{
610612
if ($this->isCustomControl($options)) {
613+
$view = $this->form->getView();
614+
Bs4CustomFileInputAsset::register($view);
615+
$view->registerJs('bsCustomFileInput.init();');
611616
Html::removeCssClass($options, 'form-control');
612617
Html::addCssClass($options, 'custom-file-input');
613618
Html::addCssClass($this->labelOptions, 'custom-file-label');
@@ -696,7 +701,7 @@ public function passwordInput($options = [])
696701
* @param array $options the tag options in terms of name-value pairs. The following options are specially
697702
* handled:
698703
*
699-
* - `custom`: _bool_, whether to render bootstrap 4.x custom checkbox/radio styled control. Defaults to `false`.
704+
* - `custom`: _bool_, whether to render bootstrap 4.x custom radio styled control. Defaults to `false`.
700705
* This is applicable only for Bootstrap 4.x forms.
701706
* @see https://getbootstrap.com/docs/4.1/components/forms/#checkboxes-and-radios-1
702707
* - `uncheck`: _string_, the value associated with the uncheck state of the radio button. If not set, it will take the
@@ -987,8 +992,9 @@ protected function getColCss($size)
987992
protected function getToggleField($type = self::TYPE_CHECKBOX, $options = [], $enclosedByLabel = null)
988993
{
989994
$this->initDisability($options);
990-
$custom = $this->isCustomControl($options);
991995
$isBs4 = $this->form->isBs4();
996+
$custom = $this->isCustomControl($options);
997+
$switch = ArrayHelper::remove($options, 'switch', false) && $isBs4 && $type === self::TYPE_CHECKBOX;
992998
if ($enclosedByLabel === null) {
993999
$enclosedByLabel = !$isBs4 && !$custom;
9941000
}
@@ -1006,7 +1012,7 @@ protected function getToggleField($type = self::TYPE_CHECKBOX, $options = [], $e
10061012
Html::addCssClass($this->labelOptions, "{$prefix}-label");
10071013
Html::addCssClass($options, "{$prefix}-input");
10081014
if ($custom) {
1009-
Html::addCssClass($this->checkWrapperOptions, "custom-{$type}");
1015+
Html::addCssClass($this->checkWrapperOptions, 'custom-' . ($switch ? 'switch' : $type));
10101016
}
10111017
} elseif (!$enclosedByLabel) {
10121018
Html::addCssClass($this->checkWrapperOptions, "not-enclosed");

src/Bs4CustomFileInputAsset.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2019
5+
* @package yii2-widgets
6+
* @subpackage yii2-widget-activeform
7+
* @version 1.5.9
8+
*/
9+
10+
namespace kartik\form;
11+
12+
use kartik\base\PluginAssetBundle;
13+
14+
/**
15+
* Asset bundle for the custom file input animation for bootstrap 4.
16+
*
17+
* @author Kartik Visweswaran <[email protected]>
18+
* @since 1.0
19+
*/
20+
class Bs4CustomFileInputAsset extends PluginAssetBundle
21+
{
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function init()
26+
{
27+
$this->setSourcePath(__DIR__ . '/assets');
28+
$this->setupAssets('js', ['js/bs-custom-file-input']);
29+
parent::init();
30+
}
31+
}

src/assets/css/activeform.css

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,51 @@
1717
cursor: help;
1818
opacity: 0.8;
1919
}
20-
2120
.kv-hint-icon:hover, .kv-hint-icon:focus {
2221
opacity: 1;
2322
}
24-
2523
.kv-hint-content {
2624
opacity: 0.9;
2725
}
28-
2926
.kv-hint-label {
3027
border-bottom: 1px dashed #888;
3128
cursor: help;
3229
}
33-
3430
label.disabled, label.readonly, .disabled label, .readonly label {
3531
cursor: not-allowed;
3632
filter: alpha(opacity=65);
3733
-webkit-box-shadow: none;
3834
box-shadow: none;
3935
opacity: .65;
4036
}
41-
4237
@media (min-width: 768px) {
4338
.form-inline .form-group, .form-inline .form-control {
4439
vertical-align: top;
4540
}
46-
4741
.form-inline .btn-default {
4842
margin: 0;
4943
}
50-
5144
.form-inline .checkbox, .form-inline .radio {
5245
padding-top: 6px;
5346
}
5447
}
55-
5648
.input-multiselect {
5749
overflow: auto;
5850
min-height: 145px !important;
5951
}
60-
6152
.kv-form-bs4.tooltip-feedback .form-group.has-error {
6253
position: relative;
6354
margin-bottom: 2rem;
6455
}
65-
6656
/**
6757
* feedback icon styling
6858
*/
6959
.kv-form-bs4 .has-feedback {
7060
position: relative;
7161
}
72-
7362
.kv-form-bs4 .has-feedback .form-control {
7463
padding-right: 2.65625rem;
7564
}
76-
7765
.kv-form-bs4 .form-control-feedback {
7866
position: absolute;
7967
top: 0;
@@ -85,80 +73,65 @@ label.disabled, label.readonly, .disabled label, .readonly label {
8573
text-align: center;
8674
pointer-events: none;
8775
}
88-
8976
.has-feedback.has-size-lg .form-control-feedback {
9077
font-size: 18px;
9178
top: 31px;
9279
}
93-
9480
.has-feedback.has-size-sm .form-control-feedback {
9581
font-size: 12px;
9682
}
97-
9883
.kv-form-bs4 .has-feedback.has-size-lg .form-control-feedback {
9984
width: 2.875rem;
10085
height: 2.875rem;
10186
line-height: 2.875rem;
10287
font-size: 1.25rem;
10388
}
104-
10589
.kv-form-bs4 .has-feedback.has-size-sm .form-control-feedback {
10690
width: 1.75rem;
10791
height: 1.75rem;
10892
line-height: 1.75rem;
10993
font-size: 0.875rem;
11094
}
111-
11295
.has-error .form-control-feedback {
11396
color: #a94442;
11497
}
115-
11698
.kv-form-bs4 .has-feedback label ~ .form-control-feedback {
11799
top: 2.125rem;
118100
}
119-
120101
.kv-form-bs4 .has-feedback label.sr-only ~ .form-control-feedback {
121102
top: 0;
122103
}
123-
124104
.kv-feedback-success,
125105
.kv-feedback-error,
126106
.has-error .kv-feedback-default,
127107
.has-success .kv-feedback-default {
128108
display: none;
129109
}
130-
131110
.has-error .kv-feedback-error,
132111
.has-success .kv-feedback-success {
133112
display: inline-block;
134113
}
135-
136114
@media (min-width: 768px) {
137115
.kv-form-bs4.form-inline .has-feedback .form-control-feedback,
138116
.kv-form-bs4.navbar-form .has-feedback .form-control-feedback {
139117
top: 0;
140118
}
141119
}
142-
143120
.kv-form-bs4.form-horizontal .has-feedback .form-control-feedback {
144121
right: 0.9375rem;
145122
}
146-
147123
/**
148124
* multiple addons border fix
149125
**/
150126
.input-group .input-group-addon:not(:last-child) {
151127
border-right: 0;
152128
}
153-
154129
.input-group .input-group-addon:last-child {
155130
border-left: 1px solid #ccc;
156131
}
157-
158132
.form-control + .input-group-addon:not(:first-child) {
159133
border-left: 0;
160134
}
161-
162135
/**
163136
* addons success and error states
164137
**/
@@ -167,113 +140,93 @@ label.disabled, label.readonly, .disabled label, .readonly label {
167140
background-color: #dff0d8;
168141
border-color: #3c763d;
169142
}
170-
171143
.has-error.highlight-addon .input-group-addon {
172144
color: #a94442;
173145
background-color: #f2dede;
174146
border-color: #a94442;
175147
}
176-
177148
.has-success.highlight-addon .input-group-btn .btn:not(:disabled):not(.disabled) {
178149
color: #fff;
179150
background-color: #449d44;
180151
border-color: #398439;
181152
}
182-
183153
.has-success.highlight-addon .input-group-btn .btn:not(:disabled):not(.disabled):hover {
184154
color: #fff;
185155
background-color: #449d44;
186156
border-color: #398439;
187157
}
188-
189158
.has-error.highlight-addon .input-group-btn .btn:not(:disabled):not(.disabled) {
190159
color: #fff;
191160
background-color: #c9302c;
192161
border-color: #ac2925;
193162
}
194-
195163
.has-error.highlight-addon .input-group-btn .btn:not(:disabled):not(.disabled):hover {
196164
color: #fff;
197165
background-color: #c9302c;
198166
border-color: #ac2925;
199167
}
200-
201168
.kv-form-bs4 .has-success.highlight-addon .input-group-text {
202169
color: #28a745;
203170
background-color: #d4edda;
204171
border-color: #28a745;
205172
}
206-
207173
.kv-form-bs4 .has-error.highlight-addon .input-group-text {
208174
color: #dc3545;
209175
background-color: #f8d7da;
210176
border-color: #dc3545;
211177
}
212-
213178
.kv-form-bs4 .has-success.highlight-addon .input-group .btn:not(:disabled):not(.disabled) {
214179
color: #fff;
215180
background-color: #28a745;
216181
border-color: #28a745;
217182
}
218-
219183
.kv-form-bs4 .has-success.highlight-addon .input-group .btn:not(:disabled):not(.disabled):hover {
220184
color: #fff;
221185
background-color: #218838;
222186
border-color: #1e7e34;
223187
}
224-
225188
.kv-form-bs4 .has-success.highlight-addon .input-group .btn:not(:disabled):not(.disabled):focus {
226189
box-shadow: 0 0 0 0.2rem rgba(40, 167, 69, 0.5);
227190
}
228-
229191
.kv-form-bs4 .has-error.highlight-addon .input-group .btn:not(:disabled):not(.disabled) {
230192
color: #fff;
231193
background-color: #dc3545;
232194
border-color: #dc3545;
233195
}
234-
235196
.kv-form-bs4 .has-error.highlight-addon .input-group .btn:not(:disabled):not(.disabled):hover {
236197
color: #fff;
237198
background-color: #c82333;
238199
border-color: #bd2130;
239200
}
240-
241201
.kv-form-bs4 .has-error.highlight-addon .input-group .btn:not(:disabled):not(.disabled):focus {
242202
box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.5);
243203
}
244-
245204
.kv-form-bs4 .has-error .invalid-feedback {
246205
display: block;
247206
}
248-
249207
.kv-form-bs4 .hint-block {
250208
font-size: 0.75rem;
251209
margin-top: 0.375rem;
252210
color: #999;
253211
}
254-
255212
.checkbox.not-enclosed,
256213
.radio.not-enclosed {
257214
padding-left: 20px;
258215
}
259-
260216
.checkbox.not-enclosed label,
261217
.radio.not-enclosed label {
262218
padding-left: 2px;
263219
}
264-
265220
.form-inline .checkbox.not-enclosed,
266221
.form-inline .radio.not-enclosed {
267222
padding-left: 5px;
268223
padding-right: 5px;
269224
}
270-
271225
.form-inline .checkbox.not-enclosed label,
272226
.form-inline .radio.not-enclosed label {
273227
margin-top: -5px;
274228
}
275-
276-
.form-group.required .has-star:not(.custom-control-label)::after,
229+
.form-group.required .has-star:not(.custom-control-label):not(.custom-file-label)::after,
277230
.is-required::after {
278231
content: "*";
279232
margin-left: 3px;

src/assets/css/activeform.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)