Skip to content

Commit 8327f0c

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

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

Diff for: 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, domProps: {printer: () => console.log('domProps being used')}}, 'Reset'),
2629
]),
2730
);
2831

Diff for: src/Incorporator.ts

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ export default class Incorporator extends PureComponent<Props, State> {
2828
});
2929
}
3030

31+
public componentDidUpdate(prevProps, prevState) {
32+
const x = this
33+
const {domProps} = this.props.targetProps
34+
if (domProps && this.props.targetRef) {
35+
Object.entries(domProps)
36+
.forEach(([key, val]) => {
37+
this.props.targetRef.current[key] = val
38+
})
39+
}
40+
}
41+
3142
private incorporateHandlers<P>(props: P, scope: Scope): P {
3243
const handlers = scope.getSelectorHandlers(this.selector);
3344
for (const evType of Object.keys(handlers)) {
@@ -45,6 +56,7 @@ export default class Incorporator extends PureComponent<Props, State> {
4556
output.ref = targetRef;
4657
}
4758
delete output.sel;
59+
delete output.domProps
4860
return output;
4961
}
5062

Diff for: 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
);

Diff for: test/conversion.ts

+43
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,47 @@ describe('Conversion', function() {
229229
setTimeout(check, 150);
230230
setTimeout(check, 200);
231231
});
232+
233+
it('applies domProps to element when given', done => {
234+
function main(sources: {react: ReactSource}) {
235+
const inc$ = xs.create({
236+
start(listener: any) {
237+
setTimeout(() => {
238+
sources.react
239+
.select('button')
240+
.events('press')
241+
.addListener(listener);
242+
}, 30);
243+
},
244+
stop() {},
245+
});
246+
const count$ = inc$.fold((acc: number, x: any) => acc + x.target.blah, 0);
247+
const vdom$ = count$.map((i: number) =>
248+
h(Touchable, {sel: 'button', domProps: {blah: 3}}, [h('div', [h('h1', {}, '' + i)])]),
249+
);
250+
return {react: vdom$};
251+
}
252+
253+
let turn = 0;
254+
const RootComponent = makeCycleReactComponent(() => {
255+
const source = new ReactSource();
256+
const sink = main({react: source}).react;
257+
return {source, sink};
258+
});
259+
const r = renderer.create(createElement(RootComponent));
260+
const root = r.root;
261+
const check = () => {
262+
const to = root.findByType(Touchable);
263+
const view = to.props.children;
264+
const text = view.props.children;
265+
assert.strictEqual(text.props.children, `${turn * 3}`);
266+
to.instance.press();
267+
turn++;
268+
if (turn === 1) {
269+
done();
270+
}
271+
};
272+
setTimeout(check, 100);
273+
});
274+
232275
});

0 commit comments

Comments
 (0)