element-vir 4.1.4 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -150,6 +150,33 @@ export const MySimpleWithPropsElement = defineFunctionalElement({
|
|
|
150
150
|
});
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
+
## Updating properties
|
|
154
|
+
|
|
155
|
+
Grab `setProps` from `renderCallback`'s parameters to update the values in `props`. This causes a re-render. (Note that this example also uses the `listen` directive to respond to click events.)
|
|
156
|
+
|
|
157
|
+
<!-- example-link: src/readme-examples/my-simple-with-set-props.element.ts -->
|
|
158
|
+
|
|
159
|
+
```TypeScript
|
|
160
|
+
import {defineFunctionalElement, html, listen} from 'element-vir';
|
|
161
|
+
|
|
162
|
+
export const MySimpleWithPropsElement = defineFunctionalElement({
|
|
163
|
+
tagName: 'my-simple-element-with-props',
|
|
164
|
+
props: {
|
|
165
|
+
currentUsername: 'dev',
|
|
166
|
+
currentEmail: undefined as string | undefined,
|
|
167
|
+
},
|
|
168
|
+
renderCallback: ({props, setProps}) => html`
|
|
169
|
+
<span
|
|
170
|
+
${listen('click', () => {
|
|
171
|
+
setProps({currentUsername: 'new name!'});
|
|
172
|
+
})}
|
|
173
|
+
>
|
|
174
|
+
Hello there ${props.currentUsername}!
|
|
175
|
+
</span>
|
|
176
|
+
`,
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
153
180
|
## Assigning to properties (inputs)
|
|
154
181
|
|
|
155
182
|
Use the `assign` directive to assign properties to child custom elements:
|
|
@@ -216,14 +243,14 @@ export const MyAppWithEventsElement = defineFunctionalElement({
|
|
|
216
243
|
props: {
|
|
217
244
|
myNumber: -1,
|
|
218
245
|
},
|
|
219
|
-
renderCallback: ({props}) => html`
|
|
246
|
+
renderCallback: ({props, setProps}) => html`
|
|
220
247
|
<h1>My App</h1>
|
|
221
248
|
<${MySimpleWithEventsElement}
|
|
222
249
|
${listen(MySimpleWithEventsElement.events.logoutClick, () => {
|
|
223
250
|
console.info('logout triggered');
|
|
224
251
|
})}
|
|
225
252
|
${listen(MySimpleWithEventsElement.events.randomNumber, (event) => {
|
|
226
|
-
|
|
253
|
+
setProps({myNumber: event.detail});
|
|
227
254
|
})}
|
|
228
255
|
>
|
|
229
256
|
</${MySimpleWithEventsElement}>
|
|
@@ -5,8 +5,10 @@ import { PropertyInitMapBase } from './element-properties';
|
|
|
5
5
|
import { FunctionalElementInstance } from './functional-element';
|
|
6
6
|
export declare type RenderCallback<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap> = (params: RenderParams<PropertyInitGeneric, EventsInitGeneric>) => TemplateResult;
|
|
7
7
|
export declare type InitCallback<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap> = (params: RenderParams<PropertyInitGeneric, EventsInitGeneric>) => void;
|
|
8
|
+
export declare type SetPropCallback<PropertyInitGeneric extends PropertyInitMapBase> = (props: Partial<PropertyInitGeneric>) => void;
|
|
8
9
|
export declare type RenderParams<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap> = {
|
|
9
|
-
props: PropertyInitGeneric
|
|
10
|
+
props: Readonly<PropertyInitGeneric>;
|
|
11
|
+
setProps: SetPropCallback<PropertyInitGeneric>;
|
|
10
12
|
events: EventDescriptorMap<EventsInitGeneric>;
|
|
11
13
|
host: FunctionalElementInstance<PropertyInitGeneric>;
|
|
12
14
|
dispatch: <EventTypeNameGeneric extends keyof EventsInitGeneric>(event: TypedEvent<EventTypeNameGeneric extends string ? EventTypeNameGeneric : never, EventInitMapEventDetailExtractor<EventTypeNameGeneric, EventsInitGeneric>>) => boolean;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getObjectTypedKeys } from 'augment-vir';
|
|
1
2
|
export function createRenderParams(element, eventsMap) {
|
|
2
3
|
const renderParams = {
|
|
3
4
|
/**
|
|
@@ -6,8 +7,13 @@ export function createRenderParams(element, eventsMap) {
|
|
|
6
7
|
*/
|
|
7
8
|
dispatch: (event) => element.dispatchEvent(event),
|
|
8
9
|
genericDispatch: (event) => element.dispatchEvent(event),
|
|
10
|
+
setProps: (partialProps) => {
|
|
11
|
+
getObjectTypedKeys(partialProps).forEach((propKey) => {
|
|
12
|
+
element.instanceProps[propKey] = partialProps[propKey];
|
|
13
|
+
});
|
|
14
|
+
},
|
|
9
15
|
host: element,
|
|
10
|
-
props: element.instanceProps,
|
|
16
|
+
props: { ...element.instanceProps },
|
|
11
17
|
events: eventsMap,
|
|
12
18
|
};
|
|
13
19
|
return renderParams;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { collapseWhiteSpace, safeMatch } from 'augment-vir';
|
|
2
2
|
import { functionalElementRequired } from '../../require-functional-element';
|
|
3
3
|
import { hasStaticTagName } from '../has-static-tag-name';
|
|
4
4
|
import { makeCheckTransform, transformTemplate, } from '../transform-template';
|
|
@@ -23,7 +23,7 @@ const htmlChecksAndTransforms = [
|
|
|
23
23
|
function extractCustomElementTags(input) {
|
|
24
24
|
const tagNameMatches = safeMatch(input, /<\/[\s\n]*[^\s\n><]+[\s\n]*>/g);
|
|
25
25
|
return tagNameMatches.reduce((accum, match) => {
|
|
26
|
-
const tagName =
|
|
26
|
+
const tagName = collapseWhiteSpace(match.replace(/\n/g, ' ')).replace(/<\/|>/g, '');
|
|
27
27
|
// custom elements always have a dash in them
|
|
28
28
|
if (tagName.includes('-')) {
|
|
29
29
|
return accum.concat(tagName);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "element-vir",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"custom",
|
|
6
6
|
"web",
|
|
@@ -31,22 +31,18 @@
|
|
|
31
31
|
"prepublishOnly": "npm run compile && npm run test:full",
|
|
32
32
|
"spellcheck": "virmator spellcheck",
|
|
33
33
|
"start": "npm install && vite --force --config ./src/vite/vite.config.ts",
|
|
34
|
-
"test": "npm run type-check &&
|
|
34
|
+
"test": "npm run type-check && virmator test --no-write-config",
|
|
35
35
|
"test:full": "npm test && npm run spellcheck && virmator format check && npm run update-docs -- --check",
|
|
36
36
|
"type-check": "tsc --noEmit",
|
|
37
37
|
"update-docs": "virmator code-in-markdown README.md --index src/index.ts"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"augment-vir": "
|
|
41
|
-
"lit": "
|
|
40
|
+
"augment-vir": "2.1.0",
|
|
41
|
+
"lit": "2.2.3"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"ts-node": "^10.6.0",
|
|
48
|
-
"typescript": "^4.6.2",
|
|
49
|
-
"virmator": "^1.4.4",
|
|
50
|
-
"vite": "^2.8.6"
|
|
44
|
+
"jest-environment-jsdom": "28.0.2",
|
|
45
|
+
"virmator": "2.0.7",
|
|
46
|
+
"vite": "2.9.8"
|
|
51
47
|
}
|
|
52
48
|
}
|