Skip to content

Commit

Permalink
refactor rest of class componetns
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Jan 19, 2024
1 parent 6babdfe commit d5c2e97
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 346 deletions.
143 changes: 70 additions & 73 deletions protocol-designer/src/components/SelectionRect.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
import * as React from 'react'

import styles from './SelectionRect.css'
import { DragRect, GenericRect } from '../collision-types'
import type { DragRect, GenericRect } from '../collision-types'

interface Props {
onSelectionMove?: (e: MouseEvent, arg: GenericRect) => unknown
onSelectionDone?: (e: MouseEvent, arg: GenericRect) => unknown
onSelectionMove?: (e: MouseEvent, arg: GenericRect) => void
onSelectionDone?: (e: MouseEvent, arg: GenericRect) => void
svg?: boolean // set true if this is an embedded SVG
children?: React.ReactNode
originXOffset?: number
originYOffset?: number
}

interface State {
positions: DragRect | null
}

export class SelectionRect extends React.Component<Props, State> {
parentRef?: HTMLElement | SVGElement | null

constructor(props: Props) {
super(props)
this.state = { positions: null }
}

renderRect(args: DragRect): React.ReactNode {
export const SelectionRect = (props: Props): JSX.Element => {
const parentRef = React.useRef<HTMLElement | SVGElement | null>(null)

Check warning on line 16 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L16

Added line #L16 was not covered by tests
const {
onSelectionMove,
onSelectionDone,
svg,
children,
originXOffset,
originYOffset,
} = props
const [positions, setPositions] = React.useState<DragRect | null>(null)

Check warning on line 25 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L24-L25

Added lines #L24 - L25 were not covered by tests

const renderRect = (args: DragRect): React.ReactNode => {

Check warning on line 27 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L27

Added line #L27 was not covered by tests
const { xStart, yStart, xDynamic, yDynamic } = args
const left = Math.min(xStart, xDynamic)
const top = Math.min(yStart, yDynamic)
const width = Math.abs(xDynamic - xStart)
const height = Math.abs(yDynamic - yStart)
const { originXOffset = 0, originYOffset = 0 } = this.props
if (this.props.svg) {
if (svg) {
// calculate ratio btw clientRect bounding box vs svg parent viewBox
// WARNING: May not work right if you're nesting SVGs!
const parentRef = this.parentRef
if (!parentRef) {
if (!parentRef.current) {
return null
}

Expand All @@ -44,7 +42,7 @@ export class SelectionRect extends React.Component<Props, State> {
height: number
left: number
top: number
} = parentRef.getBoundingClientRect()
} = parentRef.current.getBoundingClientRect()

Check warning on line 45 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L45

Added line #L45 was not covered by tests
// @ts-expect-error(sa, 2021-7-1): parentRef.closest might return null
const viewBox: { width: number; height: number } = parentRef.closest(
'svg'
Expand All @@ -55,8 +53,8 @@ export class SelectionRect extends React.Component<Props, State> {

return (
<rect
x={(left - clientRect.left) * xScale - originXOffset}
y={(top - clientRect.top) * yScale - originYOffset}
x={(left - clientRect.left) * xScale - (originXOffset ?? 0)}
y={(top - clientRect.top) * yScale - (originYOffset ?? 0)}
width={width * xScale}
height={height * yScale}
className={styles.selection_rect}
Expand All @@ -77,7 +75,7 @@ export class SelectionRect extends React.Component<Props, State> {
)
}

getRect(args: DragRect): GenericRect {
const getRect = (args: DragRect): GenericRect => {

Check warning on line 78 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L78

Added line #L78 was not covered by tests
const { xStart, yStart, xDynamic, yDynamic } = args
// convert internal rect position to more generic form
// TODO should this be used in renderRect?
Expand All @@ -89,74 +87,73 @@ export class SelectionRect extends React.Component<Props, State> {
}
}

handleMouseDown: React.MouseEventHandler = e => {
document.addEventListener('mousemove', this.handleDrag)
document.addEventListener('mouseup', this.handleMouseUp)
this.setState({
positions: {
xStart: e.clientX,
xDynamic: e.clientX,
yStart: e.clientY,
yDynamic: e.clientY,
},
const handleMouseDown: React.MouseEventHandler<
SVGGElement | HTMLElement
> = e => {
document.addEventListener('mousemove', handleDrag)
document.addEventListener('mouseup', handleMouseUp)
setPositions({

Check warning on line 95 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L92-L95

Added lines #L92 - L95 were not covered by tests
xStart: e.clientX,
xDynamic: e.clientX,
yStart: e.clientY,
yDynamic: e.clientY,
})
}

handleDrag: (e: MouseEvent) => void = e => {
if (this.state.positions) {
const handleDrag: (e: MouseEvent) => void = e => {

Check warning on line 103 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L103

Added line #L103 was not covered by tests
if (positions) {
const nextRect = {
...this.state.positions,
...positions,
xDynamic: e.clientX,
yDynamic: e.clientY,
}
this.setState({ positions: nextRect })

const rect = this.getRect(nextRect)
this.props.onSelectionMove && this.props.onSelectionMove(e, rect)
setPositions(nextRect)

Check warning on line 111 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L111

Added line #L111 was not covered by tests

const rect = getRect(nextRect)

Check warning on line 113 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L113

Added line #L113 was not covered by tests
onSelectionMove != null && onSelectionMove(e, rect)
}
}

handleMouseUp: (e: MouseEvent) => void = e => {
const handleMouseUp: (e: MouseEvent) => void = e => {

Check warning on line 118 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L118

Added line #L118 was not covered by tests
if (!(e instanceof MouseEvent)) {
return
}
document.removeEventListener('mousemove', this.handleDrag)
document.removeEventListener('mouseup', this.handleMouseUp)
document.removeEventListener('mousemove', handleDrag)
document.removeEventListener('mouseup', handleMouseUp)

Check warning on line 123 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L122-L123

Added lines #L122 - L123 were not covered by tests

const finalRect = this.state.positions && this.getRect(this.state.positions)
const finalRect = positions != null && getRect(positions)

// clear the rectangle
this.setState({ positions: null })
setPositions(null)

Check warning on line 128 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L128

Added line #L128 was not covered by tests

// call onSelectionDone callback with {x0, x1, y0, y1} of final selection rectangle
this.props.onSelectionDone &&
finalRect &&
this.props.onSelectionDone(e, finalRect)
onSelectionDone != null && finalRect && onSelectionDone(e, finalRect)
}

render(): React.ReactNode {
const { svg, children } = this.props

return svg ? (
<g
onMouseDown={this.handleMouseDown}
ref={ref => {
this.parentRef = ref
}}
>
{children}
{this.state.positions && this.renderRect(this.state.positions)}
</g>
) : (
<div
onMouseDown={this.handleMouseDown}
ref={ref => {
this.parentRef = ref
}}
>
{this.state.positions && this.renderRect(this.state.positions)}
{children}
</div>
)
}
return svg ? (
<g
onMouseDown={handleMouseDown}
ref={ref => {
if (ref) {
parentRef.current = ref

Check warning on line 139 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L139

Added line #L139 was not covered by tests
}
}}
>
{children}
{positions != null && renderRect(positions)}
</g>
) : (
<div
onMouseDown={handleMouseDown}
ref={ref => {
if (ref) {
parentRef.current = ref

Check warning on line 151 in protocol-designer/src/components/SelectionRect.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/SelectionRect.tsx#L151

Added line #L151 was not covered by tests
}
}}
>
{positions && renderRect(positions)}
{children}
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function TipPositionField(props: Props): JSX.Element {
} = props

Check warning on line 39 in protocol-designer/src/components/StepEditForm/fields/TipPositionField/index.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/StepEditForm/fields/TipPositionField/index.tsx#L39

Added line #L39 was not covered by tests
const [isModalOpen, setModalOpen] = React.useState(false)
const labwareEntities = useSelector(stepFormSelectors.getLabwareEntities)

Check warning on line 41 in protocol-designer/src/components/StepEditForm/fields/TipPositionField/index.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/StepEditForm/fields/TipPositionField/index.tsx#L41

Added line #L41 was not covered by tests
const labwareDef = labwareId != null ? labwareEntities[labwareId].def : null
const labwareDef =
labwareId != null && labwareEntities[labwareId] != null
? labwareEntities[labwareId].def
: null

let wellDepthMm: number = 0

Check warning on line 47 in protocol-designer/src/components/StepEditForm/fields/TipPositionField/index.tsx

View check run for this annotation

Codecov / codecov/patch

protocol-designer/src/components/StepEditForm/fields/TipPositionField/index.tsx#L47

Added line #L47 was not covered by tests
if (labwareDef != null) {
Expand Down
Loading

0 comments on commit d5c2e97

Please sign in to comment.