1
0
Fork 0
freesewing/packages/freesewing.shared/components/workbench/menu/design-options/option.js

130 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-01-29 18:24:36 +01:00
import { Chevron } from 'shared/components/navigation/primary.js'
2022-01-26 13:41:40 +01:00
import PctDegOption from 'shared/components/workbench/inputs/design-option-pct-deg'
2022-01-26 10:04:15 +01:00
import CountOption from 'shared/components/workbench/inputs/design-option-count'
import ListOption from 'shared/components/workbench/inputs/design-option-list'
import { formatMm, formatPercentage, optionType } from 'shared/utils.js'
2022-01-29 18:24:36 +01:00
import { Li, Ul, Details, Summary, SumButton, SumDiv, Deg } from 'shared/components/workbench/menu'
2022-02-08 20:49:19 +01:00
import { useTranslation } from 'next-i18next'
const values = {
pct: props => {
const val = (typeof props.gist?.options?.[props.option] === 'undefined')
? props.pattern.config.options[props.option].pct/100
: props.gist.options[props.option]
return (
<span className={
val=== props.pattern.config.options[props.option].pct/100
? 'text-secondary-focus'
: 'text-accent'
}>
{formatPercentage(val)}
{props.pattern.config.options[props.option]?.toAbs
? ' | ' +formatMm(props.pattern.config.options[props.option]?.toAbs(val, props.gist))
: null
}
</span>
)
2022-01-26 09:38:47 +01:00
},
bool: props => {
const { t } = useTranslation(['app'])
2022-01-26 09:38:47 +01:00
const dflt = props.pattern.config.options[props.option].bool
const current = props.gist?.options?.[props.option]
return (
<span className={
(dflt==current || typeof current === 'undefined')
? 'text-secondary-focus'
2022-01-26 09:38:47 +01:00
: 'text-accent'
}>
{props.gist?.options?.[props.option]
? t('yes')
: t('no')
2022-01-26 09:38:47 +01:00
}
</span>
)
},
count: props => {
2022-01-26 10:04:15 +01:00
const dflt = props.pattern.config.options[props.option].count
const current = props.gist?.options?.[props.option]
return (dflt==current || typeof current === 'undefined')
? <span className="text-secondary-focus">{dflt}</span>
2022-01-26 11:53:43 +01:00
: <span className="text-accent">{current}</span>
2022-01-26 09:38:47 +01:00
},
list: props => {
2022-01-26 11:53:43 +01:00
const dflt = props.pattern.config.options[props.option].dflt
const current = props.gist?.options?.[props.option]
const prefix = `${props.option}.o.`
2022-01-26 11:53:43 +01:00
return (dflt==current || typeof current === 'undefined')
? <span className="text-secondary-focus">{props.t(prefix+dflt)}</span>
2022-02-08 20:49:19 +01:00
: <span className="text-accent">{props.t(prefix+current)}</span>
2022-01-26 11:53:43 +01:00
},
deg: props => {
2022-01-26 13:41:40 +01:00
const dflt = props.pattern.config.options[props.option].deg
const current = props.gist?.options?.[props.option]
return (dflt==current || typeof current === 'undefined')
? <span className="text-secondary-focus">{dflt}&deg;</span>
2022-01-26 13:41:40 +01:00
: <span className="text-accent">{current}&deg;</span>
2022-01-26 09:38:47 +01:00
},
mm: props => {
2022-01-26 11:53:43 +01:00
return <p>No mm val yet</p>
2022-01-26 09:38:47 +01:00
},
constant: props => {
2022-01-26 11:53:43 +01:00
return <p>No constant val yet</p>
2022-01-26 09:38:47 +01:00
},
}
const Tmp = props => <p>not yet</p>
const inputs = {
2022-01-26 13:41:40 +01:00
pct: PctDegOption,
2022-01-26 10:04:15 +01:00
count: CountOption,
2022-01-26 13:41:40 +01:00
deg: props => <PctDegOption {...props} type='deg' />,
2022-01-26 10:04:15 +01:00
list: ListOption,
2022-01-26 13:41:40 +01:00
mm: <p>Mm options are not supported. Please report this.</p>,
constant: Tmp,
}
2022-01-25 12:39:29 +01:00
const Option = props => {
const { t } = useTranslation([`o_${props.pattern.config.name}`])
const type = optionType(props.pattern.config.options[props.option])
const Input = inputs[type]
const Value = values[type]
2022-01-26 09:38:47 +01:00
const toggleBoolean = () => {
const dflt = props.pattern.config.options[props.option].bool
const current = props.gist?.options?.[props.option]
if (typeof current === 'undefined')
props.updateGist(['options', props.option], !dflt)
else props.unsetGist(['options', props.option])
}
2022-01-29 18:24:36 +01:00
return (type === 'bool')
? (
<Li>
<SumButton onClick={toggleBoolean}>
<SumDiv>
<Deg />
<span>{t(`${props.option}.t`) }</span>
2022-01-29 18:24:36 +01:00
</SumDiv>
2022-02-08 20:49:19 +01:00
<Value type={type} {...props} t={t} />
2022-01-29 18:24:36 +01:00
</SumButton>
</Li>
) : (
<Li>
<Details>
<Summary>
<SumDiv>
<Deg />
<span>{t(`${props.option}.t`)}</span>
2022-01-29 18:24:36 +01:00
</SumDiv>
2022-02-08 20:49:19 +01:00
<Value type={type} {...props} t={t} />
2022-01-29 18:24:36 +01:00
<Chevron w={6} m={3}/>
</Summary>
<Input {...props} ot={t} />
2022-01-29 18:24:36 +01:00
</Details>
</Li>
)
2022-01-25 12:39:29 +01:00
}
export default Option