-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref GH-2
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "hz-pressible", | ||
"version": "0.0.1", | ||
"description": "A pressible component", | ||
"main": "index.js", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
A component that manages a pressed state for the children. | ||
|
||
```js | ||
|
||
const myPressible = ({pressed, setPress}) => | ||
<div | ||
onMouseDown={() => setPress(true)} | ||
onMouseUp={() => setPress(false)} | ||
style={{ | ||
backgroundColor: pressed ? 'pink' : 'gray', | ||
padding: 10, | ||
display: 'inline-block', | ||
}} | ||
> | ||
Press Me | ||
</div>; | ||
|
||
<Pressible render={myPressible} /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @flow | ||
import {Component} from 'react'; | ||
|
||
// eslint-disable-next-line no-duplicate-imports | ||
import type {Element} from 'react'; | ||
|
||
type RenderProps = {}; | ||
|
||
type Props = { | ||
/** | ||
* Something something | ||
* @param {[type]} props [description] | ||
* @return {[type]} [description] | ||
*/ | ||
render: (props: RenderProps) => Element<*>, | ||
/** | ||
* [value description] | ||
* @type {[type]} | ||
*/ | ||
onPress?: (value: boolean) => void, | ||
}; | ||
|
||
type State = { | ||
pressed: boolean, | ||
}; | ||
|
||
const intialState = { | ||
pressed: false, | ||
}; | ||
|
||
/** | ||
* This is a basic pressible component | ||
* @returns Component | ||
*/ | ||
class Pressible extends Component<Props, State> { | ||
state = {...intialState}; | ||
|
||
componentDidUpdate(prevProps: Props, prevState: State) { | ||
if ( | ||
typeof this.props.onPress === 'function' && | ||
prevState.pressed !== this.state.pressed | ||
) { | ||
this.props.onPress(this.state.pressed); | ||
} | ||
} | ||
|
||
handlePress = (pressed: boolean) => { | ||
this.setState((state: State): ?State => { | ||
if (pressed === state.pressed) return null; | ||
return {...state, pressed}; | ||
}); | ||
}; | ||
|
||
render() { | ||
return this.props.render({ | ||
...this.props, | ||
...this.state, | ||
setPress: this.handlePress, | ||
}); | ||
} | ||
} | ||
|
||
export default Pressible; |