From 72b081ef6c761793a2e2972045e9569d8940c70a Mon Sep 17 00:00:00 2001 From: NicolasMerget Date: Thu, 11 Apr 2024 12:03:50 +0200 Subject: [PATCH] fix: issue with user-valid for form-components --- .../src/components/checkbox/checkbox.scss | 2 +- .../src/components/radio/radio.scss | 2 +- .../src/styles/_form-components.scss | 55 ++++++++----------- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/packages/components/src/components/checkbox/checkbox.scss b/packages/components/src/components/checkbox/checkbox.scss index f23458763d6..5c39c8c7db2 100644 --- a/packages/components/src/components/checkbox/checkbox.scss +++ b/packages/components/src/components/checkbox/checkbox.scss @@ -6,7 +6,7 @@ @use "../../styles/form-components"; .db-checkbox { - @extend %check-element; + @include form-components.set-default-check-element(check); input { border-radius: variables.$db-border-radius-sm; diff --git a/packages/components/src/components/radio/radio.scss b/packages/components/src/components/radio/radio.scss index dd94fd6d16a..19cd4a84af2 100644 --- a/packages/components/src/components/radio/radio.scss +++ b/packages/components/src/components/radio/radio.scss @@ -3,7 +3,7 @@ @use "../../styles/form-components"; .db-radio { - @extend %check-element; + @include form-components.set-default-check-element(radio); input { border-color: currentColor; diff --git a/packages/components/src/styles/_form-components.scss b/packages/components/src/styles/_form-components.scss index d902c9e92bf..13b27c8284f 100644 --- a/packages/components/src/styles/_form-components.scss +++ b/packages/components/src/styles/_form-components.scss @@ -136,33 +136,24 @@ $floating-label-size: calc( } } -@function get-validations($selector, $key) { - $has-selectors: ""; +// This doesn't contain text, search and password, because they don't have an auto-validation +$input-valid-types: "color", "date", "datetime-local", "email", "file", "hidden", + "month", "number", "range", "tel", "time", "url", "week"; +@function get-validations($selector, $key) { $validations: ":required"; - $user: "user-"; - - // Differentiating form elements - @if ($selector == select or $selector == check) { - $user: ""; - } @else { - // TODO: add validations for input & textarea as well - // https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#using_built-in_form_validation - $validations: ":required"; - } - // We need to do a more complicated check for non-typed-input-elements @if ($selector == input) { - $validations: ':not([type="text"], [type="password"], [type="search"]),:is([type="text"], [type="password"], [type="search"]):required'; - } - - // Check elements (radio and checkbox) should get selected by their native HTML tag `input` - @if ($selector == check) { - $selector: input; + // https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#using_built-in_form_validation + $validations: ":required, [minlength], [maxlength], [pattern]"; + @each $type in $input-valid-types { + $validations: $validations + ", [type=#{$type}]"; + } + } @else if($selector == textarea) { + $validations: ":required, [minlength], [maxlength]"; } - $has-selectors: $has-selectors + - "&:has(#{$selector}:not([aria-invalid]):is(#{$validations}):#{$user}#{$key}),"; + $has-selectors: "&:has(#{$selector}:not([aria-invalid]):is(#{$validations}):user-#{$key}),"; @return string.unquote(string.slice($has-selectors, 1, -2)); } @@ -173,17 +164,19 @@ $floating-label-size: calc( $boolean: "false"; } - #{get-validations($selector,$key)} { - @content; + $user: ""; + @if ($selector == check) { + $user: "user-"; } - @if ($selector == check) { + @if ($selector == check or $selector == radio) { $selector: input; - } - // :user-valid or :user-invalid workaround - @supports not selector(:user-#{$key}) { - &:has(#{$selector}:not([aria-invalid]):required:#{$key}) { + &:has(#{$selector}:not([aria-invalid]):required:#{$user}#{$key}) { + @content; + } + } @else { + #{get-validations($selector,$key)} { @content; } } @@ -367,13 +360,13 @@ $floating-label-size: calc( } } -%check-element { +@mixin set-default-check-element($selector) { @include set-required-label(input); - @include get-validity(check) { + @include get-validity($selector) { @include get-validity-color-check("valid"); } - @include get-validity(check, "invalid") { + @include get-validity($selector, "invalid") { @include get-validity-color-check("invalid"); }