Skip to content

Commit

Permalink
feat: improved inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Opty1712 committed Feb 9, 2023
1 parent c97dbb6 commit 492ef78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/components/PageColorizer/PageColorizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ export const PageColorizer: FC<PageColorizerProps> = ({ children }) => {
<RgbColorPicker color={selectedColor} onChange={setSelectedColor} />

<Inputs>
{getKeys(selectedColor).map((key) => (
<Input
key={key}
value={selectedColor[key]}
onChange={getOnInputChangeHandler(key)}
/>
))}
{getKeys(selectedColor).map((key) => {
return (
<Input
key={key}
type="number"
min={0}
max={255}
value={Number(selectedColor[key])}
onChange={getOnInputChangeHandler(key)}
/>
);
})}
</Inputs>

<Buttons>
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageColorizer/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const Input = styled.input`
height: 50px;
width: 50px;
text-align: center;
font-size: 30px;
font-size: 20px;
border-color: #eee;
`;

Expand Down
12 changes: 11 additions & 1 deletion src/components/PageColorizer/useColorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export const useColorizer = () => {

const getOnInputChangeHandler =
(part: keyof RgbColor) => (event: ChangeEvent<HTMLInputElement>) => {
setSelectedColor((value) => ({ ...value, [part]: event.target.value }));
const incomingValue = event.target.value;
const newPartValue = Number(incomingValue.replace(onlyDigits, ''));

setSelectedColor((value) => ({
...value,
[part]: newPartValue > maxValue ? maxValue : newPartValue
}));
};

return {
Expand All @@ -40,3 +46,7 @@ const initialColor: RgbColor = {
g: 255,
b: 255
};

const maxValue = 255;

const onlyDigits = /[^\d]/g;

1 comment on commit 492ef78

@vercel
Copy link

@vercel vercel bot commented on 492ef78 Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

near-frontend – ./

near-frontend-opty1712.vercel.app
near-frontend-blue.vercel.app
near-frontend-git-main-opty1712.vercel.app

Please sign in to comment.