Skip to content

Commit add78c4

Browse files
committedAug 3, 2016
Use children instead of component prop
Also, use a <button> by default instead of a <div> Fixes #1
1 parent 8311816 commit add78c4

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed
 

‎README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ class App extends React.Component {
5555
}
5656
```
5757

58-
By default, a `<PointTarget>` renders a `<div>`. Use the `<PointTarget component>` prop to make it render something else. All props are passed directly through to your component (besides `onClick`, `onTouchStart`, `onTouchMove`, `onTouchEnd`, and `onTouchCancel`, which `<PointTarget>` needs to overwrite in order to do its thing).
58+
By default, a `<PointTarget>` renders a `<button>`. Use the [children](https://facebook.github.io/react/tips/children-props-type.html) prop to make it render something else.
5959

60-
For example, to render a `<button>`:
60+
*Note:* The `onClick`, `onTouchStart`, `onTouchMove`, `onTouchEnd`, and `onTouchCancel` props will be overwritten because `<PointTarget>` needs them to do its thing).
61+
62+
For example, to render a `<div>`:
6163

6264
```js
6365
import React from 'react'
@@ -70,12 +72,12 @@ class App extends React.Component {
7072

7173
render() {
7274
return (
73-
<PointTarget component="button" onPoint={this.handlePoint}/>
75+
<PointTarget onPoint={this.handlePoint}>
76+
<div>Point here</div>
77+
</PointTarget>
7478
)
7579
}
7680
}
7781
```
7882

79-
You can also pass in custom component classes. That's it :)
80-
81-
Enjoy!
83+
That's it :) Enjoy!

‎modules/PointTarget.js

+10-22
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ const touchY = (event) =>
88

99
class PointTarget extends React.Component {
1010
static propTypes = {
11-
component: PropTypes.oneOfType([
12-
PropTypes.func,
13-
PropTypes.string
14-
]).isRequired,
11+
children: PropTypes.node,
1512
tolerance: PropTypes.number,
1613
onPoint: PropTypes.func
1714
}
1815

1916
static defaultProps = {
20-
component: 'div',
17+
children: React.createElement('button'),
2118
tolerance: 10
2219
}
2320

@@ -65,24 +62,15 @@ class PointTarget extends React.Component {
6562
}
6663

6764
render() {
68-
const { component, ...props } = this.props
65+
const element = React.Children.only(this.props.children)
6966

70-
// Let React setup event handlers for us.
71-
// TODO: Warn if they try to pass these props in?
72-
props.onClick = this.handleClick
73-
props.onTouchStart = this.handleTouchStart
74-
props.onTouchMove = this.handleTouchMove
75-
props.onTouchCancel = this.handleTouchCancel
76-
props.onTouchEnd = this.handleTouchEnd
77-
78-
// Avoid unknown props warning.
79-
delete props.onPoint
80-
delete props.tolerance
81-
82-
return React.createElement(
83-
component,
84-
props
85-
)
67+
return React.cloneElement(element, {
68+
onClick: this.handleClick,
69+
onTouchStart: this.handleTouchStart,
70+
onTouchMove: this.handleTouchMove,
71+
onTouchCancel: this.handleTouchCancel,
72+
onTouchEnd: this.handleTouchEnd
73+
})
8674
}
8775
}
8876

‎modules/__tests__/PointTarget-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ describe('A <PointTarget>', () => {
1515
node = document.createElement('div')
1616
})
1717

18+
describe('with no children', () => {
19+
it('renders a button', () => {
20+
render(<PointTarget/>, node, () => {
21+
expect(node.firstChild.tagName).toEqual('BUTTON')
22+
})
23+
})
24+
})
25+
26+
describe('with children', () => {
27+
it('renders them', () => {
28+
render(<PointTarget><div/></PointTarget>, node, () => {
29+
expect(node.firstChild.tagName).toEqual('DIV')
30+
})
31+
})
32+
})
33+
1834
describe('when it is clicked', () => {
1935
it('calls the onPoint callback', () => {
2036
let called = false

1 commit comments

Comments
 (1)

ryanflorence commented on Aug 3, 2016

@ryanflorence
Member

👍

Please sign in to comment.