Skip to content

Commit

Permalink
color picker behavior test
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-smith-tcril committed Oct 19, 2023
1 parent 309ee18 commit d677d80
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions src/ColorPicker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,59 @@ import { OverlayTrigger } from '../Overlay';
import Tooltip from '../Tooltip';
import useToggle from '../hooks/useToggle';

const colorIsValid = (colorToValidate) => {
const hexRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
return hexRegex.test(colorToValidate);
}

Check failure on line 16 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Missing semicolon

const formatHexColorString = (colorString) => {
if (!colorString.startsWith('#')) {
colorString = `#${colorString}`;

Check failure on line 20 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Assignment to function parameter 'colorString'
}
return colorString.slice(0,7);

Check failure on line 22 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

A space is required after ','
}

Check failure on line 23 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Missing semicolon

function ColorPicker({
color, setColor, className, size,
}) {
const [isOpen, open, close] = useToggle(false);
const [target, setTarget] = React.useState(null);
const [hexValid, setHexValid] = React.useState(true);
const [hexValid, setHexValid] = React.useState(() => (color === '' || colorIsValid(formatHexColorString(color))));
const [hexColorString, setHexColorString] = React.useState(() => formatHexColorString(color));
const [colorToDisplay, setColorToDisplay] = React.useState(() => {
const formattedColor = formatHexColorString(color);
if (colorIsValid(formattedColor)) {
return formattedColor;
}

const validateHex = useCallback((input) => {
const hexRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
if (input.length > 1 && !input.startsWith('#')) {
setColor(`#${input}`);
} else {
setColor(input);
return '#fff';
});

const setValidatedColor = (newColor) => {
if (newColor === '') {
setHexValid(true);
setColor('');
setHexColorString('');
setColorToDisplay('#fff');
return;
}
if (input === '' || hexRegex.test(input) === true) {

newColor = formatHexColorString(newColor);

Check failure on line 50 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Assignment to function parameter 'newColor'

if (colorIsValid(newColor)) {
setHexValid(true);
} else {
setHexValid(false);
setColor(newColor);
setHexColorString(newColor);
setColorToDisplay(newColor);
return;
}
}, [setColor]);

// this is needed for when a user changes the color through the sliders
useEffect(() => validateHex(color), [validateHex, color]);
setHexValid(false);
setHexColorString(newColor);

Check failure on line 62 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
// not sure if we want to do this, but if we don't then the value of the picker could be out of sync from what is in the textbox

Check failure on line 63 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

This line has a length of 134. Maximum allowed is 120

Check failure on line 63 in src/ColorPicker/index.jsx

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
setColor(newColor);
}

return (
<>
Expand Down Expand Up @@ -65,16 +95,17 @@ function ColorPicker({
className="pgn__color-modal rounded shadow"
style={{ textAlign: 'start' }}
>
<HexColorPicker color={color || ''} onChange={setColor} />
<HexColorPicker color={colorToDisplay} onChange={setValidatedColor} />
<Form.Group className="pgn__hex-form" size="sm">
<div>
<Form.Label className="pgn__hex-label">Hex</Form.Label>
<Form.Control
className="pgn__hex-field"
isInvalid={!hexValid}
value={color}
onChange={(e) => validateHex(e.target.value)}
value={hexColorString}
onChange={(e) => setValidatedColor(e.target.value)}
data-testid="hex-input"
spellCheck="false"
/>
</div>
{!hexValid && (
Expand Down

0 comments on commit d677d80

Please sign in to comment.