Skip to content

Commit eddb9f4

Browse files
authored
Set min or max values on blur for outside boundaries (#17)
* Set min or max values on blur for outside boundaries * Use `&&` operator instead of `||` * Extract `validateMinMax` method * Add input min max validation docs
1 parent 62e05fc commit eddb9f4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

docs/pages/components/input.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ the [Properties table](#properties) for the full list of options.
165165
<br />
166166
```
167167

168+
### Min Max Validation
169+
170+
```html:preview
171+
<zn-input type="date" label="Input type: Date" placeholder="Date" help-text="Calendar icon opens the browser default date picker" min="01/01/1970"></zn-input>
172+
<br />
173+
<zn-input type="number" label="Input type: Number" min="10" max="100"></zn-input>
174+
```
175+
168176
### Prefix & Suffix Icons
169177

170178
Several input types have specific `prefix` and `suffix` elements or icons that are displayed by default. You can also

src/components/input/input.component.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,21 @@ export default class ZnInput extends ZincElement implements ZincFormControl {
245245
return this.input.validationMessage;
246246
}
247247

248+
private validateMinMax(): void {
249+
if (this.type !== 'number' && this.type !== 'date') {
250+
return;
251+
}
252+
if (typeof this.min !== 'undefined' && parseInt(this.value as string) < parseInt(this.min as string)) {
253+
this.value = this.min.toString();
254+
}
255+
if (typeof this.max !== 'undefined' && parseInt(this.value as string) > parseInt(this.max as string)) {
256+
this.value = this.max.toString();
257+
}
258+
}
259+
248260
private handleBlur() {
249261
this.hasFocus = false;
262+
this.validateMinMax();
250263
this.emit('zn-blur');
251264
}
252265

@@ -464,7 +477,7 @@ export default class ZnInput extends ZincElement implements ZincFormControl {
464477
'input--disabled': this.disabled,
465478
'input--focused': this.hasFocus,
466479
'input--empty': !this.value,
467-
'input--no-spin-buttons': this.noSpinButtons || this.type !== 'currency',
480+
'input--no-spin-buttons': this.noSpinButtons && this.type !== 'currency',
468481
})}>
469482
470483
<span part="prefix" class="input__prefix">

0 commit comments

Comments
 (0)