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

fix(number-field): add support for scrubbing #4476

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
114 changes: 109 additions & 5 deletions packages/number-field/src/NumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ export class NumberField extends TextfieldBase {

_forcedUnit = '';

@property({ type: Boolean, reflect: true })
public scrubbing = false;

/**
* An `<sp-number-field>` element will process its numeric value with
* `new Intl.NumberFormat(this.resolvedLanguage, this.formatOptions).format(this.valueAsNumber)`
Expand Down Expand Up @@ -159,6 +162,9 @@ export class NumberField extends TextfieldBase {
@property({ type: Number, reflect: true, attribute: 'step-modifier' })
public stepModifier = 10;

@property({ type: Number })
public stepperpixel?: number;

@property({ type: Number })
public override set value(rawValue: number) {
const value = this.validateInput(rawValue);
Expand Down Expand Up @@ -293,13 +299,29 @@ export class NumberField extends TextfieldBase {
private change!: (event: PointerEvent) => void;
private safty!: number;
private languageResolver = new LanguageResolutionController(this);
private pointerDragXLocation?: number;
private pointerDownTime?: number;
private scrubDistance = 0;

private handlePointerdown(event: PointerEvent): void {
if (event.button !== 0) {
event.preventDefault();
return;
}
this.managedInput = true;

if (!this.focused) {
this.setPointerCapture(event.pointerId);
this.scrub(event);
}
}

private handleButtonPointerdown(event: PointerEvent): void {
if (event.button !== 0) {
event.preventDefault();
return;
}
this.managedInput = true;
this.buttons.setPointerCapture(event.pointerId);
const stepUpRect = this.buttons.children[0].getBoundingClientRect();
const stepDownRect = this.buttons.children[1].getBoundingClientRect();
Expand Down Expand Up @@ -338,11 +360,11 @@ export class NumberField extends TextfieldBase {
this.change(event);
}

private handlePointermove(event: PointerEvent): void {
private handleButtonPointermove(event: PointerEvent): void {
this.findChange(event);
}

private handlePointerup(event: PointerEvent): void {
private handleButtonPointerup(event: PointerEvent): void {
this.buttons.releasePointerCapture(event.pointerId);
cancelAnimationFrame(this.nextChange);
clearTimeout(this.safty);
Expand Down Expand Up @@ -391,6 +413,81 @@ export class NumberField extends TextfieldBase {
this.stepBy(-1 * factor);
}

private handlePointermove(event: PointerEvent): void {
this.scrub(event);
}

private handlePointerup(event: PointerEvent): void {
this.releasePointerCapture(event.pointerId);
this.scrub(event);
cancelAnimationFrame(this.nextChange);
clearTimeout(this.safty);
this.managedInput = false;
this.setValue();
}

private scrub(event: PointerEvent): void {
switch (event.type) {
case 'pointerdown':
this.scrubbing = true;
this.pointerDragXLocation = event.clientX;
this.pointerDownTime = Date.now();
this.inputElement.disabled = true;
this.addEventListener('pointermove', this.handlePointermove);
this.addEventListener('pointerup', this.handlePointerup);
this.addEventListener('pointercancel', this.handlePointerup);
event.preventDefault();
break;

case 'pointermove':
if (
this.pointerDragXLocation &&
this.pointerDownTime &&
Date.now() - this.pointerDownTime > 250
) {
const amtPerPixel = this.stepperpixel || this._step;
const dist: number =
event.clientX - this.pointerDragXLocation;
const delta =
Math.round(dist * amtPerPixel) *
(event.shiftKey ? this.stepModifier : 1);
this.scrubDistance += Math.abs(dist);
this.pointerDragXLocation = event.clientX;
this.stepBy(delta);
event.preventDefault();
}
break;

default:
this.pointerDragXLocation = undefined;
this.scrubbing = false;
this.inputElement.disabled = false;
this.removeEventListener('pointermove', this.handlePointermove);
this.removeEventListener('pointerup', this.handlePointerup);
this.removeEventListener('pointercancel', this.handlePointerup);

// if user has scrubbed, disallow focus of field
const bounds = this.getBoundingClientRect();
if (
this.scrubDistance > 0 &&
this.pointerDownTime &&
Date.now() - this.pointerDownTime > 250
) {
event.preventDefault();
} else if (
event.clientX >= bounds.x &&
event.clientX <= bounds.x + bounds.width &&
event.clientY >= bounds.y &&
event.clientY <= bounds.y + bounds.height
) {
this.focus();
}
this.scrubDistance = 0;
this.pointerDownTime = undefined;
break;
}
}

private handleKeydown(event: KeyboardEvent): void {
if (this.isComposing) return;
switch (event.code) {
Expand Down Expand Up @@ -426,6 +523,9 @@ export class NumberField extends TextfieldBase {
}

protected override onFocus(): void {
if (this.pointerDragXLocation) {
return;
}
super.onFocus();
this._trackingValue = this.inputValue;
this.keyboardFocused = !this.readonly && true;
Expand Down Expand Up @@ -721,7 +821,10 @@ export class NumberField extends TextfieldBase {
@focusin=${this.handleFocusin}
@focusout=${this.handleFocusout}
${streamingListener({
start: ['pointerdown', this.handlePointerdown],
start: [
'pointerdown',
this.handleButtonPointerdown,
],
streamInside: [
[
'pointermove',
Expand All @@ -730,15 +833,15 @@ export class NumberField extends TextfieldBase {
'pointerover',
'pointerout',
],
this.handlePointermove,
this.handleButtonPointermove,
],
end: [
[
'pointerup',
'pointercancel',
'pointerleave',
],
this.handlePointerup,
this.handleButtonPointerup,
],
})}
>
Expand Down Expand Up @@ -814,6 +917,7 @@ export class NumberField extends TextfieldBase {
this.addEventListener('keydown', this.handleKeydown);
this.addEventListener('compositionstart', this.handleCompositionStart);
this.addEventListener('compositionend', this.handleCompositionEnd);
this.addEventListener('pointerdown', this.handlePointerdown);
}

protected override updated(changes: PropertyValues<this>): void {
Expand Down
93 changes: 93 additions & 0 deletions packages/number-field/src/number-field.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,99 @@ governing permissions and limitations under the License.
visibility: hidden;
}

:host(:not([focused], [disabled])) {
cursor: ew-resize;
}

:host(:not([focused], [disabled])) input {
cursor: ew-resize;
}

:host([hide-stepper]:not([quiet])) .input {
border-radius: var(
--spectrum-alias-border-radius-regular,
var(--spectrum-global-dimension-size-50)
);
}

:host([dir='ltr'][invalid]:not([hide-stepper])) .icon {
/* [dir=ltr] .spectrum-Textfield.is-invalid .spectrum-Textfield-validationIcon */
right: calc(
var(--spectrum-stepper-button-width) +
var(--spectrum-textfield-error-icon-margin-left)
);
}

