add "reset to default" color option for ColorPicker (#8643)

* add reset to default color option

* Do not show reset button in DOM if resetDefaultValue is undefined

* handle edge case: if invalid hexcode is set for resetDefaultValue

* Do not show the reset button if the color is already set to the default value

* Remove data-testid from Button

---------

Co-authored-by: Praneeth Bhogaraju <[email protected]>
Co-authored-by: sean-brydon <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-05-05 13:25:18 +01:00
1 parent 76807a7e70
commit 371e7b024a
3 files changed
+28

No files matched your search

@@ -4,6 +4,8 @@ import { HexColorInput, HexColorPicker } from "react-colorful";
import cx from "@calcom/lib/classNames";
import { fallBackHex, isValidHexCode } from "@calcom/lib/getBrandColours";
import { Button } from "@calcom/ui";
import { RotateCcw } from "@calcom/ui/components/icon";
export type ColorPickerProps = {
defaultValue: string;
@@ -11,12 +13,18 @@ export type ColorPickerProps = {
container?: HTMLElement;
popoverAlign?: React.ComponentProps<typeof Popover.Content>["align"];
className?: string;
resetDefaultValue?: string;
};
const ColorPicker = (props: ColorPickerProps) => {
const init = !isValidHexCode(props.defaultValue)
? fallBackHex(props.defaultValue, false)
: props.defaultValue;
const resetDefaultValue =
props.resetDefaultValue &&
(!isValidHexCode(props.resetDefaultValue)
? fallBackHex(props.resetDefaultValue, false)
: props.resetDefaultValue);
const [color, setColor] = useState(init);
return (
@@ -57,6 +65,22 @@ const ColorPicker = (props: ColorPickerProps) => {
}}
type="text"
/>
{resetDefaultValue && color != resetDefaultValue && (
<div className="px-1">
<Button
color={resetDefaultValue == "#292929" ? "primary" : "secondary"}
target="_blank"
variant="icon"
rel="noreferrer"
StartIcon={RotateCcw}
tooltip="Reset to default"
onClick={() => {
setColor(fallBackHex(resetDefaultValue, false));
props.onChange(resetDefaultValue);
}}
/>
</div>
)}
</div>
);
};