GeezerVison is showing github.com — :sparkles: bind noop and inner references · nullstack/nullstack@edfb535
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: client/events.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import router from './router' | 2 | 2 | import { camelize } from '../shared/string'; | | 3 | +import noop from '../shared/noop' | 3 | 4 |
| 4 | 5 | export const eventCallbacks = new WeakMap() | 5 | 6 | export const eventSubjects = new WeakMap() | @@ -58,7 +59,7 @@ export function generateCallback(selector, name) { | 58 | 59 | object[property] = event.target[valueName]; | 59 | 60 | } | 60 | 61 | } | 61 | | - if (subject[name] === true) return | | 62 | + if (subject[name] === noop) return | 62 | 63 | if (Array.isArray(subject[name])) { | 63 | 64 | for (const subcallback of subject[name]) { | 64 | 65 | executeEvent(subcallback, subject, event, data) | |
Collapse file Copy file name to clipboardExpand all lines: loaders/register-inner-components.js +17-15Lines changed: 17 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,23 +11,25 @@ module.exports = function (source) { | 11 | 11 | traverse(ast, { | 12 | 12 | ClassMethod(path) { | 13 | 13 | if (path.node.key.name.startsWith('render')) { | 14 | | - traverse(path.node, { | 15 | | - JSXIdentifier(subpath) { | 16 | | - if (/^[A-Z]/.test(subpath.node.name)) { | 17 | | - if (!path.scope.hasBinding(subpath.node.name)) { | 18 | | - const start = path.node.body.body[0].start; | 19 | | - if (!positions.includes(start)) { | 20 | | - positions.push(start); | 21 | | - } | 22 | | - if (!injections[start]) { | 23 | | - injections[start] = []; | 24 | | - } | 25 | | - if (!injections[start].includes(subpath.node.name)) { | 26 | | - injections[start].push(subpath.node.name); | 27 | | - } | | 14 | + function identify(subpath) { | | 15 | + if (/^[A-Z]/.test(subpath.node.name)) { | | 16 | + if (!path.scope.hasBinding(subpath.node.name)) { | | 17 | + const start = path.node.body.body[0].start; | | 18 | + if (!positions.includes(start)) { | | 19 | + positions.push(start); | | 20 | + } | | 21 | + if (!injections[start]) { | | 22 | + injections[start] = []; | | 23 | + } | | 24 | + if (!injections[start].includes(subpath.node.name)) { | | 25 | + injections[start].push(subpath.node.name); | 28 | 26 | } | 29 | 27 | } | 30 | | - }, | | 28 | + } | | 29 | + } | | 30 | + traverse(path.node, { | | 31 | + JSXIdentifier: identify, | | 32 | + Identifier: identify, | 31 | 33 | }, path.scope, path); | 32 | 34 | } | 33 | 35 | } | |
Collapse file Copy file name to clipboardExpand all lines: plugins/anchorable.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import noop from '../shared/noop' | | 2 | + | 1 | 3 | function match(node) { | 2 | 4 | return ( | 3 | 5 | node && | @@ -10,7 +12,7 @@ function match(node) { | 10 | 12 |
| 11 | 13 | function transform({ node }) { | 12 | 14 | if (!match(node)) return | 13 | | - node.attributes.onclick ??= true | | 15 | + node.attributes.onclick ??= noop | 14 | 16 | } | 15 | 17 |
| 16 | 18 | export default { transform, client: true } |
Collapse file Copy file name to clipboardExpand all lines: plugins/bindable.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import noop from '../shared/noop' | | 2 | + | 1 | 3 | function match(node) { | 2 | 4 | return node?.attributes?.bind !== undefined | 3 | 5 | } | @@ -16,11 +18,11 @@ function transform({ node, environment }) { | 16 | 18 | node.attributes.name = node.attributes.name || node.attributes.bind; | 17 | 19 | if (environment.client) { | 18 | 20 | if (node.attributes.type === 'checkbox' || node.attributes.type === 'radio') { | 19 | | - node.attributes.onclick ??= true; | | 21 | + node.attributes.onclick ??= noop; | 20 | 22 | } else if (node.type !== 'input' && node.type !== 'textarea') { | 21 | | - node.attributes.onchange ??= true; | | 23 | + node.attributes.onchange ??= noop; | 22 | 24 | } else { | 23 | | - node.attributes.oninput ??= true; | | 25 | + node.attributes.oninput ??= noop; | 24 | 26 | } | 25 | 27 | } | 26 | 28 | } | |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export default function noop() { } |
Collapse file Copy file name to clipboardExpand all lines: tests/src/Element.njs +27-2Lines changed: 27 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import Nullstack from 'nullstack'; | 2 | 2 |
| | 3 | +class ClassElement extends Nullstack { | | 4 | + | | 5 | + render({ prop }) { | | 6 | + return ( | | 7 | + <div data-class-component={prop} /> | | 8 | + ) | | 9 | + } | | 10 | + | | 11 | +} | | 12 | + | | 13 | +function FunctionalElement({ prop }) { | | 14 | + return ( | | 15 | + <div data-functional-component={prop} /> | | 16 | + ) | | 17 | +} | | 18 | + | 3 | 19 | class Element extends Nullstack { | 4 | | - | | 20 | + | | 21 | + renderInnerElement({ prop }) { | | 22 | + return ( | | 23 | + <div data-inner-component={prop} /> | | 24 | + ) | | 25 | + } | | 26 | + | 5 | 27 | render() { | 6 | 28 | return ( | 7 | | - <> | | 29 | + <> | 8 | 30 | <element tag="b" data-tag="b">b</element> | 9 | 31 | <element data-tag="fragment"> | 10 | 32 | <element tag="abbr" data-tag="abbr">abbr</element> | 11 | 33 | </element> | | 34 | + <element tag={InnerElement} prop /> | | 35 | + <element tag={ClassElement} prop /> | | 36 | + <element tag={FunctionalElement} prop /> | 12 | 37 | </> | 13 | 38 | ) | 14 | 39 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/src/Element.test.js +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,4 +19,20 @@ describe('FullStackLifecycle', () => { | 19 | 19 | expect(element).toBeFalsy(); | 20 | 20 | }); | 21 | 21 |
| | 22 | + test('elements can be inner components and receive props', async () => { | | 23 | + const element = await page.$('[data-inner-component]'); | | 24 | + expect(element).toBeTruthy(); | | 25 | + }); | | 26 | + | | 27 | + test('elements can be class components and receive props', async () => { | | 28 | + const element = await page.$('[data-class-component]'); | | 29 | + expect(element).toBeTruthy(); | | 30 | + }); | | 31 | + | | 32 | + test('elements can be functional components and receive props', async () => { | | 33 | + const element = await page.$('[data-functional-component]'); | | 34 | + expect(element).toBeTruthy(); | | 35 | + }); | | 36 | + | | 37 | + | 22 | 38 | }); |
Collapse file Copy file name to clipboardExpand all lines: tests/src/RenderableComponent.njs +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,16 +6,23 @@ class RenderableComponent extends Nullstack { | 6 | 6 | return <div data-nested /> | 7 | 7 | } | 8 | 8 |
| 9 | | - renderInnerComponent({ children }) { | | 9 | + renderInnerComponent({ children, reference: Reference }) { | 10 | 10 | return ( | 11 | 11 | <div class="InnerComponent"> | 12 | 12 | <p> Inner Component </p> | 13 | 13 | <NestedInnerComponent /> | | 14 | + <Reference prop /> | 14 | 15 | {children} | 15 | 16 | </div> | 16 | 17 | ) | 17 | 18 | } | 18 | 19 |
| | 20 | + renderInnerReference({ prop }) { | | 21 | + return ( | | 22 | + <div data-reference={prop} /> | | 23 | + ) | | 24 | + } | | 25 | + | 19 | 26 | renderFalsy() { | 20 | 27 | return false; | 21 | 28 | } | @@ -37,7 +44,7 @@ class RenderableComponent extends Nullstack { | 37 | 44 | <element class="element" tag={params.condition ? 'div' : 'span'}> | 38 | 45 | element tag | 39 | 46 | </element> | 40 | | - <InnerComponent> | | 47 | + <InnerComponent reference={InnerReference}> | 41 | 48 | <span class="children"> children </span> | 42 | 49 | </InnerComponent> | 43 | 50 | <ul> | |
Collapse file Copy file name to clipboardExpand all lines: tests/src/RenderableComponent.test.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -84,6 +84,11 @@ describe('RenderableComponent', () => { | 84 | 84 | expect(element).toBeFalsy(); | 85 | 85 | }); | 86 | 86 |
| | 87 | + test('inner components can be referenced and receive props', async () => { | | 88 | + const element = await page.$('[data-reference]'); | | 89 | + expect(element).toBeTruthy(); | | 90 | + }); | | 91 | + | 87 | 92 | }); | 88 | 93 |
| 89 | 94 | describe('RenderableComponent ?condition=true', () => { | |
Collapse file Copy file name to clipboardExpand all lines: tests/src/TwoWayBindings.test.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -205,4 +205,10 @@ describe('TwoWayBindings', () => { | 205 | 205 | expect(element).toBeTruthy(); | 206 | 206 | }); | 207 | 207 |
| | 208 | + test('custom bindable components receive a no op function if no onchange is passed to them', async () => { | | 209 | + await page.waitForSelector('[data-onchange="noop"]') | | 210 | + const element = await page.$('[data-onchange="noop"]'); | | 211 | + expect(element).toBeTruthy(); | | 212 | + }); | | 213 | + | 208 | 214 | }); |
|