From 5a23dff80011d8eef25ebbd8063b6237a6740377 Mon Sep 17 00:00:00 2001 From: Matt Pharoah Date: Mon, 11 Mar 2024 16:11:28 -0400 Subject: [PATCH] Added form integration --- .../multi-range/multi-range.component.ts | 76 ++++++++++++++++--- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/src/components/multi-range/multi-range.component.ts b/src/components/multi-range/multi-range.component.ts index 95135660..08dfe84a 100644 --- a/src/components/multi-range/multi-range.component.ts +++ b/src/components/multi-range/multi-range.component.ts @@ -1,12 +1,16 @@ import { classMap } from 'lit/directives/class-map.js'; +import { defaultValue } from '../../internal/default-value.js'; +import { FormControlController } from '../../internal/form.js'; import { HasSlotController } from '../../internal/slot.js'; import { html, nothing } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { property, query, queryAll } from 'lit/decorators.js'; +import componentStyles from '../../styles/component.styles.js'; import formControlStyles from '../../styles/form-control.styles.js'; import ShoelaceElement from '../../internal/shoelace-element.js'; import styles from './multi-range.styles.js'; import type { CSSResultGroup, PropertyValues } from 'lit'; +import type { ShoelaceFormControl } from '../../internal/shoelace-element.js'; const numericSort = function (a: number, b: number): number { return a - b; @@ -42,8 +46,11 @@ const arraysDiffer = function (a: readonly number[], b: readonly number[]): bool * @cssproperty --track-color-inactive - The of the portion of the track that represents the remaining value. * @cssproperty --track-height - The height of the track. */ -export default class SlMultiRange extends ShoelaceElement { - static styles: CSSResultGroup = [formControlStyles, styles]; +export default class SlMultiRange extends ShoelaceElement implements ShoelaceFormControl { + static styles: CSSResultGroup = [componentStyles, formControlStyles, styles]; + + /** The name of the range, submitted as a name/value pair with form data. */ + @property() name = ''; /** The range's label. If you need to display HTML, use the `label` slot instead. */ @property() label = ''; @@ -84,6 +91,9 @@ export default class SlMultiRange extends ShoelaceElement { return this.#value; } + /** The default value of the form control. Primarily used for resetting the form control. */ + @defaultValue() defaultValue = '0,100'; + /** * A function used to format the tooltip's value. The range's value is passed as the first and only argument. The * function should return a string to display in the tooltip. @@ -96,10 +106,12 @@ export default class SlMultiRange extends ShoelaceElement { @queryAll('.handle') handles: NodeListOf; #hasSlotController = new HasSlotController(this, 'help-text', 'label'); + #formControlController = new FormControlController(this, { assumeInteractionOn: ['sl-change'] }); #resizeObserver: ResizeObserver | null = null; #value: readonly number[] = [0, 100]; #sliderValues = new Map(); #hasFocus = false; + #validationError = ''; #nextId = 1; override render(): unknown { @@ -143,7 +155,6 @@ export default class SlMultiRange extends ShoelaceElement { 'tooltip-top': this.tooltip === 'top', 'tooltip-bottom': this.tooltip === 'bottom' })} - @focusin=${this.#onFocus} @focusout=${this.#onBlur} >