Refactor NameableGraph into LayoutCell

Chose a more semantic name for NameableGraph and its sub-components,
removed unused functions
pull/10616/head
Alex P 2017-09-20 13:34:19 -07:00
parent 57fadfe991
commit 9f419116dd
5 changed files with 61 additions and 122 deletions

View File

@ -1,13 +1,10 @@
import React, {Component, PropTypes} from 'react' import React, {Component, PropTypes} from 'react'
import _ from 'lodash' import _ from 'lodash'
import classnames from 'classnames'
import ContextMenu from 'shared/components/ContextMenu' import LayoutCellMenu from 'shared/components/LayoutCellMenu'
import CustomTimeIndicator from 'shared/components/CustomTimeIndicator' import LayoutCellHeader from 'shared/components/LayoutCellHeader'
import {NEW_DEFAULT_DASHBOARD_CELL} from 'src/dashboards/constants/index' class LayoutCell extends Component {
class NameableGraph extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = {
@ -50,11 +47,10 @@ class NameableGraph extends Component {
const {cellName, isDeleting} = this.state const {cellName, isDeleting} = this.state
const queries = _.get(cell, ['queries'], []) const queries = _.get(cell, ['queries'], [])
const cellNameIsDefault = cellName === NEW_DEFAULT_DASHBOARD_CELL.name
return ( return (
<div className="dash-graph"> <div className="dash-graph">
<ContextMenu <LayoutCellMenu
cell={cell} cell={cell}
onDeleteClick={this.handleDeleteClick} onDeleteClick={this.handleDeleteClick}
onDelete={this.handleDeleteCell} onDelete={this.handleDeleteCell}
@ -63,24 +59,11 @@ class NameableGraph extends Component {
handleClickOutside={this.closeMenu} handleClickOutside={this.closeMenu}
onEdit={this.handleSummonOverlay} onEdit={this.handleSummonOverlay}
/> />
<div <LayoutCellHeader
className={classnames('dash-graph--heading', { cellName={cellName}
'dash-graph--heading-draggable': isEditable, queries={queries}
})} isEditable={isEditable}
> />
<span
className={
cellNameIsDefault
? 'dash-graph--name dash-graph--name__default'
: 'dash-graph--name'
}
>
{cellName}
{queries && queries.length
? <CustomTimeIndicator queries={queries} />
: null}
</span>
</div>
<div className="dash-graph--container"> <div className="dash-graph--container">
{queries.length {queries.length
? children ? children
@ -100,7 +83,7 @@ class NameableGraph extends Component {
const {array, bool, func, node, number, shape, string} = PropTypes const {array, bool, func, node, number, shape, string} = PropTypes
NameableGraph.propTypes = { LayoutCell.propTypes = {
cell: shape({ cell: shape({
name: string.isRequired, name: string.isRequired,
isEditing: bool, isEditing: bool,
@ -115,4 +98,4 @@ NameableGraph.propTypes = {
onCancelEditCell: func, onCancelEditCell: func,
} }
export default NameableGraph export default LayoutCell

View File

@ -0,0 +1,41 @@
import React, {PropTypes} from 'react'
import classnames from 'classnames'
import CustomTimeIndicator from 'shared/components/CustomTimeIndicator'
import {NEW_DEFAULT_DASHBOARD_CELL} from 'src/dashboards/constants/index'
const LayoutCellHeader = ({queries, isEditable, cellName}) => {
const cellNameIsDefault = cellName === NEW_DEFAULT_DASHBOARD_CELL.name
return (
<div
className={classnames('dash-graph--heading', {
'dash-graph--heading-draggable': isEditable,
})}
>
<span
className={
cellNameIsDefault
? 'dash-graph--name dash-graph--name__default'
: 'dash-graph--name'
}
>
{cellName}
{queries && queries.length
? <CustomTimeIndicator queries={queries} />
: null}
</span>
</div>
)
}
const {array, bool, string} = PropTypes
LayoutCellHeader.propTypes = {
queries: array,
isEditable: bool,
cellName: string,
}
export default LayoutCellHeader

View File

@ -1,7 +1,7 @@
import React, {PropTypes} from 'react' import React, {PropTypes} from 'react'
import OnClickOutside from 'react-onclickoutside' import OnClickOutside from 'react-onclickoutside'
const ContextMenu = OnClickOutside( const LayoutCellMenu = OnClickOutside(
({isDeleting, onEdit, onDeleteClick, onDelete, cell}) => ({isDeleting, onEdit, onDeleteClick, onDelete, cell}) =>
<div <div
className={ className={
@ -29,17 +29,17 @@ const ContextMenu = OnClickOutside(
</div> </div>
) )
const ContextMenuContainer = props => { const LayoutCellMenuContainer = props => {
if (!props.isEditable) { if (!props.isEditable) {
return null return null
} }
return <ContextMenu {...props} /> return <LayoutCellMenu {...props} />
} }
const {bool, func, shape} = PropTypes const {bool, func, shape} = PropTypes
ContextMenuContainer.propTypes = { LayoutCellMenuContainer.propTypes = {
isDeleting: bool, isDeleting: bool,
onEdit: func, onEdit: func,
onDelete: func, onDelete: func,
@ -48,6 +48,6 @@ ContextMenuContainer.propTypes = {
isEditable: bool, isEditable: bool,
} }
ContextMenu.propTypes = ContextMenuContainer.propTypes LayoutCellMenu.propTypes = LayoutCellMenuContainer.propTypes
export default ContextMenuContainer export default LayoutCellMenuContainer

View File

@ -2,7 +2,7 @@ import React, {Component, PropTypes} from 'react'
import ReactGridLayout, {WidthProvider} from 'react-grid-layout' import ReactGridLayout, {WidthProvider} from 'react-grid-layout'
import NameableGraph from 'shared/components/NameableGraph' import LayoutCell from 'shared/components/LayoutCell'
import RefreshingGraph from 'shared/components/RefreshingGraph' import RefreshingGraph from 'shared/components/RefreshingGraph'
import AlertsApp from 'src/alerts/containers/AlertsApp' import AlertsApp from 'src/alerts/containers/AlertsApp'
import NewsFeed from 'src/status/components/NewsFeed' import NewsFeed from 'src/status/components/NewsFeed'
@ -154,7 +154,7 @@ class LayoutRenderer extends Component {
return ( return (
<div key={cell.i}> <div key={cell.i}>
<NameableGraph <LayoutCell
onCancelEditCell={onCancelEditCell} onCancelEditCell={onCancelEditCell}
isEditable={isEditable} isEditable={isEditable}
onEditCell={onEditCell} onEditCell={onEditCell}
@ -175,7 +175,7 @@ class LayoutRenderer extends Component {
axes={axes} axes={axes}
onZoom={onZoom} onZoom={onZoom}
/>} />}
</NameableGraph> </LayoutCell>
</div> </div>
) )
}) })

View File

@ -1,85 +0,0 @@
import React, {PropTypes} from 'react'
import classnames from 'classnames'
import CustomTimeIndicator from 'shared/components/CustomTimeIndicator'
const NameableGraphHeader = ({
onCancelEditCell,
isEditable,
onRenameCell,
onUpdateCell,
cell,
cellName,
cell: {i, name, queries},
}) => {
const isInputVisible = isEditable && cell.isEditing
const className = classnames('dash-graph--heading', {
'dash-graph--heading-draggable': isEditable,
})
const onKeyUp = evt => {
if (evt.key === 'Enter') {
onUpdateCell({...cell, name: cellName})()
}
if (evt.key === 'Escape') {
onCancelEditCell(i)
}
}
return (
<div className={className}>
{isInputVisible
? <GraphNameInput
value={cellName}
onChange={onRenameCell}
onBlur={onUpdateCell({...cell, name: cellName})}
onKeyUp={onKeyUp}
/>
: <GraphName name={name} queries={queries} />}
</div>
)
}
const {arrayOf, bool, func, string, shape} = PropTypes
NameableGraphHeader.propTypes = {
cell: shape(),
cellName: string,
onRenameCell: func,
onUpdateCell: func,
isEditable: bool,
onCancelEditCell: func,
}
const GraphName = ({name, queries}) =>
<span className="dash-graph--name">
{name}
{queries && queries.length
? <CustomTimeIndicator queries={queries} />
: null}
</span>
GraphName.propTypes = {
name: string,
queries: arrayOf(shape()),
}
const GraphNameInput = ({value, onKeyUp, onChange, onBlur}) =>
<input
className="form-control input-sm dash-graph--name-edit"
type="text"
value={value}
autoFocus={true}
onChange={onChange}
onBlur={onBlur}
onKeyUp={onKeyUp}
/>
GraphNameInput.propTypes = {
value: string,
onKeyUp: func,
onChange: func,
onBlur: func,
}
export default NameableGraphHeader