Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 23 additions & 41 deletions src/components/playground/FontWeightInput.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { RestartAlt } from '@mui/icons-material';
import {
Box,
InputAdornment,
CircularProgress,
InputLabel,
Tooltip,
FormControl,
Input,
FormHelperText,
Select,
MenuItem,
} from '@mui/material';
import { useState, useEffect, useCallback, memo } from 'react';
import {
usePlaygroundUtils,
usePlaygroundFonts,
usePlaygroundTheme,
} from 'hooks/contextHooks';
import { usePlaygroundUtils, usePlaygroundFonts, usePlaygroundTheme } from 'hooks/contextHooks';
import { useDefaultValue } from 'hooks/cwmHooks';
import {
propRules,
fontWeightNameToValue,
} from 'models/themePlaygroundOptions';
import { loadFonts } from 'models/utils';

function FontWeightInput({ prop, pathToProp, propName }) {
Expand All @@ -46,21 +37,7 @@ function FontWeightInput({ prop, pathToProp, propName }) {
return setError('Please add a font first');
}

// handle case of not a convertable string weight
// 'bolder', 'lighter', 'inherit' ....
if (!(userInput in fontWeightNameToValue)) {
setPropByPath(`${pathToProp}.${propName}`, userInput);
return true;
}

const isWeightValueNumber = Number.isNaN(Number(userInput)) === false;

// convert the string weight to an number
// 'bold' & 'normal'
let weightToLoad = userInput;
if (isWeightValueNumber === false) {
weightToLoad = fontWeightNameToValue[userInput];
}
const weightToLoad = userInput;

setLoading(true);
loadFonts([`${fontFamily}:${weightToLoad}`]).then((fontLoaded) => {
Expand Down Expand Up @@ -89,12 +66,7 @@ function FontWeightInput({ prop, pathToProp, propName }) {
const handleChange = (e) => {
const userInput = e.target.value;
setValue(userInput);
if (!propRules[propName].test(userInput)) {
setError(`Invalid ${prop.displayText}`);
return;
}

e.target.blur();
loadWeight(userInput);
setError('');
};
Expand All @@ -115,15 +87,25 @@ function FontWeightInput({ prop, pathToProp, propName }) {
</Tooltip>
</Box>
</InputLabel>
<Input
value={value}
onChange={handleChange}
endAdornment={
<InputAdornment position="end">
{loading ? <CircularProgress size="1.5rem" /> : null}
</InputAdornment>
}
/>
<Select value={value} onChange={handleChange}>
{prop.options?.map((data) => (
<MenuItem
value={data.value}
key={`fw-selector-menuitem-${data.value}`}
>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
flex: 1,
}}
>
<span>{data.label}</span>
</Box>
</MenuItem>
))}
</Select>
<FormHelperText>{error || `Load ${prop.displayText}.`}</FormHelperText>
</FormControl>
);
Expand Down