Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with user-valid for form-components #2496

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/radio/radio.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 24 additions & 31 deletions packages/components/src/styles/_form-components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
nmerget marked this conversation as resolved.
Show resolved Hide resolved
@each $type in $input-valid-types {
$validations: $validations + ", [type=#{$type}]";
}
} @else if($selector == textarea) {
$validations: ":required, [minlength], [maxlength]";
nmerget marked this conversation as resolved.
Show resolved Hide resolved
}

$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));
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -367,13 +360,13 @@ $floating-label-size: calc(
}
}

%check-element {
@mixin set-default-check-element($selector) {
nmerget marked this conversation as resolved.
Show resolved Hide resolved
@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");
}

Expand Down
Loading