Skip to content

Commit 6374185

Browse files
dspiridonov4xxibeshkenadze
authored andcommitted
[CAES-257] Added LongClickBehaviour component. Fixed components which… (#171)
* [CAES-257] Added LongClickBehaviour component. Fixed components which is using it * [CAES-257] Removed handler * [CAES-257] Renamed LongClick to HoldClick
1 parent 6026d71 commit 6374185

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { Component } from 'react';
2+
3+
class HoldClickBehaviour extends Component {
4+
timer = null;
5+
6+
handleButtonPress = () => {
7+
const { delay = 250, onHoldStart } = this.props;
8+
9+
this.timer = setTimeout(onHoldStart, delay);
10+
};
11+
12+
handleButtonRelease = () => {
13+
const { onHoldEnd } = this.props;
14+
15+
clearTimeout(this.timer);
16+
17+
onHoldEnd();
18+
};
19+
20+
render() {
21+
const { onHoldStart, onHoldEnd, ...props } = this.props;
22+
23+
return (
24+
<div
25+
{...props}
26+
role="presentation"
27+
onTouchStart={this.handleButtonPress}
28+
onTouchEnd={this.handleButtonRelease}
29+
onMouseDown={this.handleButtonPress}
30+
onMouseUp={this.handleButtonRelease}
31+
onMouseLeave={this.handleButtonRelease}
32+
/>
33+
);
34+
}
35+
}
36+
37+
export default HoldClickBehaviour;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as HoldClickBehaviour } from './HoldClickBehaviour';

components/Input/PasswordInput.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { PureComponent } from 'react';
22
import styled from 'styled-components';
33
import Input from './Input';
44
import { Icon } from '../Icon';
5+
import { HoldClickBehaviour } from '../HoldClickBehaviour';
56

67
const StyledIcon = styled(Icon)`
78
cursor: pointer;
@@ -12,10 +13,10 @@ class PasswordInput extends PureComponent {
1213
visible: false,
1314
};
1415

15-
handleChangeVisible = () => {
16-
this.setState(prevState => ({
17-
visible: !prevState.visible,
18-
}));
16+
handleToggleVisible = visible => () => {
17+
this.setState({
18+
visible,
19+
});
1920
};
2021

2122
render() {
@@ -32,12 +33,12 @@ class PasswordInput extends PureComponent {
3233
type={type}
3334
postfix={
3435
value && (
35-
<StyledIcon
36-
name={iconName}
37-
width={18}
38-
height={18}
39-
onClick={this.handleChangeVisible}
40-
/>
36+
<HoldClickBehaviour
37+
onHoldStart={this.handleToggleVisible(true)}
38+
onHoldEnd={this.handleToggleVisible(false)}
39+
>
40+
<StyledIcon name={iconName} width={18} height={18} />
41+
</HoldClickBehaviour>
4142
)
4243
}
4344
/>

components/Item/Types/Credentials/Credentials.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import styled from 'styled-components';
33
import copy from 'copy-text-to-clipboard';
4-
import { Icon, Label } from 'components';
4+
import { Icon, Label, HoldClickBehaviour } from 'components';
55
import {
66
Wrapper,
77
Row,
@@ -47,7 +47,7 @@ class Credentials extends Component {
4747
isPasswordVisible: false,
4848
};
4949

50-
handleTogglePasswordVisibility = visible => {
50+
handleToggleVisibility = visible => () => {
5151
this.setState(() => ({
5252
isPasswordVisible: visible,
5353
}));
@@ -124,20 +124,12 @@ class Credentials extends Component {
124124
<FieldValue>
125125
<FixedSizeField>{pwd}</FixedSizeField>
126126
<Row>
127-
<StyledEyeIcon
128-
name={eyeIconName}
129-
width={20}
130-
height={20}
131-
onMouseDown={() => {
132-
return this.handleTogglePasswordVisibility(true);
133-
}}
134-
onMouseUp={() => {
135-
return this.handleTogglePasswordVisibility(false);
136-
}}
137-
onMouseOver={() => {
138-
return this.handleTogglePasswordVisibility(false);
139-
}}
140-
/>
127+
<HoldClickBehaviour
128+
onHoldStart={this.handleToggleVisibility(true)}
129+
onHoldEnd={this.handleToggleVisibility(false)}
130+
>
131+
<StyledEyeIcon name={eyeIconName} width={20} height={20} />
132+
</HoldClickBehaviour>
141133
<StyledIcon
142134
name="copy"
143135
width={19}

components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export { Toggle } from './Toggle';
7070
export { RangeInput } from './RangeInput';
7171
export { PasswordIndicator } from './PasswordIndicator';
7272
export { TagsInput } from './TagsInput';
73+
export { HoldClickBehaviour } from './HoldClickBehaviour';

0 commit comments

Comments
 (0)