Skip to content

Commit

Permalink
feat(atomic): add Lit equivalent for @MapProp decorator (#4850)
Browse files Browse the repository at this point in the history
## With Stencil
```.ts
@MapProp() @prop() public dependsOn: Record<string, string> = {};
```

## With Lit
```.ts
@MapProperty() @Property() public dependsOn: Record<string, string> = {};
```

The new Lit decorator supports the same options (`splitValues`,
`attributePrefix`)


https://coveord.atlassian.net/browse/KIT-3830

---------

Co-authored-by: Louis Bompart <[email protected]>
Co-authored-by: renovate-coveo[bot] <115253437+renovate-coveo[bot]@users.noreply.github.com>
Co-authored-by: GitHub Actions Bot <>
Co-authored-by: Frederic Beaudoin <[email protected]>
  • Loading branch information
4 people authored Jan 20, 2025
1 parent 0cfeec7 commit 5d907b1
Showing 1 changed file with 32 additions and 0 deletions.
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.toString());

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

0 comments on commit 5d907b1

Please sign in to comment.