forked from reactjs/react-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 6
/
InputWithPopup.js
82 lines (69 loc) · 1.99 KB
/
InputWithPopup.js
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
var ComponentPropType = require('./helpers/ComponentPropType');
var React = require('react/addons');
var {PureRenderMixin} = React.addons;
var joinClasses = require('react/lib/joinClasses');
var getUniqueId = require('./helpers/getUniqueId');
/**
* <InputWithPopup> enables us to render an <input> (or other component with
* similar contract) with an attached popup flyout.
*
* To use it, pass the values that you would normally pass to an <input> to
* the <InputWithPopup>, and then toggle the
*/
var InputWithPopup = React.createClass({
mixins: [PureRenderMixin],
propTypes: {
/**
* Boolean value that's true when the popup should be open.
* Default is false.
*/
isOpen: React.PropTypes.bool,
/**
* The component to render as the input element.
* Default is <input>.
*/
inputComponent: ComponentPropType,
/**
* The component to render as the wrapper around the popup.
* Default is <div>
*/
popupComponent: ComponentPropType
},
getDefaultProps: function() {
return {
isOpen: false,
inputComponent: 'input',
popupComponent: 'div'
};
},
getInitialState: function() {
return {
id: getUniqueId('InputWithPopup')
};
},
render: function() {
var InputComponent = this.props.inputComponent;
var PopupComponent = this.props.popupComponent;
var {className, children, isOpen, ...otherProps} = this.props;
return (
<div className="InputWithPopup">
<InputComponent
{...otherProps}
aria-haspopup="true"
aria-owns={this.state.id}
aria-expanded={isOpen+''}
className={joinClasses('InputWithPopup-input', className)}
/>
<PopupComponent
id={this.state.id}
className={joinClasses(
'InputWithPopup-popup',
isOpen && 'InputWithPopup-popup--isOpen'
)}>
{children}
</PopupComponent>
</div>
);
}
});
module.exports = InputWithPopup;