All files / src/components/ui/form RenameInput.tsx

0% Statements 0/27
0% Branches 0/15
0% Functions 0/9
0% Lines 0/26

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                                                                                                                                                                                                           
import React, { useCallback, useEffect, useState } from "react";
import l10n from "shared/lib/lang/l10n";
import styled from "styled-components";
import { IMEUnstyledInput } from "ui/form/IMEInput";
import { CheckIcon } from "ui/icons/Icons";
 
export const Input = styled(IMEUnstyledInput)`
  background: ${(props) => props.theme.colors.input.background};
  color: ${(props) => props.theme.colors.input.text};
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-grow: 1;
  border: 0;
  font-size: 11px;
  margin-left: -5px;
  padding-left: 5px;
  margin-right: -18px;
  border-radius: 4px;
`;
 
export const CompleteButton = styled.button`
  z-index: 10000;
  position: relative;
  top: 0px;
  left: -4px;
  width: 21px;
  height: 100%;
  border: 0;
  border-radius: ${(props) => Math.max(0, props.theme.borderRadius - 1)}px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  line-height: 10px;
  font-size: 12px;
  font-weight: bold;
  background: transparent;
  border-color: transparent;
  &:hover {
    background: rgba(128, 128, 128, 0.3);
  }
  &:active {
    background: rgba(128, 128, 128, 0.4);
  }
  svg {
    width: 12px;
    height: 12px;
    fill: ${(props) => props.theme.colors.input.text};
  }
`;
 
interface RenameInputProps {
  autoFocus?: boolean;
  value?: string;
  onRenameComplete: (newValue: string) => void;
  onRenameCancel: () => void;
}
 
export const RenameInput = ({
  autoFocus,
  value,
  onRenameComplete,
  onRenameCancel,
}: RenameInputProps) => {
  const [name, setName] = useState(value ?? "");
 
  const onRenameChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      setName(e.currentTarget.value);
    },
    []
  );
 
  const onRenameKeyDown = useCallback(
    (e: React.KeyboardEvent<HTMLInputElement>) => {
      if (e.key === "Escape") {
        onRenameCancel();
        setName(value ?? "");
      } else Iif (e.key === "Enter") {
        onRenameComplete(name);
      }
    },
    [name, onRenameCancel, onRenameComplete, value]
  );
 
  const onRenameBlur = useCallback(() => {
    onRenameComplete(name);
  }, [name, onRenameComplete]);
 
  useEffect(() => {
    setName(value ?? "");
  }, [value]);
 
  return (
    <>
      <Input
        autoFocus={autoFocus}
        value={name}
        onChange={onRenameChange}
        onKeyDown={onRenameKeyDown}
        onBlur={onRenameBlur}
      />
      <CompleteButton onClick={onRenameBlur} title={l10n("FIELD_RENAME")}>
        <CheckIcon />
      </CompleteButton>
    </>
  );
};