-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): add ability to change order of variable dropdowns
- Loading branch information
1 parent
8196a00
commit 678f4a3
Showing
11 changed files
with
242 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
ui/src/dashboards/components/variablesControlBar/DraggableDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Libraries | ||
import * as React from 'react' | ||
import { | ||
DragSource, | ||
DropTarget, | ||
ConnectDropTarget, | ||
ConnectDragSource, | ||
DropTargetConnector, | ||
DragSourceConnector, | ||
DragSourceMonitor, | ||
} from 'react-dnd' | ||
import classnames from 'classnames' | ||
|
||
// Components | ||
import VariableDropdown from './VariableDropdown' | ||
|
||
// Constants | ||
const dropdownType = 'dropdown' | ||
|
||
const dropdownSource = { | ||
beginDrag(props: Props) { | ||
return { | ||
id: props.id, | ||
index: props.index, | ||
} | ||
}, | ||
} | ||
|
||
interface Props { | ||
id: string | ||
index: number | ||
name: string | ||
moveDropdown: (dragIndex: number, hoverIndex: number) => void | ||
dashboardID: string | ||
} | ||
|
||
interface DropdownSourceCollectedProps { | ||
isDragging: boolean | ||
connectDragSource: ConnectDragSource | ||
} | ||
|
||
interface DropdownTargetCollectedProps { | ||
connectDropTarget?: ConnectDropTarget | ||
} | ||
|
||
const dropdownTarget = { | ||
hover(props, monitor, component) { | ||
if (!component) { | ||
return null | ||
} | ||
const dragIndex = monitor.getItem().index | ||
const hoverIndex = props.index | ||
|
||
// Don't replace items with themselves | ||
if (dragIndex === hoverIndex) { | ||
return | ||
} | ||
|
||
// Time to actually perform the action | ||
props.moveDropdown(dragIndex, hoverIndex) | ||
|
||
monitor.getItem().index = hoverIndex | ||
}, | ||
} | ||
|
||
class Dropdown extends React.Component< | ||
Props & DropdownSourceCollectedProps & DropdownTargetCollectedProps | ||
> { | ||
public render() { | ||
const { | ||
name, | ||
id, | ||
dashboardID, | ||
isDragging, | ||
connectDragSource, | ||
connectDropTarget, | ||
} = this.props | ||
|
||
const className = classnames('variable-dropdown', { | ||
'variable-dropdown__dragging': isDragging, | ||
}) | ||
|
||
return connectDragSource( | ||
connectDropTarget( | ||
<div style={{display: 'inline-block'}}> | ||
<div className={className}> | ||
{/* TODO: Add variable description to title attribute when it is ready */} | ||
<div className="variable-dropdown--label"> | ||
<div className="customizable-field--drag"> | ||
<span className="hamburger" /> | ||
</div> | ||
<span>{name}</span> | ||
</div> | ||
<div className="variable-dropdown--placeholder" /> | ||
<VariableDropdown variableID={id} dashboardID={dashboardID} /> | ||
</div> | ||
</div> | ||
) | ||
) | ||
} | ||
} | ||
|
||
export default DropTarget<Props & DropdownTargetCollectedProps>( | ||
dropdownType, | ||
dropdownTarget, | ||
(connect: DropTargetConnector) => ({ | ||
connectDropTarget: connect.dropTarget(), | ||
}) | ||
)( | ||
DragSource<Props & DropdownSourceCollectedProps>( | ||
dropdownType, | ||
dropdownSource, | ||
(connect: DragSourceConnector, monitor: DragSourceMonitor) => ({ | ||
connectDragSource: connect.dragSource(), | ||
isDragging: monitor.isDragging(), | ||
}) | ||
)(Dropdown) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import {AppState} from 'src/shared/reducers/app' | ||
import {VariablesState} from 'src/variables/reducers' | ||
|
||
export interface LocalStorage { | ||
VERSION: string | ||
app: AppState | ||
ranges: any[] | ||
variables: VariablesState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.