-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathChooserInput.jsx
169 lines (145 loc) · 3.97 KB
/
ChooserInput.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { pathPropType } from '../helpers/pathHelpers';
import styles from '../../styles/cspace-input/ChooserInput.css';
import buttonStyles from '../../styles/cspace-input/MiniButton.css';
const propTypes = {
children: PropTypes.node,
chooseButtonLabel: PropTypes.string,
className: PropTypes.string,
// TODO: Stop using propTypes in isInput. Until then, these unused props need to be declared so
// this component is recognized as an input.
/* eslint-disable react/no-unused-prop-types */
name: PropTypes.string,
parentPath: pathPropType,
subpath: pathPropType,
/* eslint-enable react/no-unused-prop-types */
// eslint-disable-next-line react/forbid-prop-types
value: PropTypes.any,
readOnly: PropTypes.bool,
formatValue: PropTypes.func,
onChooseButtonClick: PropTypes.func,
onDrop: PropTypes.func,
};
const defaultProps = {
children: undefined,
chooseButtonLabel: 'Select…',
className: undefined,
name: undefined,
parentPath: undefined,
subpath: undefined,
value: undefined,
readOnly: undefined,
formatValue: (value) => value,
onChooseButtonClick: undefined,
onDrop: undefined,
};
export default class ChooserInput extends Component {
constructor(props) {
super(props);
this.handleDragEnter = this.handleDragEnter.bind(this);
this.handleDragLeave = this.handleDragLeave.bind(this);
this.handleDragOver = this.handleDragOver.bind(this);
this.handleDrop = this.handleDrop.bind(this);
this.state = {
isDraggedOver: false,
};
}
handleDragEnter(event) {
event.preventDefault();
event.stopPropagation();
this.setState({
isDraggedOver: true,
});
}
handleDragLeave(event) {
event.preventDefault();
event.stopPropagation();
this.setState({
isDraggedOver: false,
});
}
// eslint-disable-next-line class-methods-use-this
handleDragOver(event) {
event.preventDefault();
event.stopPropagation();
}
handleDrop(event) {
const {
onDrop,
} = this.props;
if (onDrop) {
event.preventDefault();
event.stopPropagation();
onDrop(event.dataTransfer);
this.setState({
isDraggedOver: false,
});
}
}
render() {
const {
children,
chooseButtonLabel,
className,
value,
readOnly,
formatValue,
onChooseButtonClick,
onDrop,
} = this.props;
const {
isDraggedOver,
} = this.state;
const formattedValue = formatValue(value);
const classes = classNames(
className,
isDraggedOver ? styles.dragOver : styles.normal,
);
let dragDropProps;
if (onDrop && !readOnly) {
dragDropProps = {
onDragEnter: this.handleDragEnter,
onDragLeave: this.handleDragLeave,
onDragOver: this.handleDragOver,
onDrop: this.handleDrop,
};
}
let chooseButton;
if (!readOnly) {
chooseButton = (
<button
className={buttonStyles.common}
disabled={readOnly}
name="choose"
type="button"
onClick={onChooseButtonClick}
>
{chooseButtonLabel}
</button>
);
}
return (
// Allow click/drag/drop events on the file info div.
<div
className={classes}
>
{children}
{chooseButton}
{/* The choose button provides keyboard accessibility -- clicking on the file info
* container is just an extra convenience -- so no key event should be required. */}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
<div
onClick={readOnly ? undefined : onChooseButtonClick}
// eslint-disable-next-line react/jsx-props-no-spreading
{...dragDropProps}
>
{formattedValue}
</div>
</div>
);
}
}
ChooserInput.propTypes = propTypes;
ChooserInput.defaultProps = defaultProps;