Skip to content

Commit

Permalink
fix(slider): update "value" default to match browser native range input
Browse files Browse the repository at this point in the history
BREAKING CHANGE: When not supplied value will now be set to halfway between min and max.
  • Loading branch information
Westbrook committed Feb 12, 2022
1 parent 226c70e commit 9f77111
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
16 changes: 15 additions & 1 deletion packages/slider/src/SliderHandle.ts
Expand Up @@ -88,8 +88,12 @@ export class SliderHandle extends Focusable {

_forcedUnit = '';

/**
* By default, the value of a Slider Handle will be halfway between its
* `min` and `max` values, or the `min` value when `max` is less than `min`.
*/
@property({ type: Number })
value = 10;
value!: number;

@property({ type: Boolean, reflect: true })
public dragging = false;
Expand Down Expand Up @@ -134,6 +138,16 @@ export class SliderHandle extends Focusable {
super.update(changes);
}

protected firstUpdated(changedProperties: PropertyValues<this>): void {
super.firstUpdated(changedProperties);
const { max, min } = this as { max: number; min: number };
if (this.value == null) {
if (!isNaN(max) && !isNaN(min)) {
this.value = max < min ? min : min + (max - min) / 2;
}
}
}

protected updated(changedProperties: PropertyValues<this>): void {
if (changedProperties.has('value')) {
const oldValue = changedProperties.get('value');
Expand Down
26 changes: 13 additions & 13 deletions packages/slider/test/slider.test.ts
Expand Up @@ -99,7 +99,7 @@ describe('Slider', () => {

await elementUpdated(el);

expect(el.value).to.equal(10);
expect(el.value).to.equal(46);
expect(el.highlight).to.be.false;

el.focus();
Expand All @@ -108,14 +108,14 @@ describe('Slider', () => {
});
await elementUpdated(el);

expect(el.value).to.equal(9);
expect(el.value).to.equal(45);
expect(el.highlight).to.be.true;
await sendKeys({
press: 'ArrowUp',
});
await elementUpdated(el);

expect(el.value).to.equal(10);
expect(el.value).to.equal(46);
expect(el.highlight).to.be.true;
});
it('accepts pointer events', async () => {
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Slider', () => {

await elementUpdated(el);

expect(el.value).to.equal(10);
expect(el.value).to.equal(35);

const controls = el.shadowRoot.querySelector(
'#controls'
Expand All @@ -236,7 +236,7 @@ describe('Slider', () => {
await elementUpdated(el);

expect(pointerId).to.equal(-1);
expect(el.value).to.equal(10);
expect(el.value).to.equal(35);
expect(el.dragging, 'handle is not yet being dragged').to.be.false;

controls.dispatchEvent(
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('Slider', () => {

expect(el.dragging).to.be.false;
expect(pointerId).to.equal(-1);
expect(el.value).to.equal(10);
expect(el.value).to.equal(50);

const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
handle.setPointerCapture = (id: number) => (pointerId = id);
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('Slider', () => {
await elementUpdated(el);

expect(pointerId).to.equal(-1);
expect(el.value).to.equal(10);
expect(el.value).to.equal(50);
});
it('accepts pointermove events', async () => {
const el = await fixture<Slider>(
Expand All @@ -340,7 +340,7 @@ describe('Slider', () => {
);
await elementUpdated(el);

expect(el.value).to.equal(10);
expect(el.value).to.equal(50);

const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
await sendMouse({
Expand Down Expand Up @@ -380,7 +380,7 @@ describe('Slider', () => {
);
await elementUpdated(el);

expect(el.value, 'initial').to.equal(10);
expect(el.value, 'initial').to.equal(50);

const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
el.track.setPointerCapture = (id: number) => (pointerId = id);
Expand Down Expand Up @@ -561,7 +561,7 @@ describe('Slider', () => {

await elementUpdated(el);

expect(el.value).to.equal(10);
expect(el.value).to.equal(50);
expect(el.dragging).to.be.false;

const handle = el.shadowRoot.querySelector('.handle') as HTMLDivElement;
Expand All @@ -574,7 +574,7 @@ describe('Slider', () => {
);
await nextFrame();

expect(el.value).to.equal(10);
expect(el.value).to.equal(50);
});
it('responds to input events on the <input/> element', async () => {
const el = await fixture<Slider>(
Expand All @@ -585,7 +585,7 @@ describe('Slider', () => {

await elementUpdated(el);

expect(el.value).to.equal(10);
expect(el.value).to.equal(50);

const input = el.shadowRoot.querySelector('.input') as HTMLInputElement;

Expand Down Expand Up @@ -815,7 +815,7 @@ describe('Slider', () => {

await elementUpdated(el);

expect(el.value).to.equal(10);
expect(el.value).to.equal(50);

el.min = 0;
el.max = 200;
Expand Down

0 comments on commit 9f77111

Please sign in to comment.