2018-02-06 13:23:50 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import copy from 'clipboard-copy';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import remcalc from 'remcalc';
|
|
|
|
import { Col, Row } from 'joyent-react-styled-flexboxgrid';
|
2018-04-12 12:53:00 +03:00
|
|
|
import { Margin } from 'styled-components-spacing';
|
|
|
|
|
|
|
|
import { FormLabel, Input } from '..';
|
2018-02-06 13:23:50 +02:00
|
|
|
|
|
|
|
import {
|
2018-04-12 12:53:00 +03:00
|
|
|
default as Tooltip,
|
|
|
|
Container as TooltipContainer,
|
|
|
|
Target as TooltipTarget
|
|
|
|
} from '../tooltip';
|
2018-02-06 13:23:50 +02:00
|
|
|
|
|
|
|
import { Clipboard } from '../icons';
|
|
|
|
|
|
|
|
const InputIconWrapper = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
input {
|
|
|
|
padding-right: ${remcalc(30)};
|
|
|
|
}
|
|
|
|
|
|
|
|
div {
|
|
|
|
position: relative;
|
|
|
|
left: ${remcalc(-26)};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2018-03-28 15:51:34 +03:00
|
|
|
const ClipboardIconActionable = styled(Clipboard)`
|
2018-02-06 13:23:50 +02:00
|
|
|
cursor: pointer;
|
|
|
|
`;
|
|
|
|
|
|
|
|
class CopyToClipboardTooltip extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
copied: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = () => {
|
|
|
|
const { children: text } = this.props;
|
|
|
|
|
|
|
|
copy(text);
|
|
|
|
|
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
copied: true
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({
|
|
|
|
copied: false
|
|
|
|
});
|
|
|
|
}, 4000);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render = () => (
|
|
|
|
<TooltipContainer hoverable>
|
|
|
|
<TooltipTarget>
|
|
|
|
<ClipboardIconActionable onClick={this.handleClick} />
|
|
|
|
</TooltipTarget>
|
|
|
|
<Tooltip placement="top" success={Boolean(this.state.copied)}>
|
|
|
|
{this.state.copied ? 'Copied To Clipboard' : 'Copy To Clipboard'}
|
|
|
|
</Tooltip>
|
|
|
|
</TooltipContainer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CopiableField = ({ md, label, text, ...rest }) => (
|
|
|
|
<Row>
|
|
|
|
<Col xs={12} md={md || 7}>
|
2018-04-12 12:53:00 +03:00
|
|
|
<Margin bottom={0.5}>
|
|
|
|
{label ? <FormLabel>{label}</FormLabel> : null}
|
|
|
|
</Margin>
|
2018-02-06 13:23:50 +02:00
|
|
|
<InputIconWrapper {...rest}>
|
|
|
|
<Input {...rest} monospace onBlur={null} fluid value={text} />
|
|
|
|
<CopyToClipboardTooltip>{text}</CopyToClipboardTooltip>
|
|
|
|
</InputIconWrapper>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default CopiableField;
|