simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 18 | import { SearchRounded } from '@mui/icons-material'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import { InputAdornment, InputBase } from '@mui/material'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 20 | import { ChangeEvent, FormEvent, useState } from 'react'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 21 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 22 | type NewContactFormProps = { |
| 23 | onChange?: (v: string) => void; |
| 24 | onSubmit?: (v: string) => void; |
| 25 | }; |
| 26 | |
| 27 | export default function NewContactForm(props: NewContactFormProps) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 28 | const [value, setValue] = useState(''); |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 29 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 30 | const handleChange = (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 31 | setValue(event.target.value); |
| 32 | if (props.onChange) props.onChange(event.target.value); |
| 33 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 34 | |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 35 | const handleSubmit = (event: FormEvent<HTMLFormElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 36 | event.preventDefault(); |
| 37 | if (value && props.onSubmit) props.onSubmit(value); |
| 38 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 39 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 40 | return ( |
| 41 | <form className="main-search" onSubmit={handleSubmit} noValidate autoComplete="off"> |
| 42 | <InputBase |
| 43 | className="main-search-input" |
| 44 | type="search" |
| 45 | placeholder="Ajouter un contact" |
| 46 | value={value} |
| 47 | onChange={handleChange} |
| 48 | startAdornment={ |
| 49 | <InputAdornment position="start"> |
| 50 | <SearchRounded /> |
| 51 | </InputAdornment> |
| 52 | } |
| 53 | /> |
| 54 | </form> |
| 55 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 56 | } |