Skip to content

Commit e839997

Browse files
committed
feat: add support for adding dom element properties similar to @cycle/dom
1 parent 2205231 commit e839997

File tree

7 files changed

+51
-12
lines changed

7 files changed

+51
-12
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
/lib
55
/dist
66
/.cache
7+
fixtures
8+
support
9+
plugins
710
node_modules
811
package-lock.json
912
pnpm-lock.yaml

cypress.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
22
"baseUrl": "http://localhost:1234",
3-
"video": false
3+
"chromeWebSecurity": false,
4+
"defaultCommandTimeout": 10000,
5+
"modifyObstructiveCode": false,
6+
"video": false,
7+
"fixturesFolder": false
48
}

cypress/integration/test.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
/// <reference types="cypress" />
22

3+
const { watchFile } = require("fs")
4+
35
context('Page load', () => {
46
beforeEach(() => {
57
cy.visit('/')
8+
cy.wait(500)
69
})
710
describe('React integration', () => {
11+
812
it('Should mount', () => {
913
cy.get('#app')
1014
.should('exist', 'success')
1115
})
16+
it('Should have foo property on button', () => {
17+
cy.get('.clicker')
18+
// .its('foo')
19+
// .should('eq', 3)
20+
.then(($el) => {
21+
cy.wrap($el[0].foo).should('eq', 3)
22+
})
23+
})
1224
})
1325
})

example/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function main(sources) {
1313
const reset$ = sources.react
1414
.select(btnSel)
1515
.events('click')
16+
.debug((ev) => {
17+
return ev.target.printer()
18+
})
1619
.mapTo(() => 0);
1720

1821
const count$ = xs
@@ -22,7 +25,7 @@ function main(sources) {
2225
const vdom$ = count$.map(i =>
2326
h('div', [
2427
h('h1', `Hello ${i} times`),
25-
h('button', {sel: btnSel}, 'Reset'),
28+
h('button', {sel: btnSel, className: 'clicker', domProps: {foo: 3, printer: () => console.log('domProps being used')}}, 'Reset'),
2629
]),
2730
);
2831

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@
4949
"compile": "npm run compile-cjs && npm run compile-es6",
5050
"compile-cjs": "tsc --module commonjs --outDir ./lib/cjs",
5151
"compile-es6": "echo 'TODO' : tsc --module es6 --outDir ./lib/es6",
52-
"test": "$(npm bin)/mocha test/*.ts --require ts-node/register --recursive; cypress run",
52+
"test": "$(npm bin)/mocha test/*.ts --require ts-node/register --recursive",
53+
"full-test": "npm test; npm run cypress:run",
5354
"start": "parcel example/index.html",
54-
"serve-test": "start-server-and-test start http://localhost:1234 test"
55+
"serve-test": "start-server-and-test start http://localhost:1234 full-test",
56+
"cypress:open": "cypress open",
57+
"cypress:run": "cypress run"
5558
}
5659
}

src/Incorporator.ts

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export default class Incorporator extends PureComponent<Props, State> {
2626
this.unsubscribe = this.props.scope.subscribe(this.selector, () => {
2727
this.setState((prev: any) => ({flip: !prev.flip}));
2828
});
29+
30+
const {domProps} = this.props.targetProps
31+
if (domProps && this.props.targetRef) {
32+
Object.entries(domProps)
33+
.forEach(([key, val]) => {
34+
this.props.targetRef.current[key] = val
35+
})
36+
}
2937
}
3038

3139
private incorporateHandlers<P>(props: P, scope: Scope): P {
@@ -45,6 +53,7 @@ export default class Incorporator extends PureComponent<Props, State> {
4553
output.ref = targetRef;
4654
}
4755
delete output.sel;
56+
delete output.domProps
4857
return output;
4958
}
5059

src/incorporate.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createElement, forwardRef} from 'react';
1+
import {createElement, forwardRef, createRef} from 'react';
22
import {Scope} from './scope';
33
import {ScopeContext} from './context';
44
import Incorporator from './Incorporator';
@@ -10,13 +10,18 @@ export function incorporate(type: any) {
1010
wrapperComponents.set(
1111
type,
1212
forwardRef<any, any>((props, ref) =>
13-
createElement(ScopeContext.Consumer, null, (scope: Scope) =>
14-
createElement(Incorporator, {
15-
targetProps: props,
16-
targetRef: ref,
17-
target: type,
18-
scope: scope,
19-
}),
13+
createElement(ScopeContext.Consumer, null, (scope: Scope) => {
14+
let targetRef = ref
15+
if (!ref && props.domProps) {
16+
targetRef = createRef()
17+
}
18+
return createElement(Incorporator, {
19+
targetProps: props,
20+
targetRef,
21+
target: type,
22+
scope: scope,
23+
})
24+
}
2025
),
2126
),
2227
);

0 commit comments

Comments
 (0)