1
0
Fork 0
freesewing/packages/freesewing.shared/components/workbench/inputs/design-option-pct-deg.js

129 lines
4 KiB
JavaScript
Raw Normal View History

import { useState } from 'react'
import ClearIcon from 'shared/components/icons/clear.js'
import EditIcon from 'shared/components/icons/edit.js'
import { formatMm, round } from 'shared/utils.js'
2022-02-08 20:49:19 +01:00
import { useTranslation } from 'next-i18next'
2022-01-26 13:41:40 +01:00
const EditOption = props => (
<div className="form-control mb-2 w-full">
<label className="label">
2022-01-26 13:41:40 +01:00
<span className="label-text text-neutral-content">{props.min}{props.suffix}</span>
<span className="label-text font-bold text-neutral-content">{props.value}{props.suffix}</span>
<span className="label-text text-neutral-content">{props.max}{props.suffix}</span>
</label>
<label className="input-group input-group-sm">
<input
type="number"
className={`
input input-sm input-bordered grow text-base-content
`}
value={props.value}
onChange={props.handleChange}
/>
2022-01-26 13:41:40 +01:00
<span className="text-neutral-content font-bold">{props.suffix}</span>
</label>
</div>
)
2022-01-26 13:41:40 +01:00
const DesignOptionPctDeg = props => {
2022-02-08 20:49:19 +01:00
const { t } = useTranslation(['app'])
2022-01-26 13:41:40 +01:00
const suffix = props.type === 'deg' ? '°' : '%'
const factor = props.type === 'deg' ? 1 : 100
const { max, min } = props.pattern.config.options[props.option]
const dflt = props.pattern.config.options[props.option][props.type || 'pct']
const val = (typeof props.gist?.options?.[props.option] === 'undefined')
2022-01-26 13:41:40 +01:00
? dflt
: props.gist.options[props.option] * factor
const [value, setValue] = useState(val)
2022-01-26 13:41:40 +01:00
const [editOption, setEditOption] = useState(false)
const handleChange = (evt) => {
const newVal = evt.target.value
setValue(newVal)
2022-01-26 13:41:40 +01:00
props.updateGist(['options', props.option], newVal/factor)
}
const reset = () => {
2022-01-26 13:41:40 +01:00
setValue(dflt)
props.unsetGist(['options', props.option])
}
return (
<div className="py-4 mx-6 border-l-2 pl-2">
2022-01-27 15:18:32 +01:00
<p className="m-0 p-0 px-2 mb-2 text-neutral-content opacity-60 italic">
{props.ot(`${props.option}.d`)}
2022-01-27 15:18:32 +01:00
</p>
<div className="flex flex-row justify-between">
2022-01-26 13:41:40 +01:00
{editOption
? <EditOption
value={value}
handleChange={handleChange}
min={min}
max={max}
2022-01-26 13:41:40 +01:00
setEditOption={setEditOption}
2022-02-08 20:49:19 +01:00
t={t}
2022-01-26 13:41:40 +01:00
suffix={suffix}
/>
: (
<>
2022-01-26 13:41:40 +01:00
<span className="opacity-50">{round(min)}{suffix}</span>
<span className={
2022-01-26 13:41:40 +01:00
`font-bold ${val===dflt ? 'text-secondary' : 'text-accent'}`}
>
2022-01-26 13:41:40 +01:00
{round(val)}{suffix}
</span>
2022-01-26 13:41:40 +01:00
<span className="opacity-50">{round(max)}{suffix}</span>
</>
)
}
</div>
<input
type="range"
max={max}
min={min}
step={0.1}
value={value}
onChange={handleChange}
className={`
range range-sm mt-1
2022-01-26 13:41:40 +01:00
${val === dflt ? 'range-secondary' : 'range-accent'}
`}
/>
<div className="flex flex-row justify-between">
2022-01-26 13:41:40 +01:00
<span className={val===dflt ? 'text-secondary' : 'text-accent'}>
{props.pattern.config.options[props.option]?.toAbs
? formatMm(props.pattern.config.options[props.option].toAbs(value/100, props.gist))
: ' '
}
</span>
<div>
<button
2022-02-08 20:49:19 +01:00
title={t('reset')}
className="btn btn-ghost btn-xs text-accent"
2022-01-26 13:41:40 +01:00
disabled={val === dflt}
onClick={reset}
>
<ClearIcon />
</button>
<button
2022-02-08 20:49:19 +01:00
title={t('editThing', { thing: suffix })}
className={`
btn btn-ghost btn-xs hover:text-secondary-focus
2022-01-26 13:41:40 +01:00
${editOption
? 'text-accent'
: 'text-secondary'
}
`}
2022-01-26 13:41:40 +01:00
onClick={() => setEditOption(!editOption)}
>
<EditIcon />
</button>
</div>
</div>
</div>
)
}
2022-01-26 13:41:40 +01:00
export default DesignOptionPctDeg