Skip to content
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

feat(protocol-designer): implement rounding properly #2458

Merged
merged 5 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import * as React from 'react'
import round from 'lodash/round'
import {
AlertModal,
FormGroup,
Expand Down Expand Up @@ -121,7 +122,7 @@ export default class FlowRateField extends React.Component<Props, State> {
minFlowRate > modalFlowRateNum ||
modalFlowRateNum > maxFlowRate
)
const correctDecimals = Number(modalFlowRateNum.toFixed(DECIMALS_ALLOWED)) === modalFlowRateNum
const correctDecimals = round(modalFlowRateNum, DECIMALS_ALLOWED) === modalFlowRateNum
const allowSave = modalUseDefault || (!outOfBounds && correctDecimals)

let errorMessage = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as React from 'react'
import cx from 'classnames'
import {connect} from 'react-redux'
import round from 'lodash/round'
import {
Modal,
OutlineButton,
Expand All @@ -25,6 +26,7 @@ import styles from './TipPositionInput.css'

const SMALL_STEP_MM = 1
const LARGE_STEP_MM = 10
const DECIMALS_ALLOWED = 1

type DP = { updateValue: (string) => mixed }

Expand All @@ -37,10 +39,10 @@ type OP = {
}

type Props = OP & DP
type State = { value: string}
type State = { value: string }

const formatValue = (value: mixed) => (
parseFloat(value).toFixed(1)
const formatValue = (value: number | string): string => (
String(round(Number(value), DECIMALS_ALLOWED))
)

class TipPositionModal extends React.Component<Props, State> {
Expand Down Expand Up @@ -75,14 +77,14 @@ class TipPositionModal extends React.Component<Props, State> {
}
handleChange = (e: SyntheticEvent<HTMLSelectElement>) => {
const {value} = e.currentTarget
const valueFloat = formatValue(value)
const valueFloat = Number(formatValue(value))
const maximumHeightMM = (this.props.wellHeightMM * 2)

if (!value) {
this.setState({value})
} else if (Number(valueFloat) > maximumHeightMM) {
} else if (valueFloat > maximumHeightMM) {
this.setState({value: formatValue(maximumHeightMM)})
} else if (Number(valueFloat) >= 0) {
} else if (valueFloat >= 0) {
const numericValue = value.replace(/[^.0-9]/, '')
this.setState({value: numericValue.replace(/(\d*[.]{1}\d{1})(\d*)/, (match, group1) => group1)})
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// @flow
import * as React from 'react'
import round from 'lodash/round'

import PIPETTE_TIP_IMAGE from '../../../images/pipette_tip.svg'
import WELL_CROSS_SECTION_IMAGE from '../../../images/well_cross_section.svg'

import styles from './TipPositionInput.css'

const WELL_HEIGHT_PIXELS = 48
const PIXEL_DECIMALS = 2
type Props = {
mmFromBottom: string,
wellHeightMM: number,
Expand All @@ -15,7 +17,7 @@ type Props = {
const TipPositionZAxisViz = (props: Props) => {
const fractionOfWellHeight = Number(props.mmFromBottom) / props.wellHeightMM
const pixelsFromBottom = (Number(fractionOfWellHeight) * WELL_HEIGHT_PIXELS) - WELL_HEIGHT_PIXELS
const roundedPixelsFromBottom = String(pixelsFromBottom.toFixed(2))
const roundedPixelsFromBottom = String(round(pixelsFromBottom, PIXEL_DECIMALS))
const bottomPx = props.wellHeightMM ? roundedPixelsFromBottom : (parseFloat(props.mmFromBottom) - WELL_HEIGHT_PIXELS)
return (
<div className={styles.viz_wrapper}>
Expand Down
3 changes: 2 additions & 1 deletion protocol-designer/src/components/steplist/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function formatVolume (inputVolume: ?string | ?number, sigDigits?: number
return inputVolume || ''
}

const PERCENTAGE_DECIMALS_ALLOWED = 1
export const formatPercentage = (part: number, total: number): string => {
return `${Number((part / total) * 100).toFixed(1)}%`
return `${round((part / total) * 100, PERCENTAGE_DECIMALS_ALLOWED)}%`
}