Skip to content

Commit f5cfcee

Browse files
committed
fix: init of version 2.0
BREAKING CHANGE: The lib needs to be updated to use - the latest React and React Native - a new Context API - flow type - a new theming system (currently it doesn't respect the Pure Component idea) Some props will be changed. A new components will be added.
1 parent a8a932a commit f5cfcee

19 files changed

+907
-4
lines changed

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"semi": false,
23
"singleQuote": true,
34
"tabWidth": 2,
45
"trailingComma": "all"

src/components/Button/Button.js

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/* eslint-disable import/no-unresolved, import/extensions */
2+
import { View, Text, StyleSheet } from 'react-native'
3+
import React, { PureComponent } from 'react'
4+
import PropTypes from 'prop-types'
5+
import { ViewPropTypes } from '../utils'
6+
/* eslint-enable import/no-unresolved, import/extensions */
7+
import Icon from '../Icon'
8+
import RippleFeedback from '../RippleFeedback'
9+
/* eslint-disable import/no-unresolved, import/extensions */
10+
import getPlatformElevation from '../styles/getPlatformElevation'
11+
/* eslint-enable import/no-unresolved, import/extensions */
12+
import withTheme from '../styles/withTheme'
13+
14+
const propTypes = {
15+
testID: PropTypes.string,
16+
/**
17+
* If true button will be disabled
18+
*/
19+
disabled: PropTypes.bool,
20+
/**
21+
* If true button will be raised
22+
*/
23+
raised: PropTypes.bool,
24+
/**
25+
* Called when button is pressed. Text is passed as param
26+
*/
27+
onPress: PropTypes.func,
28+
/**
29+
* Called when button is long pressed. Text is passed as param
30+
*/
31+
onLongPress: PropTypes.func,
32+
/**
33+
* Text will be shown on button
34+
*/
35+
text: PropTypes.string.isRequired,
36+
/**
37+
* Button text will be in uppercase letters
38+
*/
39+
upperCase: PropTypes.bool,
40+
/**
41+
* If specified it'll be shown before text
42+
*/
43+
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
44+
/**
45+
* Name of Icon set that should be use. From react-native-vector-icons
46+
*/
47+
iconSet: PropTypes.string,
48+
/**
49+
* You can override any style for this button
50+
*/
51+
style: PropTypes.shape({
52+
container: ViewPropTypes.style,
53+
text: Text.propTypes.style, // eslint-disable-line
54+
}),
55+
primary: PropTypes.bool,
56+
accent: PropTypes.bool,
57+
}
58+
const defaultProps = {
59+
testID: null,
60+
icon: null,
61+
onPress: null,
62+
onLongPress: null,
63+
primary: false,
64+
accent: false,
65+
disabled: false,
66+
raised: false,
67+
upperCase: true,
68+
iconSet: null,
69+
style: {},
70+
}
71+
72+
function getStyles(props, state) {
73+
const { primary, accent, disabled, raised, theme } = props
74+
const {
75+
button,
76+
buttonFlat,
77+
buttonRaised,
78+
buttonDisabled,
79+
buttonRaisedDisabled,
80+
palette,
81+
} = theme
82+
83+
const local = {
84+
container: {},
85+
}
86+
87+
if (!disabled) {
88+
if (primary && !raised) {
89+
local.text = { color: palette.primaryColor }
90+
} else if (accent && !raised) {
91+
local.text = { color: palette.accentColor }
92+
}
93+
94+
if (primary && raised) {
95+
local.container.backgroundColor = palette.primaryColor
96+
local.text = { color: palette.canvasColor }
97+
} else if (accent && raised) {
98+
local.container.backgroundColor = palette.accentColor
99+
local.text = { color: palette.canvasColor }
100+
}
101+
}
102+
103+
if (raised && !disabled) {
104+
local.container = {
105+
...local.container,
106+
...getPlatformElevation(state.elevation),
107+
}
108+
}
109+
110+
return {
111+
container: [
112+
button.container,
113+
!raised && buttonFlat.container,
114+
raised && buttonRaised.container,
115+
!raised && disabled && buttonDisabled.container,
116+
raised && disabled && buttonRaisedDisabled.container,
117+
local.container,
118+
props.style.container,
119+
],
120+
text: [
121+
button.text,
122+
!raised && buttonFlat.text,
123+
raised && buttonRaised.text,
124+
!raised && disabled && buttonDisabled.text,
125+
raised && disabled && buttonRaisedDisabled.text,
126+
local.text,
127+
props.style.text,
128+
],
129+
icon: [
130+
button.icon,
131+
!raised && buttonFlat.icon,
132+
disabled && buttonDisabled.icon,
133+
raised && buttonRaised.icon,
134+
local.icon,
135+
props.style.icon,
136+
],
137+
}
138+
}
139+
140+
class Button extends PureComponent {
141+
constructor(props) {
142+
super(props)
143+
this.state = {
144+
elevation: 2, // eslint-disable-line
145+
}
146+
}
147+
148+
onPress = () => {
149+
const { text, onPress } = this.props
150+
151+
if (onPress) {
152+
onPress(text)
153+
}
154+
}
155+
156+
setElevation = () => {
157+
this.setState({
158+
elevation: 4, // eslint-disable-line
159+
})
160+
}
161+
162+
removeElevation = () => {
163+
this.setState({
164+
elevation: 2, // eslint-disable-line
165+
})
166+
}
167+
168+
renderIcon = styles => {
169+
const { icon, iconSet } = this.props
170+
const textFlatten = StyleSheet.flatten(styles.text)
171+
172+
if (!icon) {
173+
return null
174+
}
175+
176+
let result
177+
178+
if (React.isValidElement(icon)) {
179+
result = icon
180+
} else if (typeof icon === 'string') {
181+
result = (
182+
<Icon
183+
iconSet={iconSet}
184+
name={icon}
185+
color={textFlatten.color}
186+
style={styles.icon}
187+
size={24}
188+
/>
189+
)
190+
}
191+
192+
return result
193+
}
194+
195+
render() {
196+
const {
197+
text,
198+
disabled,
199+
raised,
200+
upperCase,
201+
onLongPress,
202+
testID,
203+
} = this.props
204+
205+
const styles = getStyles(this.props, this.state)
206+
207+
const content = (
208+
<View style={styles.container} pointerEvents="box-only">
209+
{this.renderIcon(styles)}
210+
<Text style={styles.text}>{upperCase ? text.toUpperCase() : text}</Text>
211+
</View>
212+
)
213+
214+
if (disabled) {
215+
return content
216+
}
217+
218+
return (
219+
<RippleFeedback
220+
testID={testID}
221+
onPress={!disabled ? this.onPress : null}
222+
onLongPress={!disabled ? onLongPress : null}
223+
onPressIn={raised ? this.setElevation : null}
224+
onPressOut={raised ? this.removeElevation : null}
225+
delayPressIn={50}
226+
>
227+
{content}
228+
</RippleFeedback>
229+
)
230+
}
231+
}
232+
233+
Button.propTypes = propTypes
234+
Button.defaultProps = defaultProps
235+
236+
export default withTheme(Button)

src/components/Button/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @flow
2+
import Component from './Button'
3+
4+
export default Component

0 commit comments

Comments
 (0)