-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Combine events and actions into select box #2394
Changes from all commits
8fa6477
2dcd986
e46183e
8cccb59
9d2c907
3582f54
7d8fbac
9ed25af
1c84fc4
10e431c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
.select-box { | ||
position: absolute; | ||
min-width: 620px; | ||
box-shadow: 0 0 8px rgba(0, 0, 0, 0.4); | ||
border-radius: 2px; | ||
border: 1px solid rgba(0, 0, 0, 0.1); | ||
height: 400px; | ||
background: #fff; | ||
z-index: 999; | ||
overflow: visible; | ||
.ant-input { | ||
border-width: 0 0 1px 0; | ||
outline: none; | ||
} | ||
.ant-divider { | ||
margin: 12px 0; | ||
border-width: 2px; | ||
} | ||
|
||
.ant-list-item { | ||
padding: 4px 12px; | ||
cursor: pointer; | ||
border: none; | ||
|
||
&:hover, | ||
&.selected { | ||
background: rgba(0, 0, 0, 0.1); | ||
} | ||
} | ||
.search-list { | ||
overflow-y: scroll; | ||
height: calc(100% - 32px); | ||
padding-top: 4px; | ||
/* Hide scrollbar for IE, Edge and Firefox */ | ||
-ms-overflow-style: none; /* IE and Edge */ | ||
scrollbar-width: none; /* Firefox */ | ||
|
||
/* Hide scrollbar for Chrome, Safari and Opera */ | ||
::-webkit-scrollbar { | ||
display: none; | ||
} | ||
} | ||
.info-box { | ||
padding: 1rem; | ||
overflow-y: scroll; | ||
height: 100%; | ||
pre { | ||
margin: 0; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import React, { useRef, useEffect, useState } from 'react' | ||
import { useActions, useValues } from 'kea' | ||
import { Col, Row, Input, Divider } from 'antd' | ||
import { List } from 'antd' | ||
import { DownOutlined, RightOutlined } from '@ant-design/icons' | ||
import { ActionType } from '~/types' | ||
import { searchItems, selectBoxLogic } from 'lib/logic/selectBoxLogic' | ||
import './SelectBox.scss' | ||
import { selectBoxLogicType } from 'types/lib/logic/selectBoxLogicType' | ||
|
||
export interface SelectBoxItem { | ||
dataSource: SelectedItem[] | ||
renderInfo({ item }: { item: SelectedItem }): JSX.Element | ||
name: JSX.Element | string | ||
} | ||
|
||
export interface SelectedItem { | ||
name: string | ||
key: string | ||
action?: ActionType | ||
event?: string | ||
volume?: number | ||
usage_count?: number | ||
category?: string | ||
} | ||
|
||
export function SelectBox({ | ||
items, | ||
selectedItemKey, | ||
onSelect, | ||
onDismiss, | ||
}: { | ||
items: SelectBoxItem[] | ||
selectedItemKey: string | ||
onSelect: CallableFunction | ||
onDismiss: CallableFunction | ||
}): JSX.Element { | ||
const dropdownRef = useRef() | ||
const dropdownLogic = selectBoxLogic({ updateFilter: onSelect, items }) | ||
const { selectedItem, RenderInfo } = useValues(dropdownLogic) | ||
const { setSearch, setSelectedItem, onKeyDown } = useActions(dropdownLogic) | ||
|
||
const deselect = (e): void => { | ||
if (dropdownRef?.current?.contains(e.target)) { | ||
return | ||
} | ||
onDismiss && onDismiss(e) | ||
} | ||
|
||
useEffect(() => { | ||
if (selectedItemKey) { | ||
const allSources = items.map((item) => item.dataSource).flat() | ||
setSelectedItem(allSources.filter((item) => item.key === selectedItemKey)[0] || false) | ||
const offset = document.querySelector('.search-list [datakey="' + selectedItemKey + '"]')?.offsetTop | ||
document.querySelector('.search-list').scrollTop = offset | ||
} | ||
document.addEventListener('mousedown', deselect) | ||
document.addEventListener('keydown', onKeyDown) | ||
macobo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return () => { | ||
document.removeEventListener('mousedown', deselect) | ||
document.removeEventListener('keydown', onKeyDown) | ||
} | ||
}, []) | ||
return ( | ||
<div ref={dropdownRef} className="select-box" tabIndex="0"> | ||
<Row style={{ height: '100%' }}> | ||
<Col sm={14} style={{ borderRight: '1px solid rgba(0, 0, 0, 0.1)', maxHeight: '100%' }}> | ||
<Input | ||
placeholder="Search events" | ||
autoFocus | ||
onChange={(e) => { | ||
setSearch(e.target.value) | ||
}} | ||
style={{ width: '100%', borderRadius: 0 }} | ||
/> | ||
<div className="search-list"> | ||
{items.map((item) => ( | ||
<> | ||
<SelectUnit | ||
name={item.name} | ||
dropdownLogic={dropdownLogic} | ||
dataSource={item.dataSource} | ||
/> | ||
<Divider /> | ||
</> | ||
))} | ||
</div> | ||
</Col> | ||
<Col sm={10} className="info-box"> | ||
{RenderInfo && <RenderInfo item={selectedItem} />} | ||
</Col> | ||
</Row> | ||
</div> | ||
) | ||
} | ||
|
||
export function SelectUnit({ | ||
name, | ||
dataSource, | ||
dropdownLogic, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: Why is this a prop? Could find no other instances where we pass logics around this way besides this PR but I didn't look super hard. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm take a look at PropertyFilter.js for example. It's a way of making sure the props for the logic are passed through correctly everywhere. |
||
}: { | ||
name: string | JSX.Element | ||
dataSource: SelectedItem[] | ||
dropdownLogic: selectBoxLogicType | ||
}): JSX.Element { | ||
const [isCollapsed, setIsCollapsed] = useState(false) | ||
const { setSelectedItem, clickSelectedItem } = useActions(dropdownLogic) | ||
const { selectedItem, search, blockMouseOver } = useValues(dropdownLogic) | ||
const data = !search ? dataSource : searchItems(dataSource, search) | ||
return ( | ||
<> | ||
<span onClick={() => setIsCollapsed(!isCollapsed)}> | ||
<h4 style={{ cursor: 'pointer', userSelect: 'none', padding: '4px 12px', marginBottom: 0 }}> | ||
{isCollapsed || data.length === 0 ? <RightOutlined /> : <DownOutlined />} {name} | ||
<span | ||
style={{ float: 'right', fontWeight: search && data.length > 0 ? 700 : 'normal' }} | ||
className="text-small" | ||
> | ||
{data.length} event{data.length !== 1 && 's'} | ||
</span> | ||
</h4> | ||
</span> | ||
{!isCollapsed && data.length > 0 && ( | ||
<List | ||
size="small" | ||
bordered={false} | ||
dataSource={data || []} | ||
renderItem={(item: SelectedItem) => ( | ||
<List.Item | ||
className={selectedItem.key === item.key && 'selected'} | ||
timgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
datakey={item.key} | ||
onClick={() => clickSelectedItem(item)} | ||
onMouseOver={() => | ||
!blockMouseOver && setSelectedItem({ ...item, key: item.key, category: name }) | ||
} | ||
> | ||
{item.name} | ||
</List.Item> | ||
)} | ||
/> | ||
)} | ||
</> | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the || false needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually not sure it is. I ran into this while developing but can't reproduce it anymore. I do still think it's safer, as the alternative is having a complete white-out