Skip to content

Commit 64ebfb7

Browse files
committed
Implement character-limit for kol-input-email
Refs: #7162
1 parent 0db0e4b commit 64ebfb7

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

packages/components/src/components/input-email/controller.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Generic } from 'adopted-style-sheets';
22

3-
import type { InputEmailProps, InputEmailWatches, MultiplePropType } from '../../schema';
4-
import { validateMultiple } from '../../schema';
3+
import type { CharacterLimitPropType, InputEmailProps, InputEmailWatches, MultiplePropType } from '../../schema';
4+
import { validateCharacterLimit, validateMultiple } from '../../schema';
55

66
import { InputTextEmailController } from '../input-text/controller';
77

@@ -17,8 +17,13 @@ export class InputEmailController extends InputTextEmailController implements In
1717
validateMultiple(this.component, value);
1818
}
1919

20+
public validateCharacterLimit(value?: CharacterLimitPropType): void {
21+
validateCharacterLimit(this.component, value);
22+
}
23+
2024
public componentWillLoad(): void {
2125
super.componentWillLoad();
26+
this.validateCharacterLimit(this.component._characterLimit);
2227
this.validateMultiple(this.component._multiple);
2328
}
2429
}

packages/components/src/components/input-email/shadow.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import clsx from 'clsx';
44

55
import type {
66
ButtonProps,
7+
CharacterLimitPropType,
78
FocusableElement,
89
HideMsgPropType,
910
IconsHorizontalPropType,
@@ -75,6 +76,7 @@ export class KolInputEmail implements InputEmailAPI, FocusableElement {
7576
private readonly onInput = (event: InputEvent) => {
7677
const value = (event.target as HTMLInputElement).value;
7778
setState(this, '_currentLength', value.length);
79+
this.controller.updateCurrentLengthDebounced(value.length);
7880
this._value = value;
7981
this.controller.onFacade.onInput(event);
8082
};
@@ -92,10 +94,13 @@ export class KolInputEmail implements InputEmailAPI, FocusableElement {
9294
}
9395

9496
private getInputProps(): InputStateWrapperProps {
97+
const ariaDescribedBy = this.controller.hasCharacterLimit() ? [`${this.state._id}-character-limit-hint`] : undefined; // When a character limit is defined, we provide an additional hint referenced by aria-describedby.
98+
9599
return {
96100
ref: this.catchRef,
97101
state: this.state,
98102
type: 'email',
103+
ariaDescribedBy,
99104
...this.controller.onFacade,
100105
onInput: this.onInput,
101106
onKeyDown: this.onKeyDown,
@@ -133,16 +138,15 @@ export class KolInputEmail implements InputEmailAPI, FocusableElement {
133138
@Prop() public _autoComplete?: InputTypeOnOff;
134139

135140
/**
136-
* Makes the element not focusable and ignore all events.
137-
* @TODO: Change type back to `DisabledPropType` after Stencil#4663 has been resolved.
141+
* When defined, a remaining characters counter is shown. The field is marked as invalid when the character limit has been exceeded.
138142
*/
139-
@Prop() public _disabled?: boolean = false;
143+
@Prop() public _characterLimit?: CharacterLimitPropType;
140144

141145
/**
142-
* Shows the character count on the lower border of the input.
143-
* @TODO: Change type back to `HasCounterPropType` after Stencil#4663 has been resolved.
146+
* Makes the element not focusable and ignore all events.
147+
* @TODO: Change type back to `DisabledPropType` after Stencil#4663 has been resolved.
144148
*/
145-
@Prop() public _hasCounter?: boolean = false;
149+
@Prop() public _disabled?: boolean = false;
146150

147151
/**
148152
* Hides the error message but leaves it in the DOM for the input's aria-describedby.
@@ -264,6 +268,7 @@ export class KolInputEmail implements InputEmailAPI, FocusableElement {
264268
@State() public state: InputEmailStates = {
265269
_autoComplete: 'off',
266270
_currentLength: 0,
271+
_currentLengthDebounced: 0,
267272
_hasValue: false,
268273
_hideMsg: false,
269274
_id: `id-${nonce()}`,
@@ -291,16 +296,16 @@ export class KolInputEmail implements InputEmailAPI, FocusableElement {
291296
this.controller.validateAutoComplete(value);
292297
}
293298

299+
@Watch('_characterLimit')
300+
public validateCharacterLimit(value?: CharacterLimitPropType): void {
301+
this.controller.validateCharacterLimit(value);
302+
}
303+
294304
@Watch('_disabled')
295305
public validateDisabled(value?: boolean): void {
296306
this.controller.validateDisabled(value);
297307
}
298308

299-
@Watch('_hasCounter')
300-
public validateHasCounter(value?: boolean): void {
301-
this.controller.validateHasCounter(value);
302-
}
303-
304309
@Watch('_hideMsg')
305310
public validateHideMsg(value?: HideMsgPropType): void {
306311
this.controller.validateHideMsg(value);

packages/components/src/schema/components/input-email.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { Generic } from 'adopted-style-sheets';
33
import type {
44
MsgPropType,
55
PropAccessKey,
6+
PropCharacterLimit,
67
PropDisabled,
7-
PropHasCounter,
88
PropHideLabel,
99
PropHideMsg,
1010
PropHorizontalIcons,
@@ -35,8 +35,8 @@ type OptionalProps = {
3535
smartButton: Stringified<ButtonProps>;
3636
value: string;
3737
} & PropAccessKey &
38+
PropCharacterLimit &
3839
PropDisabled &
39-
PropHasCounter &
4040
PropHideMsg &
4141
PropHideLabel &
4242
PropHorizontalIcons &
@@ -53,12 +53,13 @@ type RequiredStates = {
5353
autoComplete: InputTypeOnOff;
5454
hasValue: boolean;
5555
suggestions: W3CInputValue[];
56+
currentLength: number;
57+
currentLengthDebounced: number;
5658
} & PropId &
5759
PropHideMsg &
5860
PropLabelWithExpertSlot;
5961

6062
type OptionalStates = {
61-
currentLength: number;
6263
hint: string;
6364
maxLength: number;
6465
on: InputTypeOnDefault;
@@ -67,8 +68,8 @@ type OptionalStates = {
6768
smartButton: ButtonProps;
6869
value: string;
6970
} & PropAccessKey &
71+
PropCharacterLimit &
7072
PropDisabled &
71-
PropHasCounter &
7273
PropHideLabel &
7374
KoliBriHIcons &
7475
PropMsg &

0 commit comments

Comments
 (0)