generated from goitacademy/react-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): implement contact management with addition, search, and de…
…letion functionalities
- Loading branch information
Showing
8 changed files
with
224 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Component } from 'react'; | ||
import { nanoid } from 'nanoid'; | ||
|
||
export class ContactForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
name: '', | ||
number: '', | ||
}; | ||
|
||
this.handleChange = this.handleChange.bind(this); | ||
this.handleFormSubmit = this.handleFormSubmit.bind(this); | ||
} | ||
|
||
handleChange = e => { | ||
this.setState({ [e.target.name]: e.target.value }); | ||
}; | ||
|
||
handleFormSubmit = e => { | ||
e.preventDefault(); | ||
|
||
const { name, number } = this.state; | ||
const { contacts, addContact } = this.props; | ||
|
||
if (contacts.some(contact => contact.name === name)) { | ||
alert(`Contact with name "${name}" already exists!`); | ||
return; | ||
} | ||
|
||
const newContact = { | ||
id: nanoid(), | ||
name, | ||
number, | ||
}; | ||
|
||
addContact(newContact); | ||
|
||
this.setState({ name: '', number: '' }); | ||
}; | ||
|
||
render() { | ||
const { name, number } = this.state; | ||
|
||
return ( | ||
<form onSubmit={this.handleFormSubmit}> | ||
<label> | ||
Name: | ||
<input | ||
type="text" | ||
name="name" | ||
value={name} | ||
onChange={this.handleChange} | ||
pattern="^[a-zA-Zа-яА-Я]+(([' -][a-zA-Zа-яА-Я ])?[a-zA-Zа-яА-Я]*)*$" | ||
title="Name may contain only letters, apostrophe, dash and spaces. For example Adrian, Jacob Mercer, Charles de Batz de Castelmore d'Artagnan" | ||
required | ||
/> | ||
</label> | ||
<label> | ||
Phone Number: | ||
<input | ||
type="tel" | ||
name="number" | ||
value={number} | ||
onChange={this.handleChange} | ||
pattern="\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}" | ||
title="Phone number must be digits and can contain spaces, dashes, parentheses and can start with +" | ||
required | ||
/> | ||
</label> | ||
<button type="submit">Add Contact</button> | ||
</form> | ||
); | ||
} | ||
} |
Oops, something went wrong.