1
0
Fork 0
freesewing/sites/shared/components/workbench/menus/shared/values.mjs

62 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-06-22 10:52:02 -05:00
import { formatMm } from 'shared/utils.mjs'
2023-05-31 17:42:16 -05:00
/*********************************************************************************************************
* This file contains the base components to be used for displaying values in menu titles in the workbench
* Values that deal with more specific use cases should wrap one of the below components
*********************************************************************************************************/
/** The basis of it all. Handles the changed/unchanged styling for the wrapped value */
export const HighlightedValue = ({ changed, children }) => (
<span className={changed ? 'text-info' : ''}> {children} </span>
)
2023-05-31 17:42:16 -05:00
/**
* A wrapper for displaying the correct value based on whether or not the value has changed
* @param {Number|String|Boolean} options.current the current value, if it has been changed
* @param {Number|String|Boolean} options.dflt the default value
* @param {Boolean} options.changed has the value been changed?
*/
export const PlainValue = ({ current, dflt, changed }) => (
<HighlightedValue changed={changed}> {changed ? current : dflt} </HighlightedValue>
)
2023-05-31 17:42:16 -05:00
/**
* Displays the correct, translated value for a list
* @param {String|Boolean} options.current the current value, if it has been changed
* @param {Function} options.t a translation function
* @param {Object} options.config the item config
* @param {Boolean} options.changed has the value been changed?
*/
export const ListValue = ({ current, t, config, changed }) => {
2023-05-31 17:42:16 -05:00
// get the values
const val = changed ? current : config.dflt
2023-05-31 17:42:16 -05:00
// key will be based on a few factors
let key
2023-05-31 17:42:16 -05:00
// are valueTitles configured?
if (config.valueTitles) key = config.valueTitles[val]
2023-05-31 17:42:16 -05:00
// if not, is the value a string
else if (typeof val === 'string') key = val
2023-05-31 17:42:16 -05:00
// otherwise stringify booleans
else if (val) key = 'yes'
else key = 'no'
2023-05-31 17:42:16 -05:00
const translated = config.doNotTranslate ? key : t(key)
return <HighlightedValue changed={changed}>{translated}</HighlightedValue>
}
2023-05-31 17:42:16 -05:00
/** Displays the corrent, translated value for a boolean */
2023-05-29 23:22:26 -05:00
export const BoolValue = ListValue
2023-05-31 17:42:16 -05:00
/** Displays a formated mm value based on the current units */
2023-05-29 23:22:26 -05:00
export const MmValue = ({ current, config, units, changed }) => (
<HighlightedValue changed={changed}>
<span
dangerouslySetInnerHTML={{
__html: formatMm(changed ? current : config.dflt, units),
}}
/>
</HighlightedValue>
)