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

feat(atomic): add Lit equivalent for @MapProp decorator #4850

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
32 changes: 32 additions & 0 deletions packages/atomic/src/utils/props-utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
import {isArray} from '@coveo/bueno';
import {ComponentInterface, getElement} from '@stencil/core';
import {ReactiveElement} from 'lit';
import {camelToKebab, kebabToCamel} from './utils';

interface MapPropOptions {
attributePrefix?: string;
splitValues?: boolean;
}

export function mapProperty<Element extends ReactiveElement>(
options?: MapPropOptions
) {
return <
Instance extends Element & Record<string, unknown>,
K extends keyof Instance,
>(
proto: ReactiveElement,
propertyKey: K
) => {
const ctor = proto.constructor as typeof ReactiveElement;
ctor.addInitializer((instance) => {
const props = {};
const prefix =
options?.attributePrefix || camelToKebab(propertyKey as string);

mapAttributesToProp(
prefix,
props,
Array.from(instance.attributes),
options?.splitValues ?? false
);

(instance as Instance)[propertyKey] = props as Instance[K];
});
};
}

/**
* @deprecated Use the `mapProperty` decorator instead.
*/
export function MapProp(opts?: MapPropOptions) {
return (component: ComponentInterface, variableName: string) => {
const {componentWillLoad} = component;
Expand Down
Loading