Skip to content

Commit

Permalink
fix: ensure reactivity of resolved language
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed Nov 14, 2022
1 parent df65e7b commit 5863a15
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
21 changes: 18 additions & 3 deletions packages/number-field/src/NumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
property,
query,
} from '@spectrum-web-components/base/src/decorators.js';
import { LanguageResolutionController } from '@spectrum-web-components/reactive-controllers/src/LanguageResolution.js';
import {
LanguageResolutionController,
languageResolverUpdatedSymbol,
} from '@spectrum-web-components/reactive-controllers/src/LanguageResolution.js';
import { streamingListener } from '@spectrum-web-components/base/src/streaming-listener.js';
import { NumberFormatter, NumberParser } from '@internationalized/number';

Expand Down Expand Up @@ -162,7 +165,16 @@ export class NumberField extends TextfieldBase {

private convertValueToNumber(value: string): number {
if (isIPhone() && this.inputElement.inputMode === 'decimal') {
value = value.replace(',', '.');
const parts = this.numberFormatter.formatToParts(1000.1);
const sourceDecimal = value
.split('')
.find((char) => char === ',' || char === '.');
const replacementDecimal = parts.find(
(part) => part.type === 'decimal'
)?.value;
if (sourceDecimal && replacementDecimal) {
value = value.replace(sourceDecimal, replacementDecimal);
}
}
return this.numberParser.parse(value);
}
Expand Down Expand Up @@ -580,8 +592,11 @@ export class NumberField extends TextfieldBase {
super.update(changes);
}

public override willUpdate(): void {
public override willUpdate(changes: PropertyValues): void {
this.multiline = false;
if (changes.has(languageResolverUpdatedSymbol)) {
this.clearNumberFormatterCache();
}
}

protected override firstUpdated(changes: PropertyValues): void {
Expand Down
16 changes: 12 additions & 4 deletions packages/slider/src/SliderHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ governing permissions and limitations under the License.
import { PropertyValues } from '@spectrum-web-components/base';
import { property } from '@spectrum-web-components/base/src/decorators.js';
import { Focusable } from '@spectrum-web-components/shared/src/focusable.js';
import { LanguageResolutionController } from '@spectrum-web-components/reactive-controllers/src/LanguageResolution.js';
import {
LanguageResolutionController,
languageResolverUpdatedSymbol,
} from '@spectrum-web-components/reactive-controllers/src/LanguageResolution.js';
import {
NumberFormatOptions,
NumberFormatter,
Expand Down Expand Up @@ -138,8 +141,11 @@ export class SliderHandle extends Focusable {
}
}
}

if (changes.has('formatOptions') || changes.has('resolvedLanguage')) {

if (
changes.has('formatOptions') ||
changes.has(languageResolverUpdatedSymbol)
) {
delete this._numberFormatCache;
}
if (changes.has('value')) {
Expand All @@ -154,7 +160,9 @@ export class SliderHandle extends Focusable {
super.update(changes);
}

protected override firstUpdated(changedProperties: PropertyValues<this>): void {
protected override firstUpdated(
changedProperties: PropertyValues<this>
): void {
super.firstUpdated(changedProperties);
this.dispatchEvent(new CustomEvent('sp-slider-handle-ready'));
}
Expand Down
22 changes: 22 additions & 0 deletions packages/slider/stories/slider.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ const editableDecorator = (story: () => TemplateResult): TemplateResult => {
`;
};

export const max20 = (args: StoryArgs = {}): TemplateResult => {
return html`
<div style="width: 200px; margin: 12px 20px;">
<sp-slider
editable
max="20"
min="0"
value="5"
step="1"
@input=${handleEvent(args)}
@change=${handleEvent(args)}
...=${spreadProps(args)}
>
Max 20
</sp-slider>
</div>
`;
};
max20.swc_vrt = {
skip: true,
};

export const editable = (args: StoryArgs = {}): TemplateResult => {
return html`
<div style="width: 500px; margin: 12px 20px;">
Expand Down
10 changes: 9 additions & 1 deletion tools/reactive-controllers/src/LanguageResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ governing permissions and limitations under the License.
import type { ReactiveController, ReactiveElement } from 'lit';
import { ProvideLang } from '@spectrum-web-components/theme';

export const languageResolverUpdatedSymbol = Symbol(
'language resolver updated'
);

export class LanguageResolutionController implements ReactiveController {
private host: ReactiveElement;
language = document.documentElement.lang || navigator.language;
Expand All @@ -39,9 +43,13 @@ export class LanguageResolutionController implements ReactiveController {
composed: true,
detail: {
callback: (lang: string, unsubscribe: () => void) => {
const previous = this.language;
this.language = lang;
this.unsubscribe = unsubscribe;
this.host.requestUpdate();
this.host.requestUpdate(
languageResolverUpdatedSymbol,
previous
);
},
},
cancelable: true,
Expand Down

0 comments on commit 5863a15

Please sign in to comment.