Skip to content

A free and open-source block-based email template builder.

Fixing color picker by adding hex validation (#178)

cohitre68fa0529a287
diff --git a/.eslintrc.json b/.eslintrc.json
index 7b0295e..481fa3b 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -16,6 +16,7 @@
"@typescript-eslint/no-empty-interface": ["off"],
"@typescript-eslint/no-explicit-any": ["error"],
"@typescript-eslint/no-unused-vars": ["error"],
+ "curly": ["error", "all"],
"indent": ["off"],
"no-empty": ["error", { "allowEmptyCatch": true }],
"no-extra-boolean-cast": ["error", { "enforceForLogicalOperands": false }],
diff --git a/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/helpers/inputs/ColorInput/Picker.tsx b/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/helpers/inputs/ColorInput/Picker.tsx
index 37969ea..eeeddda 100644
--- a/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/helpers/inputs/ColorInput/Picker.tsx
+++ b/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/helpers/inputs/ColorInput/Picker.tsx
@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { useState } from 'react';
import { HexColorInput, HexColorPicker } from 'react-colorful';
import { Box, Stack, SxProps } from '@mui/material';
@@ -74,12 +74,20 @@ type Props = {
onChange: (v: string) => void;
};
export default function Picker({ value, onChange }: Props) {
+ const [internalValue, setInternalValue] = useState(value);
+ const handleChange = (v: string) => {
+ setInternalValue(v);
+ if (/^#[0-9a-fA-F]{6}$/.test(v)) {
+ onChange(v);
+ }
+ };
+
return (
<Stack spacing={1} sx={SX}>
- <HexColorPicker color={value} onChange={onChange} />
- <Swatch paletteColors={DEFAULT_PRESET_COLORS} value={value} onChange={onChange} />
+ <HexColorPicker color={value} onChange={handleChange} />
+ <Swatch paletteColors={DEFAULT_PRESET_COLORS} value={value} onChange={handleChange} />
<Box pt={1}>
- <HexColorInput prefixed color={value} onChange={onChange} />
+ <HexColorInput prefixed color={internalValue} onChange={handleChange} />
</Box>
</Stack>
);
diff --git a/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx b/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx
index ec485b3..ad58c2a 100644
--- a/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx
+++ b/packages/editor-sample/src/documents/blocks/helpers/block-wrappers/TuneMenu.tsx
@@ -99,13 +99,17 @@ export default function TuneMenu({ blockId }: Props) {
const handleDeleteClick = () => {
const filterChildrenIds = (childrenIds: string[] | null | undefined) => {
- if (!childrenIds) return childrenIds;
+ if (!childrenIds) {
+ return childrenIds;
+ }
return childrenIds.filter((f) => f !== blockId);
};
const nDocument: typeof document = { ...document };
for (const [id, b] of Object.entries(nDocument)) {
const block = b as TEditorBlock;
- if (id === blockId) continue;
+ if (id === blockId) {
+ continue;
+ }
switch (block.type) {
case 'EmailLayout':
@@ -153,9 +157,13 @@ export default function TuneMenu({ blockId }: Props) {
const handleMoveClick = (direction: 'up' | 'down') => {
const moveChildrenIds = (ids: string[] | null | undefined) => {
- if (!ids) return ids;
+ if (!ids) {
+ return ids;
+ }
const index = ids.indexOf(blockId);
- if (index < 0) return ids;
+ if (index < 0) {
+ return ids;
+ }
const childrenIds = [...ids];
if (direction === 'up' && index > 0) {
@@ -169,7 +177,9 @@ export default function TuneMenu({ blockId }: Props) {
const nDocument: typeof document = { ...document };
for (const [id, b] of Object.entries(nDocument)) {
const block = b as TEditorBlock;
- if (id === blockId) continue;
+ if (id === blockId) {
+ continue;
+ }
switch (block.type) {
case 'EmailLayout':