Skip to content

Commit

Permalink
fix(rn): prevent double clicking submit buttons
Browse files Browse the repository at this point in the history
fix #799

Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Jan 7, 2019
1 parent 40110a4 commit 0e2b698
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
10 changes: 6 additions & 4 deletions client/react-native/common/components/Library/Flex.js
@@ -1,5 +1,7 @@
import { View } from 'react-native'
import React from 'react'
import { View, TouchableOpacity } from 'react-native'

import Touchable from './Touchable'

const getDirection = (key, directions = { rows: 'column', cols: 'row' }) =>
directions[key] || null
Expand Down Expand Up @@ -49,7 +51,7 @@ const getProps = ({

export const Block = props => {
return props.onPress ? (
<TouchableOpacity {...getProps(props)} style={getStyle(props)} />
<Touchable {...getProps(props)} style={getStyle(props)} />
) : (
<View {...getProps(props)} style={getStyle(props)} />
)
Expand All @@ -58,7 +60,7 @@ export const Block = props => {
export const Rows = ({ direction, ...props }) => {
props.direction = 'rows'
return props.onPress ? (
<TouchableOpacity {...getProps(props)} style={getStyle(props)} />
<Touchable {...getProps(props)} style={getStyle(props)} />
) : (
<View {...getProps(props)} style={getStyle(props)} />
)
Expand All @@ -67,7 +69,7 @@ export const Rows = ({ direction, ...props }) => {
export const Cols = ({ direction, ...props }) => {
props.direction = 'cols'
return props.onPress ? (
<TouchableOpacity {...getProps(props)} style={getStyle(props)} />
<Touchable {...getProps(props)} style={getStyle(props)} />
) : (
<View {...getProps(props)} style={getStyle(props)} />
)
Expand Down
38 changes: 38 additions & 0 deletions client/react-native/common/components/Library/Touchable.js
@@ -0,0 +1,38 @@
import React, { PureComponent } from 'react'
import { TouchableOpacity } from 'react-native'

export default class Touchable extends PureComponent {
state = {
loading: false,
}

preventDoubleClicking = onPress => () => {
this.setState({ loading: true }, async () => {
if (!onPress) {
return
}
await onPress()
this.setState({ loading: false })
})
}

render () {
const { loading } = this.state
const {
style = {},
onPress,
opacity,
activeOpacity = 0.3,
...props
} = this.props
style.opacity = opacity || onPress ? 1 : activeOpacity
return (
<TouchableOpacity
onPress={!loading ? this.preventDoubleClicking(onPress) : undefined}
activeOpacity={activeOpacity}
style={style}
{...props}
/>
)
}
}

0 comments on commit 0e2b698

Please sign in to comment.