Skip to content

Commit a47009b

Browse files
authored
refactor(input): added counter (#812)
* refactor: added counter * docs: added relevant comments * fix: now it makes sense * docs: we need to hide this for the moment especially as it's not positioned correctly
1 parent f62a3e7 commit a47009b

File tree

7 files changed

+52
-15
lines changed

7 files changed

+52
-15
lines changed

source/_patterns/01-elements/_form-elements.scss

+16
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,19 @@
110110
--formElement---borderColor: #{$db-color-red-500};
111111
}
112112
}
113+
114+
// counter styling
115+
%form-element-counter {
116+
margin-top: to-rem($pxValue: 12);
117+
118+
// TODO: add CSS anchor positioning
119+
120+
// Description styles
121+
// TODO: This probably still needs to get further changed to a general "hint" pattern or similar
122+
&,
123+
& + .description {
124+
color: $db-color-cool-gray-500;
125+
font-size: to-rem($pxValue: 14);
126+
padding: to-rem($pxValue: 6) to-rem($pxValue: 10);
127+
}
128+
}

source/_patterns/01-elements/input/input.hbs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
{{{ validation }}}
1212
{{#if datalist }} list="datalist{{ id }}"{{/if }}
1313
aria-labelledby="{{ id }}-label"
14-
{{#if variant}} data-variant="{{variant}}"{{/if }}>
14+
{{#if variant}} data-variant="{{variant}}"{{/if }}
15+
{{#if maxlength }} maxlength="{{ maxlength }}"
16+
oninput="result_{{ id }}.value=this.value.length+'/'+this.attributes.maxlength.value"{{/if }}>
1517
<label class="elm-label"
1618
for="{{ id }}"
1719
aria-hidden="true"
@@ -23,3 +25,4 @@
2325
<option{{#if value }} value="{{ value }}"{{/if }}>{{ text }}</option>
2426
{{/each }}
2527
</datalist>{{/if }}
28+
{{#if maxlength }}<output for="{{ id }}" id="result_{{ id }}"></output>{{/if }}

source/_patterns/01-elements/input/input.scss

+13
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@
145145
@extend %form-element-validation;
146146
@extend %form-element-validation-programmatic;
147147
/* stylelint-enable order/order */
148+
149+
&:is(
150+
[type="email"],
151+
[type="password"],
152+
[type="search"],
153+
[type="tel"],
154+
[type="text"],
155+
[type="url"]
156+
)
157+
+ label
158+
+ output {
159+
@extend %form-element-counter;
160+
}
148161
}
149162

150163
&[type="search"] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "text",
3+
"placeholder": "Projekt Name",
4+
"id": "input16",
5+
"label": "Textlabel",
6+
"maxlength": "100"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Input - maxlength
3+
order: 12
4+
hidden: true
5+
---
6+
7+
To set the maximum length of allowed characters on an input element, you could use the `maxlength`-attribute on the input field. This would additionally provide a visual counter.
8+
9+
Please keep in mind that you'll probably have to position the output element yourself in relation to the context of e.g. a wrapping HTML element; we'll have a future solution by CSS Anchor Positioning as well.

source/_patterns/01-elements/textarea/textarea.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
{{#if disabled}} disabled{{/if }}
77
{{#if describedbyid }} aria-describedby="{{ describedbyid }}"{{/if }}
88
{{{ validation }}}
9-
{{#if maxlength }} maxlength="{{ maxlength }}"
109
{{#if invalid }} aria-invalid="{{ invalid }}"{{/if }}
10+
{{#if maxlength }} maxlength="{{ maxlength }}"
1111
oninput="result_{{ id }}.value=this.value.length+'/'+this.attributes.maxlength.value"{{/if }}
1212
{{#if variant}} data-variant="{{variant}}"{{/if }}>{{ value }}</textarea>
1313
{{#if maxlength }}<output for="{{ id }}" id="result_{{ id }}"></output>{{/if }}

source/_patterns/01-elements/textarea/textarea.scss

+2-13
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,10 @@
6161
}
6262
}
6363

64-
// TODO: Evaluate whether those could get moved to ../_form-elements.scss
6564
& + output {
66-
// amount of signs output
67-
float: right;
68-
69-
margin-top: to-rem($pxValue: 12);
65+
@extend %form-element-counter;
7066

71-
// Description styles
72-
// TODO: This probably still needs to get further changed to a general "hint" pattern or similar
73-
&,
74-
& + .description {
75-
color: $db-color-cool-gray-500;
76-
font-size: to-rem($pxValue: 14);
77-
padding: to-rem($pxValue: 6) to-rem($pxValue: 10);
78-
}
67+
float: right;
7968
}
8069

8170
// ### Style variations

0 commit comments

Comments
 (0)