:host([dir='rtl'][invalid]:not([hide-stepper])) .icon {
/* [dir=rtl] .spectrum-Textfield.is-invalid .spectrum-Textfield-validationIcon */
left: calc(
var(--spectrum-stepper-button-width) +
var(--spectrum-textfield-error-icon-margin-left)
);
}

:host([dir='ltr'][valid]:not([hide-stepper])) .icon {
/* [dir=ltr] .spectrum-Textfield.is-valid .spectrum-Textfield-validationIcon */
right: calc(
var(--spectrum-stepper-button-width) +
var(--spectrum-textfield-error-icon-margin-left)
);
}

:host([dir='rtl'][valid]:not([hide-stepper])) .icon {
/* [dir=rtl] .spectrum-Textfield.is-valid .spectrum-Textfield-validationIcon */
left: calc(
var(--spectrum-stepper-button-width) +
var(--spectrum-textfield-error-icon-margin-left)
);
}

:host([dir='ltr'][quiet][invalid]:not([hide-stepper])) .icon {
/* [dir=ltr] .spectrum-Textfield--quiet.spectrum-Textfield.is-invalid .spectrum-Textfield-validationIcon */
right: var(--spectrum-stepper-button-width);
}

:host([dir='rtl'][quiet][invalid]:not([hide-stepper])) .icon {
/* [dir=rtl] .spectrum-Textfield--quiet.spectrum-Textfield.is-invalid .spectrum-Textfield-validationIcon */
left: var(--spectrum-stepper-button-width);
}

:host([dir='ltr'][quiet][valid]:not([hide-stepper])) .icon {
/* [dir=ltr] .spectrum-Textfield--quiet.spectrum-Textfield.is-valid .spectrum-Textfield-validationIcon */
right: var(--spectrum-stepper-button-width);
}

