forked from vtex-apps/search-result
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Popup.js
145 lines (121 loc) · 3.83 KB
/
Popup.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
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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import { IconCaret } from 'vtex.store-icons'
import searchResult from '../searchResult.css'
const { Provider, Consumer } = React.createContext()
export class PopupProvider extends Component {
static propTypes = {
children: PropTypes.node,
}
state = {
openedItem: null,
}
handleClose = id => e => {
e.preventDefault()
if (id === this.state.openedItem) {
this.setState({
openedItem: null,
})
document.body.classList.remove(searchResult.filterPopupOpen)
}
}
handleClick = id => e => {
e.preventDefault()
if (id === this.state.openedItem) {
this.setState({
openedItem: null,
})
document.body.classList.remove(searchResult.filterPopupOpen)
} else {
this.setState({
openedItem: id,
})
document.body.classList.add(searchResult.filterPopupOpen)
}
}
checkOpen = id =>
id === this.state.openedItem
render() {
return (
<Provider
value={{
onToggle: this.handleClick,
isOpen: this.checkOpen,
onClose: this.handleClose,
}}
>
{this.props.children}
</Provider>
)
}
}
export default class Popup extends Component {
static propTypes = {
title: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.func,
]),
renderFooter: PropTypes.func,
icon: PropTypes.node,
}
static defaultProps = {
renderFooter: () => null,
icon: <IconCaret orientation="down" size={16} />,
}
contentRef = React.createRef()
render() {
const { children, renderFooter, title, id, icon } = this.props
return (
<Consumer>
{contextProps => {
const { isOpen, onToggle } = contextProps
const open = isOpen(id)
const className = classNames(`${searchResult.filterPopup} relative justify-center flex`, {
[searchResult.filterPopupOpen]: open,
})
const contentClassName = classNames(
`${searchResult.filterPopupContentContainer} h-auto bg-base fixed dn w-100 left-0 bottom-0 z-1 ph3 overflow-y-auto flex-column`,
{
[`${searchResult.filterPopupContentContainerOpen} flex`]: open,
}
)
const contentTop = this.contentRef.current
? this.contentRef.current.getBoundingClientRect().bottom + 1
: 0
const renderProps = {
onClose: contextProps.onClose(id),
onToggle: contextProps.onToggle(id),
}
const childrenFn = typeof children === 'function' ? children : () => children
return (
<div className={className} ref={this.contentRef}>
<button
className={classNames(`${searchResult.filterPopupButton} ph3 pv5 mv0 mh3 pointer flex justify-center items-center`, {
'bb b--muted-1': open,
'bn': !open,
})}
onClick={onToggle(id)}
>
<span className={`${searchResult.filterPopupTitle} c-on-base t-action--small ml-auto`}>{title}</span>
<span className={`${searchResult.filterPopupArrowIcon} ml-auto pl3 pt2`}>
{icon}
</span>
</button>
<div className={contentClassName} style={{ top: contentTop }}>
<div className={searchResult.filterPopupContent}>
{childrenFn(renderProps)}
</div>
<div className={searchResult.filterPopupFooter}>
{renderFooter(renderProps)}
</div>
</div>
</div>
)
}}
</Consumer>
)
}
}