Guest User

Untitled

a guest
Oct 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import React from 'react';
  2. import styled from 'styled-components';
  3.  
  4. const InputGroup = styled.p`
  5. margin: 0 0 1em 0;
  6. position: relative;
  7. `;
  8.  
  9. const Label = styled.label`
  10. display: inline-block;
  11. position: absolute;
  12. top: -16px;
  13. left: 8px;
  14. color: #666;
  15. background-color: white;
  16. padding: 4px 10px;
  17. font-size: 14px;
  18. font-weight: 400;
  19. `;
  20.  
  21. const Input = styled.input`
  22. margin: 0;
  23. width: 100%;
  24. padding: 0.8em 0.5em;
  25. background: transparent;
  26. border: solid 1px #999;
  27. outline: none;
  28. `;
  29.  
  30. const TextArea = Input.withComponent('textarea').extend`
  31. resize: none;
  32. height: 10em;
  33. `;
  34.  
  35. const Submit = styled.button`
  36. display: block;
  37. width: 100%;
  38. padding: 0.3em 0;
  39. cursor: pointer;
  40. background: rgba(148, 186, 101, 0.7);
  41. box-shadow: 0 3px 0 0 rgba(123, 163, 73, 0.7);
  42. border-radius: 2px;
  43. border: none;
  44. color: white;
  45. font-size: 1.6em;
  46. line-height: 1.2em;
  47. `;
  48.  
  49. class ContactForm extends React.Component {
  50. constructor(props) {
  51. super(props);
  52. this.state = { name: '', email: '', phone: '', message: '' };
  53. }
  54.  
  55. encode = data => {
  56. return Object.keys(data)
  57. .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
  58. .join('&');
  59. };
  60.  
  61. handleSubmit = e => {
  62. fetch('/', {
  63. method: 'POST',
  64. headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  65. body: this.encode({ 'form-name': 'content', ...this.state })
  66. })
  67. .then(() => {
  68. alert('Success!');
  69. this.setState({ name: '', email: '', phone: '', message: '' });
  70. })
  71. .catch(error => console.log(error));
  72. e.preventDefault();
  73. };
  74.  
  75. handleChange = e => this.setState({ [e.target.name]: e.target.value });
  76.  
  77. render() {
  78. const { name, email, phone, message } = this.state;
  79.  
  80. return (
  81. <form onSubmit={this.handleSubmit} data-netlify="true">
  82. <InputGroup>
  83. <Label for="name">Name</Label>
  84. <Input type="text" name="name" value={name} onChange={this.handleChange} />
  85. </InputGroup>
  86. <InputGroup>
  87. <Label for="name">Email</Label>
  88. <Input type="email" name="email" value={email} onChange={this.handleChange} />
  89. </InputGroup>
  90. <InputGroup>
  91. <Label for="phone">Phone Number</Label>
  92. <Input type="tel" name="phone" value={phone} onChange={this.handleChange} />
  93. </InputGroup>
  94. <InputGroup>
  95. <Label for="name">Message</Label>
  96. <TextArea name="message" value={message} onChange={this.handleChange} />
  97. </InputGroup>
  98. <Submit type="submit">Send</Submit>
  99. </form>
  100. );
  101. }
  102. }
  103.  
  104. export default ContactForm;
Add Comment
Please, Sign In to add comment