aboutsummaryrefslogtreecommitdiffstats
path: root/thallium-frontend/src/components/forms/TextInput/index.tsx
blob: a92fcec5de98dd3e41f182cdabfc4d40ddc793cb (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
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
import styled from "styled-components";
import { InputScale } from "./options";

import Button from "../Button";


interface TextInputProps {
    label: string;
    type: string;
    value: string;
    placeholder?: string;
    scale?: InputScale;
    onChange: (value: string) => void;
    onSubmit?: () => void | Promise<void>;
    submitLabel?: string;
}

const Input = styled.input<{ $scale: InputScale }>`
    padding: 0.5rem;
    margin-top: 0.5rem;
    font-family: inherit;
    border-radius: 0px;
    color: ${({ theme }) => theme.textColor};
    background-color: ${({ theme }) => theme.inputBackgroundColor};
    border: none;
    width: ${(props) => props.$scale};
    height: 1.5rem;
    transition: outline 0.2s;
    outline: 2px solid transparent;

    &::placeholder {
        color: ${({ theme }) => theme.inputPlaceholderColor};
    }

    &:focus {
        outline: 2px solid ${({ theme }) => theme.accent};
        border: none;
    }
`;

const InputContainer = styled.div`
    label {
        display: block;
        font-size: 0.8rem;
        font-weight: bold;
    }

    &:focus-within button {
        outline: 2px solid ${({ theme }) => theme.accent};
    }
`;


const TextInput = ({ label, type, value, placeholder, scale, onChange, onSubmit, submitLabel }: TextInputProps) => {
    if (!scale) {
        scale = InputScale.Medium;
    }

    return (
        <InputContainer>
            <label>{label}</label>
            <Input
                type={type}
                value={value}
                $scale={scale}
                onChange={(e) => { onChange(e.target.value); }}
                placeholder={placeholder}
                onKeyDown={(e) => {
                    if (e.key === "Enter" && onSubmit) {
                        void onSubmit();
                    }
                }}
            />
            {onSubmit && <Button
                onClick={() => {
                    void onSubmit();
                }}
            >
                {submitLabel ? submitLabel : "Submit"}
            </Button>}
        </InputContainer>
    );
};

export default TextInput;