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

Bind this to custom attribute converter methods #3120

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-tables-own.md
@@ -0,0 +1,5 @@
---
'@lit/reactive-element': patch
---

Bind this to custom attribute converter methods
nebarf marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 16 additions & 12 deletions packages/reactive-element/src/reactive-element.ts
Expand Up @@ -1069,10 +1069,12 @@ export abstract class ReactiveElement
this.constructor as typeof ReactiveElement
).__attributeNameForProperty(name, options);
if (attr !== undefined && options.reflect === true) {
const toAttribute =
(options.converter as ComplexAttributeConverter)?.toAttribute ??
defaultConverter.toAttribute;
const attrValue = toAttribute!(value, options.type);
const converter =
(options.converter as ComplexAttributeConverter)?.toAttribute !==
undefined
? (options.converter as ComplexAttributeConverter)
: defaultConverter;
const attrValue = converter.toAttribute!(value, options.type);
if (
DEV_MODE &&
(this.constructor as typeof ReactiveElement).enabledWarnings!.indexOf(
Expand Down Expand Up @@ -1117,17 +1119,19 @@ export abstract class ReactiveElement
// if it was just set because the attribute changed.
if (propName !== undefined && this.__reflectingProperty !== propName) {
const options = ctor.getPropertyOptions(propName);
const converter = options.converter;
const fromAttribute =
(converter as ComplexAttributeConverter)?.fromAttribute ??
(typeof converter === 'function'
? (converter as (value: string | null, type?: unknown) => unknown)
: null) ??
defaultConverter.fromAttribute;
const converter =
typeof options.converter === 'function'
? {fromAttribute: options.converter}
: options.converter?.fromAttribute !== undefined
? options.converter
: defaultConverter;
// mark state reflecting
this.__reflectingProperty = propName;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this[propName as keyof this] = fromAttribute!(value, options.type) as any;
this[propName as keyof this] = converter.fromAttribute!(
value,
options.type
) as any;
nebarf marked this conversation as resolved.
Show resolved Hide resolved
// mark state not reflecting
this.__reflectingProperty = null;
}
Expand Down
73 changes: 73 additions & 0 deletions packages/reactive-element/src/test/reactive-element_test.ts
Expand Up @@ -302,6 +302,79 @@ suite('ReactiveElement', () => {
assert.equal(el.getAttribute('foo'), 'toAttribute: FooType');
});

test('property option `converter` can use a class instance', async () => {
class IntegerAttributeConverter
implements ComplexAttributeConverter<Number>
{
private _defaultValue: Number;

constructor(defaultValue: Number) {
this._defaultValue = defaultValue;
}

toAttribute(value: Number, _type?: unknown): unknown {
if (!value) {
return this._defaultValue;
}
return `${value}`;
}

fromAttribute(value: string | null, _type?: unknown): Number {
if (!value) {
return this._defaultValue;
}

const parsedValue = Number.parseInt(value, 10);
if (isNaN(parsedValue)) {
return this._defaultValue;
}
return parsedValue;
}
}

const defaultIntAttrConverterVal = 1;

class E extends ReactiveElement {
static override get properties() {
return {
num: {
type: Number,
converter: new IntegerAttributeConverter(
defaultIntAttrConverterVal
),
reflect: true,
},
};
}

num?: number;
}

customElements.define(generateElementName(), E);
const el = new E();
container.appendChild(el);
await el.updateComplete;

assert.equal(el.getAttribute('num'), null);
assert.equal(el.num, undefined);

el.setAttribute('num', 'notANumber');
await el.updateComplete;
assert.equal(el.num, defaultIntAttrConverterVal);

el.num = 10;
await el.updateComplete;
assert.equal(el.getAttribute('num'), '10');

el.setAttribute('num', '5');
await el.updateComplete;
assert.equal(el.num, 5);

el.num = undefined;
await el.updateComplete;
assert.equal(el.getAttribute('num'), `${defaultIntAttrConverterVal}`);
});

test('property/attribute values when attributes removed', async () => {
class E extends ReactiveElement {
static override get properties() {
Expand Down