aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/Inputs.js
blob: a9b08b77e3b551f53c9949c8516610cc2a3eefc2 (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React from 'react';
import CharLimit from './decorators/CharLimit'
import styled from 'styled-components'
import FloatingLabel from './decorators/FloatingLabel'
import Dropdown from 'react-dropdown';
import { LANGS, THEMES } from './renderers/Code';

const RelPositioning = styled.div`
    position: relative; 
    height: calc(100% - 4em);
`

const FlexChild = styled.div`
    display: block;
    margin-left: 2em;
`

const TitleInput = (props) => {
    return (
        <RelPositioning>
            <FloatingLabel
                label="title"
                id={props.id}
                value={props.value} />
            <input
                name="title"
                readOnly={props.readOnly}
                className="lt-shadow"
                placeholder="Title"
                id={props.id}
                type="text"
                autoFocus
                autoComplete="off"
                onChange={props.onChange}
                value={props.value} />
            <CharLimit
                content={props.value}
                maxLength={props.maxLength} />
        </RelPositioning>
    );
}

const PasteInput = (props) => {
    function handleKeyDown(e) {
        if (e.keyCode === 9) { // tab was pressed

            // prevent autofocus on next intput
            e.preventDefault();

            // get selection start and end
            const start = e.target.selectionStart
            const end = e.target.selectionEnd

            props.insertTabCallback(start, end)

            // set cursor position to be at start
            e.target.selectionEnd = end + 4;
        }
    }

    return (
        <RelPositioning>
            <FloatingLabel
                label="content"
                id={props.id}
                value={props.content} />
            <textarea
                name="content"
                readOnly={props.readOnly}
                placeholder="Paste your text here"
                value={props.content}
                id={props.id}
                required
                onChange={props.onChange}
                onKeyDown={handleKeyDown}
                className="lt-shadow" />
            <CharLimit
                content={props.content}
                maxLength={props.maxLength} />
        </RelPositioning>
    );
}

const PassInput = (props) => {
    return (
        <FlexChild>
            <RelPositioning>
                <FloatingLabel
                    label="password"
                    id={props.id}
                    value={props.value} />
                <input
                    name="pass"
                    className="lt-shadow"
                    placeholder="password"
                    type="password"
                    autoComplete="off"
                    onChange={props.onChange}
                    value={props.value}
                    id={props.id} />
            </RelPositioning>
        </FlexChild>
    );
}

const GenericDropdown = (props) => {
    function _onSelect(option) {
        props.onChange({
            target: {
                name: props.label,
                value: option.label
            }
        });
    }

    return (
        <FlexChild>
            <Dropdown
                options={props.options}
                onChange={_onSelect}
                callBackRef={props.onChange}
                value={props.value}
                placeholder={props.placeholder}
                id={props.id} />
            <FloatingLabel
                label={props.label}
                id={props.id}
                value={props.value} />
        </FlexChild>
    );
}

const ExpiryInput = (props) => {
    const options = [
        '5 years',
        '1 year',
        '1 month',
        '1 week',
        '1 day',
        '1 hour',
        '10 min',
    ];

    return (
        <GenericDropdown 
            {...props}
            options={options}
            placeholder='1 week'
            label='expiry'
        />
    );
}

const LangInput = (props) => {
    const options = Object.entries(LANGS).map((key, _) => {
        return {
            'value': key[1],
            'label': key[0]
        }
    })

    return (
        <GenericDropdown
            {...props}
            options={options}
            placeholder={LANGS.raw}
            label='language'
        />
    );
}

const ThemeInput = (props) => {
    const options = Object.entries(THEMES).map((key, _) => {
        return {
            'value': key[1],
            'label': key[0]
        }
    })

    return (
        <GenericDropdown
            {...props}
            options={options}
            placeholder={'atom'}
            label='theme'
        />
    );
}

const PasteURLInput = ({id, fullURL}) => {
    return (
        <FlexChild>
            <RelPositioning>
                <FloatingLabel
                    label="url"
                    id={id}
                    value={id} />
                <input
                    name="paste_url"
                    className="lt-shadow"
                    type="text"
                    readOnly
                    autoFocus
                    id={id}
                    value={fullURL} />
            </RelPositioning>
        </FlexChild>
    );
}

export { TitleInput, PasteInput, PassInput, ExpiryInput, PasteURLInput, LangInput, ThemeInput }