Skip to content

Commit deea117

Browse files
mdoXhmikosR
authored andcommitted
Add form-validation-states Sass map (twbs#27999)
1 parent fd9dc1a commit deea117

4 files changed

Lines changed: 52 additions & 11 deletions

File tree

scss/_forms.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ textarea.form-control {
241241
// pseudo-classes but also includes `.is-invalid` and `.is-valid` classes for
242242
// server side validation.
243243

244-
@include form-validation-state("valid", $form-feedback-valid-color);
245-
@include form-validation-state("invalid", $form-feedback-invalid-color);
244+
@each $state, $data in $form-validation-states {
245+
@include form-validation-state($state, map-get($data, color), map-get($data, icon));
246+
}
246247

247248
// Inline forms
248249
//

scss/_variables.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,21 @@ $form-feedback-icon-valid: str-replace(url("data:image/svg+xml,%3csvg x
658658
$form-feedback-icon-invalid-color: $form-feedback-invalid-color !default;
659659
$form-feedback-icon-invalid: str-replace(url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='#{$form-feedback-icon-invalid-color}' viewBox='-2 -2 7 7'%3e%3cpath stroke='#{$form-feedback-icon-invalid-color}' d='M0 0l3 3m0-3L0 3'/%3e%3ccircle r='.5'/%3e%3ccircle cx='3' r='.5'/%3e%3ccircle cy='3' r='.5'/%3e%3ccircle cx='3' cy='3' r='.5'/%3e%3c/svg%3E"), "#", "%23") !default;
660660

661+
$form-validation-states: () !default;
662+
// stylelint-disable-next-line scss/dollar-variable-default
663+
$form-validation-states: map-merge(
664+
(
665+
"valid": (
666+
"color": $form-feedback-valid-color,
667+
"icon": $form-feedback-icon-valid
668+
),
669+
"invalid": (
670+
"color": $form-feedback-invalid-color,
671+
"icon": $form-feedback-icon-invalid
672+
),
673+
),
674+
$form-validation-states
675+
);
661676

662677
// Z-index master list
663678
//

scss/mixins/_forms.scss

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
}
2727

2828

29-
@mixin form-validation-state($state, $color) {
29+
@mixin form-validation-state($state, $color, $icon) {
3030
.#{$state}-feedback {
3131
display: none;
3232
width: 100%;
@@ -57,15 +57,10 @@
5757

5858
@if $enable-validation-icons {
5959
padding-right: $input-height-inner;
60+
background-image: $icon;
6061
background-repeat: no-repeat;
6162
background-position: center right calc(#{$input-height-inner} / 4);
6263
background-size: calc(#{$input-height-inner} / 2) calc(#{$input-height-inner} / 2);
63-
64-
@if $state == "valid" {
65-
background-image: $form-feedback-icon-valid;
66-
} @else {
67-
background-image: $form-feedback-icon-invalid;
68-
}
6964
}
7065

7166
&:focus {
@@ -97,9 +92,8 @@
9792
border-color: $color;
9893

9994
@if $enable-validation-icons {
100-
$form-feedback-icon: if($state == "valid", $form-feedback-icon-valid, $form-feedback-icon-invalid);
10195
padding-right: $custom-select-feedback-icon-padding-right;
102-
background: $custom-select-background, $form-feedback-icon no-repeat $custom-select-feedback-icon-position / $custom-select-feedback-icon-size;
96+
background: $custom-select-background, $icon no-repeat $custom-select-feedback-icon-position / $custom-select-feedback-icon-size;
10397
}
10498

10599
&:focus {

site/docs/4.2/components/forms.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,37 @@ If your form layout allows it, you can swap the `.{valid|invalid}-feedback` clas
11071107
{% endcapture %}
11081108
{% include example.html content=example %}
11091109

1110+
### Customizing
1111+
1112+
Validation states can be customized via Sass with the `$form-validation-states` map. Located in our `_variables.scss` file, this Sass map is looped over to generate the default `valid`/`invalid` validation states. Included is a nested map for customizing each state's color and icon. While no other states are supported by browsers, those using custom styles can easily add more complex form feedback.
1113+
1114+
Please note that we do not recommend customizing these values without also modifying the `form-validation-state` mixin.
1115+
1116+
{% highlight scss %}
1117+
// Sass map from `_variables.scss`
1118+
// Override this and recompile your Sass to generate different states
1119+
$form-validation-states: map-merge(
1120+
(
1121+
"valid": (
1122+
"color": $form-feedback-valid-color,
1123+
"icon": $form-feedback-icon-valid
1124+
),
1125+
"invalid": (
1126+
"color": $form-feedback-invalid-color,
1127+
"icon": $form-feedback-icon-invalid
1128+
),
1129+
),
1130+
$form-validation-states
1131+
);
1132+
1133+
// Loop from `_forms.scss`
1134+
// Any modifications to the above Sass map will be reflected in your compiled
1135+
// CSS via this loop.
1136+
@each $state, $data in $form-validation-states {
1137+
@include form-validation-state($state, map-get($data, color), map-get($data, icon));
1138+
}
1139+
{% endhighlight %}
1140+
11101141
## Custom forms
11111142

11121143
For even more customization and cross browser consistency, use our completely custom form elements to replace the browser defaults. They're built on top of semantic and accessible markup, so they're solid replacements for any default form control.

0 commit comments

Comments
 (0)