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

Add support for min, max and step #306

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
52 changes: 42 additions & 10 deletions src/plugins/input-number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class HSInputNumber
private readonly increment: HTMLElement | null;
private readonly decrement: HTMLElement | null;
private inputValue: number | null;
private readonly minInputValue: number | null;
private readonly maxInputValue: number | null;
private readonly step: number;

constructor(el: HTMLElement, options?: IInputNumberOptions) {
super(el, options);
Expand All @@ -29,7 +32,22 @@ class HSInputNumber
this.el.querySelector('[data-hs-input-number-increment]') || null;
this.decrement =
this.el.querySelector('[data-hs-input-number-decrement]') || null;
this.inputValue = this.input ? parseInt(this.input.value) : 0;

this.inputValue = 0;
if (this.input) {
this.inputValue = !isNaN(parseInt(this.input.value)) ? parseInt(this.input.value) : 0;
}

const data = this.el.dataset.hsInputNumber;
const dataOptions: IInputNumberOptions = data ? JSON.parse(data) : { step: 1 };
const concatOptions = {
...dataOptions,
...options
};

this.minInputValue = ('min' in concatOptions) ? concatOptions.min : null;
this.maxInputValue = ('max' in concatOptions) ? concatOptions.max : null;
this.step = ('step' in concatOptions && concatOptions.step > 0) ? concatOptions.step : 1;

this.init();
}
Expand All @@ -45,9 +63,9 @@ class HSInputNumber
if (this.increment) this.buildIncrement();
if (this.decrement) this.buildDecrement();

if (this.inputValue <= 0) {
this.inputValue = 0;
this.input.value = '0';
if (this.minInputValue && this.inputValue <= this.minInputValue) {
this.inputValue = this.minInputValue;
this.input.value = this.minInputValue.toString();

this.changeValue();
}
Expand All @@ -73,33 +91,47 @@ class HSInputNumber

private changeValue(event = 'none') {
const payload = { inputValue: this.inputValue };
const minInputValue = this.minInputValue ?? Number.MIN_SAFE_INTEGER;
const maxInputValue = this.maxInputValue ?? Number.MAX_SAFE_INTEGER;

this.inputValue = isNaN(this.inputValue) ? 0 : this.inputValue;

switch (event) {
case 'increment':
this.inputValue += 1;
const incrementedResult = this.inputValue + this.step;
this.inputValue = incrementedResult >= minInputValue && incrementedResult <= maxInputValue ? incrementedResult : maxInputValue;
this.input.value = this.inputValue.toString();
break;
case 'decrement':
this.inputValue -= this.inputValue <= 0 ? 0 : 1;
const decrementedResult = this.inputValue - this.step;
this.inputValue = decrementedResult >= minInputValue && decrementedResult <= maxInputValue ? decrementedResult : minInputValue;
this.input.value = this.inputValue.toString();
break;
default:
this.inputValue =
parseInt(this.input.value) <= 0 ? 0 : parseInt(this.input.value);
if (this.inputValue <= 0) this.input.value = this.inputValue.toString();
const defaultResult = isNaN(parseInt(this.input.value)) ? 0 : parseInt(this.input.value);
this.inputValue = defaultResult >= maxInputValue ? maxInputValue : defaultResult <= minInputValue ? minInputValue : defaultResult;
if (this.inputValue <= minInputValue) this.input.value = this.inputValue.toString();
break;
}

payload.inputValue = this.inputValue;

if (this.inputValue === 0) {
if (this.inputValue === minInputValue) {
this.el.classList.add('disabled');
if (this.decrement) this.disableButtons('decrement');
} else {
this.el.classList.remove('disabled');
if (this.decrement) this.enableButtons('decrement');
}

if (this.inputValue === maxInputValue) {
this.el.classList.add('disabled');
if (this.increment) this.disableButtons('increment');
} else {
this.el.classList.remove('disabled');
if (this.increment) this.enableButtons('increment');
}

this.fireEvent('change', payload);
dispatch('change.hs.inputNumber', this.el, payload);
}
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/input-number/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export interface IInputNumberOptions {
min?: number;
max?: number;
step?: number;
}

export interface IInputNumber {
Expand Down