Loëck Vézien
7 years ago
9 changed files with 171 additions and 26 deletions
@ -0,0 +1,87 @@ |
|||
// @flow
|
|||
|
|||
import React, { PureComponent, Fragment } from 'react' |
|||
import styled from 'styled-components' |
|||
import QrReader from 'react-qr-reader' |
|||
import noop from 'lodash/noop' |
|||
|
|||
import Box from 'components/base/Box' |
|||
import Icon from 'components/base/Icon' |
|||
import Input from 'components/base/Input' |
|||
|
|||
const IconQrCode = ({ onClick }: { onClick: Function }) => ( |
|||
<Box color="steel" style={{ position: 'absolute', right: 15 }}> |
|||
<Icon fontSize={30} name="qrcode" style={{ cursor: 'pointer' }} onClick={onClick} /> |
|||
</Box> |
|||
) |
|||
|
|||
const InputAddress = styled(Input).attrs({ |
|||
type: 'text', |
|||
})` |
|||
padding-right: ${p => p.withQrCode && 55}; |
|||
` |
|||
|
|||
const WrapperQrCode = styled(Box)` |
|||
margin-top: 10px; |
|||
position: absolute; |
|||
right: 15px; |
|||
top: 100%; |
|||
` |
|||
|
|||
type Props = { |
|||
value: string, |
|||
onChange: Function, |
|||
qrCodeSize: number, |
|||
withQrCode: boolean, |
|||
} |
|||
|
|||
type State = { |
|||
qrReaderOpened: boolean, |
|||
} |
|||
|
|||
class RecipientAddress extends PureComponent<Props, State> { |
|||
static defaultProps = { |
|||
value: '', |
|||
onChange: noop, |
|||
qrCodeSize: 200, |
|||
withQrCode: true, |
|||
} |
|||
|
|||
state = { |
|||
qrReaderOpened: false, |
|||
} |
|||
|
|||
handleClickQrCode = () => |
|||
this.setState(prev => ({ |
|||
qrReaderOpened: !prev.qrReaderOpened, |
|||
})) |
|||
|
|||
handleScanQrCode = (data: string) => data !== null && this.props.onChange(data) |
|||
|
|||
render() { |
|||
const { onChange, qrCodeSize, withQrCode, value } = this.props |
|||
const { qrReaderOpened } = this.state |
|||
|
|||
return ( |
|||
<Box relative justify="center"> |
|||
<InputAddress value={value} withQrCode={withQrCode} onChange={onChange} /> |
|||
{withQrCode && ( |
|||
<Fragment> |
|||
<IconQrCode onClick={this.handleClickQrCode} /> |
|||
{qrReaderOpened && ( |
|||
<WrapperQrCode> |
|||
<QrReader |
|||
onScan={this.handleScanQrCode} |
|||
onError={noop} |
|||
style={{ height: qrCodeSize, width: qrCodeSize }} |
|||
/> |
|||
</WrapperQrCode> |
|||
)} |
|||
</Fragment> |
|||
)} |
|||
</Box> |
|||
) |
|||
} |
|||
} |
|||
|
|||
export default RecipientAddress |
@ -0,0 +1,9 @@ |
|||
import React from 'react' |
|||
import { storiesOf } from '@storybook/react' |
|||
import { boolean } from '@storybook/addon-knobs' |
|||
|
|||
import RecipientAddress from 'components/RecipientAddress' |
|||
|
|||
const stories = storiesOf('RecipientAddress', module) |
|||
|
|||
stories.add('basic', () => <RecipientAddress withQrCode={boolean('withQrCode', true)} />) |
Loading…
Reference in new issue