aboutsummaryrefslogtreecommitdiff
path: root/src/settings/components/ui/TextArea.tsx
blob: 9dcd5be2a73db75d51ac3e7e6151ab1e00b5383d (plain) (blame)
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
import React from "react";
import styled from "styled-components";

const Container = styled.div`
  page-break-inside: avoid;
`;

const Label = styled.label`
  font-weight: bold;
  min-width: 14rem;
  display: inline-block;
`;

const ErrorableTextArea = styled.textarea<{ hasError: boolean }>`
  box-shadow: ${({ hasError }) => (hasError ? "0 0 2px red" : "none")};
  font-family: monospace;
  font-family: monospace;
  width: 100%;
  min-height: 64ex;
  resize: vertical;
`;

const ErrorMessage = styled.p`
  font-weight: bold;
  color: red;
  min-height: 1.5em;
`;

interface Props extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
  error?: string;
  label: string;
  onValueChange?: (name: string, value: string) => void;
}

const TextArea: React.FC<Props> = (props) => {
  const onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
    if (props.onValueChange) {
      props.onValueChange(e.target.name, e.target.value);
    }
  };

  const hasError = typeof props.error !== "undefined" && props.error !== "";
  const pp = { ...props };
  delete pp.onValueChange;
  return (
    <Container>
      <Label htmlFor={props.id}>{props.label}</Label>
      <ErrorableTextArea hasError={hasError} onChange={onChange} {...pp} />
      {hasError ? (
        <ErrorMessage role="alert">{props.error}</ErrorMessage>
      ) : null}
    </Container>
  );
};

export default TextArea;