All files / src/shared/HFInputs/HFRichInput Alignment.tsx

55.55% Statements 5/9
55.55% Branches 5/9
50% Functions 1/2
55.55% Lines 5/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                              45x           45x 1662x   1662x                                   1662x                        
import { getSelectedBlocksMetadata, setBlockData } from "draftjs-utils";
import IconWrapper from "./IconWrapper";
import { EditorState } from "react-draft-wysiwyg-next";
import {
  TextAlignCenterIcon,
  TextAlignLeftIcon,
  TextAlignRightIcon,
} from "@phosphor-icons/react";
 
interface Props {
  onChange?: (vals: any) => void;
  editorState: EditorState;
  value: "left" | "center" | "right";
}
 
const IconsMap = {
  left: TextAlignLeftIcon,
  center: TextAlignCenterIcon,
  right: TextAlignRightIcon,
};
 
export const Alignment = ({ onChange, editorState, value }: Props) => {
  const state = getSelectedBlocksMetadata(editorState).get("text-align");
 
  const onClick = () => {
    if (typeof onChange === "function") {
      if (state === value) {
        onChange(
          setBlockData(editorState, {
            "text-align": "left",
          }),
        );
      } else {
        onChange(
          setBlockData(editorState, {
            "text-align": value,
          }),
        );
      }
    }
  };
 
  return (
    <IconWrapper
      icon={IconsMap[value]}
      onClick={onClick}
      state={
        value === state || (value === "left" && state === undefined)
          ? "active"
          : "default"
      }
    />
  );
};