Skip to content

Commit

Permalink
refactor(EstimateCost): improve re-render performances (#4489)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthprost authored Nov 22, 2024
1 parent 84d8445 commit 70a3515
Show file tree
Hide file tree
Showing 16 changed files with 580 additions and 549 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-months-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ultraviolet/plus": patch
---

Improve render performances on `<EstimateCost />`
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react'
import { memo, useMemo } from 'react'
import { useEstimateCost } from '../EstimateCostProvider'
import type { Iteration, Units } from '../types'
import { UnitInput } from './UnitInput'
Expand All @@ -10,49 +10,51 @@ type CustomUnitInputProps = {
timeUnits: Units[]
}

export const CustomUnitInput = ({
defaultTimeUnit = 'hours',
setIteration,
iteration,
timeUnits,
}: CustomUnitInputProps) => {
const { locales } = useEstimateCost()
export const CustomUnitInput = memo(
({
defaultTimeUnit = 'hours',
setIteration,
iteration,
timeUnits,
}: CustomUnitInputProps) => {
const { locales } = useEstimateCost()

const options = useMemo(
() =>
timeUnits.map(unit => ({
value: unit,
label: locales[`estimate.cost.units.${unit}.label`],
})),
[timeUnits, locales],
)
const options = useMemo(
() =>
timeUnits.map(unit => ({
value: unit,
label: locales[`estimate.cost.units.${unit}.label`],
})),
[timeUnits, locales],
)

const defaultOption = useMemo(
() => options.find(({ value }) => value === defaultTimeUnit),
[defaultTimeUnit, options],
)
const defaultOption = useMemo(
() => options.find(({ value }) => value === defaultTimeUnit),
[defaultTimeUnit, options],
)

return (
<UnitInput
name="iteration"
onChange={inputValue =>
setIteration({
unit: iteration.unit,
value: inputValue,
})
}
onChangeUnitValue={unitValue => {
setIteration({
unit: unitValue as Units,
value: iteration.value,
})
}}
placeholder="0"
value={iteration.value}
unitValue={iteration.unit || defaultOption?.value}
minValue={1}
size="medium"
options={options}
/>
)
}
return (
<UnitInput
name="iteration"
onChange={inputValue =>
setIteration({
unit: iteration.unit,
value: inputValue,
})
}
onChangeUnitValue={unitValue => {
setIteration({
unit: unitValue as Units,
value: iteration.value,
})
}}
placeholder="0"
value={iteration.value}
unitValue={iteration.unit || defaultOption?.value}
minValue={1}
size="medium"
options={options}
/>
)
},
)
Loading

0 comments on commit 70a3515

Please sign in to comment.