diff --git a/ui/app/components/stepper-input.js b/ui/app/components/stepper-input.js new file mode 100644 index 000000000..eeef1c3bb --- /dev/null +++ b/ui/app/components/stepper-input.js @@ -0,0 +1,68 @@ +import Component from '@ember/component'; +import { action } from '@ember/object'; +import { debounce } from '@ember/runloop'; +import { oneWay } from '@ember/object/computed'; +import { classNames } from '@ember-decorators/component'; +import classic from 'ember-classic-decorator'; + +const ESC = 27; + +@classic +@classNames('stepper-input') +export default class StepperInput extends Component { + min = 0; + max = 10; + value = 0; + debounce = 500; + onChange() {} + + // Internal value changes immediately for instant visual feedback. + // Value is still the public API and is expected to mutate and re-render + // On onChange which is debounced. + @oneWay('value') internalValue; + + // text change: debounce set value if value changed + // debouncing here means when the text input blurs to click a button + // things don't get weird and send two change events + // text focus ESC: revert value + // + // Also add the xsmall button to the existing button story + + @action + increment() { + if (this.internalValue < this.max) { + this.incrementProperty('internalValue'); + this.update(this.internalValue); + } + } + + @action + decrement() { + if (this.internalValue > this.min) { + this.decrementProperty('internalValue'); + this.update(this.internalValue); + } + } + + @action + setValue(e) { + const newValue = Math.min(this.max, Math.max(this.min, e.target.value)); + this.set('internalValue', newValue); + this.update(this.internalValue); + } + + @action + resetTextInput(e) { + if (e.keyCode === ESC) { + e.target.value = this.internalValue; + } + } + + update(value) { + debounce(this, sendUpdateAction, value, this.debounce); + } +} + +function sendUpdateAction(value) { + return this.onChange(value); +} diff --git a/ui/app/templates/components/stepper-input.hbs b/ui/app/templates/components/stepper-input.hbs new file mode 100644 index 000000000..708276d45 --- /dev/null +++ b/ui/app/templates/components/stepper-input.hbs @@ -0,0 +1,22 @@ + + + +