2019-10-03 12:32:50 +02:00
|
|
|
import React, { useState } from 'react'
|
|
|
|
import FormFieldSlider from '../../.form/FormFieldSlider'
|
|
|
|
import OptionPreamble from '../OptionPreamble'
|
2019-04-22 18:00:26 +02:00
|
|
|
|
2020-04-25 19:09:02 +02:00
|
|
|
const PatternOptionPctDegCount = ({
|
|
|
|
min = 0,
|
|
|
|
max = 100,
|
|
|
|
step = 0.1,
|
|
|
|
type = 'pct',
|
|
|
|
updateValue,
|
|
|
|
name,
|
|
|
|
dflt,
|
|
|
|
designDflt,
|
|
|
|
title,
|
|
|
|
desc,
|
|
|
|
value,
|
|
|
|
raiseEvent,
|
|
|
|
noDocs
|
|
|
|
}) => {
|
2019-10-03 12:32:50 +02:00
|
|
|
let factor = 1
|
2020-04-25 19:09:02 +02:00
|
|
|
if (type === 'pct') factor = 100
|
|
|
|
const round = (val) => Math.round(val * 10) / 10
|
|
|
|
const [val, setVal] = useState(value === null ? dflt : round(value * factor))
|
|
|
|
const [previousValue, setPreviousValue] = useState(value === null ? dflt : round(value * factor))
|
2019-10-03 12:32:50 +02:00
|
|
|
const [expanded, setExpanded] = useState(false)
|
2019-04-22 18:00:26 +02:00
|
|
|
|
|
|
|
const update = (name, newValue, evt) => {
|
2019-10-03 12:32:50 +02:00
|
|
|
newValue = round(newValue)
|
2019-04-22 18:00:26 +02:00
|
|
|
// Sometimes, when sliding, the rapid succession of updates
|
|
|
|
// causes a weird timing issue to result in a value that is NaN.
|
|
|
|
// If that's the case, just ignore this update and keep the
|
|
|
|
// previous one instead
|
|
|
|
if (!isNaN(newValue)) {
|
2020-04-25 19:09:02 +02:00
|
|
|
setVal(newValue)
|
|
|
|
if (evt.type !== 'mousemove') updateValue(name, newValue / factor)
|
2019-04-22 18:00:26 +02:00
|
|
|
} else {
|
2020-04-25 19:09:02 +02:00
|
|
|
if (evt.type !== 'mousemove') updateValue(name, value / factor)
|
2019-04-22 18:00:26 +02:00
|
|
|
}
|
2019-10-03 12:32:50 +02:00
|
|
|
}
|
2019-04-22 18:00:26 +02:00
|
|
|
|
|
|
|
const reset = () => {
|
2020-04-25 19:09:02 +02:00
|
|
|
setVal(dflt)
|
|
|
|
updateValue(name, dflt / factor)
|
2019-10-03 12:32:50 +02:00
|
|
|
}
|
2019-04-22 18:00:26 +02:00
|
|
|
|
2019-10-03 12:32:50 +02:00
|
|
|
const patternReset = () => {
|
2020-04-25 19:09:02 +02:00
|
|
|
setVal(designDflt)
|
|
|
|
updateValue(name, designDflt / factor)
|
2019-10-03 12:32:50 +02:00
|
|
|
}
|
2019-04-25 16:51:15 +02:00
|
|
|
|
2019-10-03 12:32:50 +02:00
|
|
|
const toggleExpanded = () => setExpanded(!expanded)
|
|
|
|
|
|
|
|
let unit = ''
|
2020-04-25 19:09:02 +02:00
|
|
|
if (type === 'pct') unit = '%'
|
|
|
|
if (type === 'deg') unit = '°'
|
2019-04-22 18:00:26 +02:00
|
|
|
|
2019-04-26 08:25:15 +02:00
|
|
|
let option = (
|
|
|
|
<FormFieldSlider
|
2020-04-25 19:09:02 +02:00
|
|
|
name={name}
|
|
|
|
value={val}
|
|
|
|
min={min}
|
|
|
|
max={max}
|
|
|
|
step={type === 'count' ? 1 : step}
|
2019-04-26 08:25:15 +02:00
|
|
|
onChange={update}
|
2020-04-25 19:09:02 +02:00
|
|
|
label={'po-' + type + '-' + name}
|
2019-04-26 08:25:15 +02:00
|
|
|
updateValue={update}
|
2019-04-30 16:20:43 +02:00
|
|
|
/>
|
2019-10-03 12:32:50 +02:00
|
|
|
)
|
2019-04-26 08:25:15 +02:00
|
|
|
|
2019-04-22 18:00:26 +02:00
|
|
|
return (
|
2019-04-25 16:51:15 +02:00
|
|
|
<li>
|
2019-04-22 18:00:26 +02:00
|
|
|
<OptionPreamble
|
2020-04-25 19:09:02 +02:00
|
|
|
dflt={dflt}
|
|
|
|
designDflt={designDflt}
|
|
|
|
value={val}
|
|
|
|
desc={desc}
|
|
|
|
title={title}
|
|
|
|
id={'po-' + type + '-' + name}
|
|
|
|
displayValue={val + unit}
|
2019-04-22 18:00:26 +02:00
|
|
|
reset={reset}
|
2019-10-03 12:32:50 +02:00
|
|
|
patternReset={patternReset}
|
2019-04-25 16:51:15 +02:00
|
|
|
toggleExpanded={toggleExpanded}
|
|
|
|
expanded={expanded}
|
2019-04-22 18:00:26 +02:00
|
|
|
showHelp={() =>
|
2020-04-25 19:09:02 +02:00
|
|
|
raiseEvent('showHelp', {
|
2019-10-03 12:32:50 +02:00
|
|
|
type: 'patternOption',
|
2020-04-25 19:09:02 +02:00
|
|
|
value: name
|
2019-04-22 18:00:26 +02:00
|
|
|
})
|
|
|
|
}
|
2019-04-26 08:25:15 +02:00
|
|
|
option={option}
|
2020-04-25 19:09:02 +02:00
|
|
|
noDocs={noDocs}
|
2019-04-22 18:00:26 +02:00
|
|
|
/>
|
2019-04-25 16:51:15 +02:00
|
|
|
</li>
|
2019-10-03 12:32:50 +02:00
|
|
|
)
|
|
|
|
}
|
2019-04-22 18:00:26 +02:00
|
|
|
|
2019-10-03 12:32:50 +02:00
|
|
|
export default PatternOptionPctDegCount
|