:host([dir='rtl'][quiet][valid]:not([hide-stepper])) .icon {
/* [dir=rtl] .spectrum-Textfield--quiet.spectrum-Textfield.is-valid .spectrum-Textfield-validationIcon */
left: var(--spectrum-stepper-button-width);
}

:host([dir='ltr']:not([hide-stepper])) .icon-workflow {
/* [dir=ltr] .spectrum-Textfield-icon */
left: calc(
var(--spectrum-stepper-button-width) +
var(--spectrum-textfield-error-icon-margin-left)
);
}

:host([dir='rtl']:not([hide-stepper])) .icon-workflow {
/* [dir=rtl] .spectrum-Textfield-icon */
right: calc(
var(--spectrum-stepper-button-width) +
var(--spectrum-textfield-error-icon-margin-left)
);
}

:host([dir='ltr'][quiet]:not([hide-stepper])) .icon-workflow {
/* [dir=ltr] .spectrum-Textfield--quiet .spectrum-Textfield-icon */
left: var(--spectrum-stepper-button-width);
}

:host([dir='rtl'][quiet]:not([hide-stepper])) .icon-workflow {
/* [dir=rtl] .spectrum-Textfield--quiet .spectrum-Textfield-icon */
right: var(--spectrum-stepper-button-width);
}

:host([readonly]:not([disabled], [invalid], [focused], [keyboard-focused]))
#textfield:hover
.input {
Expand Down
39 changes: 27 additions & 12 deletions packages/number-field/test/number-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,12 @@ describe('NumberField', () => {
clientX: stepUpRect.x + 1,
clientY: stepUpRect.y + 1,
};
el.setPointerCapture = () => {
return;
};
el.releasePointerCapture = () => {
return;
};
(
el as unknown as {
buttons: HTMLDivElement;
Expand Down Expand Up @@ -724,7 +730,7 @@ describe('NumberField', () => {
expect(inputSpy.callCount).to.equal(5);
expect(changeSpy.callCount).to.equal(1);
});
it('no change in committed value - using buttons', async () => {
it('no change in committed value - using buttons', async function () {
const buttonUp = el.shadowRoot.querySelector(
'.step-up'
) as HTMLElement;
Expand All @@ -741,6 +747,7 @@ describe('NumberField', () => {
buttonDownRect.x + buttonDownRect.width / 2,
buttonDownRect.y + buttonDownRect.height / 2,
];
const initialVaue = el.value;
sendMouse({
steps: [
{
Expand All @@ -754,27 +761,32 @@ describe('NumberField', () => {
});
await oneEvent(el, 'input');
expect(el.value).to.equal(51);
expect(inputSpy.callCount).to.equal(1);
expect(inputSpy.callCount).to.equal(
Math.abs(el.value - initialVaue)
);
expect(changeSpy.callCount).to.equal(0);
await oneEvent(el, 'input');
expect(el.value).to.equal(52);
expect(inputSpy.callCount).to.equal(2);
expect(inputSpy.callCount).to.equal(
Math.abs(el.value - initialVaue)
);
expect(changeSpy.callCount).to.equal(0);
sendMouse({
const inputs = inputSpy.callCount;
const intermediateValue = el.value;
await sendMouse({
steps: [
{
type: 'move',
position: buttonDownPosition,
},
],
});
let framesToWait = FRAMES_PER_CHANGE * 2;
while (framesToWait) {
// input is only processed onces per FRAMES_PER_CHANGE number of frames
framesToWait -= 1;
await nextFrame();
while (el.value > 50) {
await oneEvent(el, 'input');
}
expect(inputSpy.callCount).to.equal(4);
expect(inputSpy.callCount).to.equal(
inputs + Math.abs(el.value - intermediateValue)
);
expect(changeSpy.callCount).to.equal(0);
await sendMouse({
steps: [
Expand All @@ -783,14 +795,17 @@ describe('NumberField', () => {
},
],
});
expect(inputSpy.callCount).to.equal(4);
expect(el.value).to.equal(50);
expect(inputSpy.callCount).to.equal(
inputs + Math.abs(el.value - intermediateValue)
);
expect(
changeSpy.callCount,
'value does not change from initial value so no "change" event is dispatched'
).to.equal(0);
});
});
it('accepts pointer interactions with the stepper UI', async () => {
it('accepts pointer interactions with the stepper UI', async function () {
const inputSpy = spy();
const el = await getElFrom(Default({ value: 50 }));
el.addEventListener('input', () => inputSpy());
Expand Down
Loading