summary refs log tree commit diff
path: root/client/src/Components/Form.js
blob: 4d10f98e99a3a94b2f4b67a019f9f7f4cbe8c20e (plain)
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
import React, {useState} from 'react';
import Button from './Button';
import isURL from "validator/lib/isURL";

function Form(props) {
    const [state, setState] = useState({value: '', valid: false});

    const handleSubmit = () => {
        if (state.valid) {
            props.addRequest(state.value);
        }
    };

    const handleChange = (e) => {
        const userInput = e.target.value;
        const valid = isURL('https://' + userInput);
        setState({value: userInput, valid: valid});
    };

    return (
        <form id="form" onSubmit={(e) => e.preventDefault()}>
            <div className={"input-group" + (state.valid ? "" : " disabled")}>
                <div className={"input-container" + (state.valid ? "" : " border-r-none")}>
                    <span className="input-field-text">https://</span>
                    <input className="input-field" required onChange={handleChange}/>
                </div>
                <Button valid={state.valid} submit={handleSubmit}/>
            </div>
        </form>
    )
}

export default Form;