Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. readItem = (id) => {
  2. const readItem = this.state.auto.filter((el) => el.id === id)
  3. this.setState({readItem})
  4. }
  5.  
  6. <Header readItem={this.state.readItem}/>
  7.  
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. id: this.props.readItem.id,
  12. name: this.props.readItem.name,
  13. marka: this.props.readItem.marka,
  14. country: this.props.readItem.country,
  15. price: this.props.readItem.price,
  16. url: this.props.readItem.url
  17. }
  18. }
  19.  
  20. <input
  21. name="name"
  22. value={name}
  23. onChange={this.inputName}
  24. placeholder="Название" />
  25.  
  26. import React, { Component } from 'react';
  27. import './App.css';
  28. import Header from './component/Header/Header'
  29. import ListAuto from './component/ListAuto/ListAuto'
  30. import EditItems from './component/Edit/EditItems'
  31.  
  32. class App extends Component {
  33. state = {
  34. auto: [
  35. {
  36. "id": 1,
  37. "name": "Авто №1",
  38. "marka": "Марка машины №1",
  39. "price": 50,
  40. "country": "Russia",
  41. "url": "https://finance.liga.net/images/general/2018/08/29/thumbnail-tw-20180829113845-1826.jpg"
  42. },
  43. {
  44. "id": 2,
  45. "name": "Авто №2",
  46. "marka": "Марка машины №2",
  47. "price": 20,
  48. "country": "Russia",
  49. "url": "https://icdn.lenta.ru/images/2017/08/23/14/20170823145604779/pic_b21fd6a6551e45ce4a397c3c2662ec8f.jpg"
  50. },
  51. ],
  52. readItem: [{
  53. "id": null,
  54. "name": null,
  55. "marka": null,
  56. "price": null,
  57. "country": null,
  58. "url": null
  59. }]
  60. }
  61.  
  62. addItem = (item) => {
  63. const addAuto = {
  64. id: this.state.auto.length + 1,
  65. name: item.name,
  66. marka: item.marka,
  67. price: item.price,
  68. country: item.country,
  69. url: item.url
  70. }
  71. this.setState(({ auto }) => {
  72. const NewArray = [
  73. ...auto,
  74. addAuto
  75. ]
  76. return {
  77. auto: NewArray
  78. }
  79.  
  80.  
  81. })
  82. }
  83. delItem = (id) => {
  84. const NewArr = this.state.auto.filter((el) => el.id !== id)
  85. this.setState({
  86. auto: NewArr
  87. })
  88. }
  89. readItem = (id) => {
  90. const readItem = this.state.auto.filter((el) => el.id === id)
  91. this.setState({readItem})
  92. }
  93.  
  94. render() {
  95. const { auto } = this.state;
  96. return (
  97. <div className="App">
  98. <Header addItem={this.addItem}
  99. readItem={this.state.readItem}/>
  100. <EditItems ArrayAutoForRead={auto}
  101. delItemId={this.delItem}
  102. readItemId={this.readItem} />
  103. <ListAuto ArrayAuto={auto} />
  104. </div>
  105. );
  106. }
  107. }
  108.  
  109. export default App;
  110.  
  111. import React, { Component } from 'react';
  112.  
  113. class Header extends Component {
  114. constructor(props) {
  115. super(props);
  116. this.state = {
  117. id: this.props.readItem.id,
  118. name: this.props.readItem.name,
  119. marka: this.props.readItem.marka,
  120. country: this.props.readItem.country,
  121. price: this.props.readItem.price,
  122. url: this.props.readItem.url
  123. }
  124. }
  125.  
  126. addItem = () => {
  127. const { name, marka, country, price, url } = this.state;
  128. if (name !== "" && marka !== "" && country !== "" && price !== "" && url !== "") {
  129. const item = {
  130. name,
  131. marka,
  132. country,
  133. price,
  134. url
  135. }
  136. this.props.addItem(item)
  137. this.setState({ name: "", marka: "", country: "", price: "", url: "" })
  138. } else {
  139. alert('Заполните все поля')
  140. }
  141. }
  142. inputName = (e) => {
  143. const name = e.target.value;
  144. this.setState({ name })
  145. }
  146. inputMarka = (e) => {
  147. const marka = e.target.value;
  148. this.setState({ marka })
  149. }
  150. inputCountry = (e) => {
  151. const country = e.target.value;
  152. this.setState({ country })
  153. }
  154. inputPrice = (e) => {
  155. const price = e.target.value;
  156. this.setState({ price })
  157. }
  158. inputUrl = (e) => {
  159. const url = e.target.value;
  160. this.setState({ url })
  161. }
  162. render() {
  163. const { name, marka, country, price, url } = this.state;
  164. return (
  165. <div className="Header">
  166. Header menu
  167. <input
  168. name="name"
  169. value={name}
  170. onChange={this.inputName}
  171. placeholder="Название" />
  172. <input
  173. name="marka"
  174. value={marka}
  175. onChange={this.inputMarka}
  176. placeholder="Марка" />
  177. <input
  178. name="country"
  179. value={country}
  180. onChange={this.inputCountry}
  181. placeholder="Производитель" />
  182. <input
  183. name="price"
  184. value={price}
  185. onChange={this.inputPrice}
  186. placeholder="Цена" />
  187. <input
  188. name="url"
  189. value={url}
  190. onChange={this.inputUrl}
  191. placeholder="url картинки" />
  192.  
  193. <button onClick={this.addItem}>Добавить</button>
  194.  
  195. </div>
  196. );
  197. }
  198. }
  199.  
  200. export default Header;
  201.  
  202. import React, { Component } from 'react';
  203. import './style.css'
  204.  
  205. class EditItems extends Component {
  206. delItem = (id) => {
  207. this.props.delItemId(id)
  208. }
  209. readItem = (id) => {
  210. this.props.readItemId(id)
  211. }
  212. render() {
  213. const {ArrayAutoForRead} = this.props
  214. return (
  215. <div className="EditItems">
  216. Редактирование данных
  217. <table >
  218. <thead>
  219. <tr>
  220. <th>Id</th>
  221. <th>Название</th>
  222. <th>Марка</th>
  223. <th>Производитель</th>
  224. <th className="th">url фото</th>
  225. <th>Цена</th>
  226. <th>Действие</th>
  227. </tr>
  228. </thead>
  229. <tbody>
  230. {
  231. ArrayAutoForRead.map( (item,index) => {
  232. return (
  233. <tr rey={index}>
  234. <th>{item.id}</th>
  235. <th>{item.name}</th>
  236. <th>{item.marka}</th>
  237. <th>{item.country}</th>
  238. <th className="th">{item.url}</th>
  239. <th>{item.price}</th>
  240. <th><button onClick={() => this.readItem(item.id)}>Редакт</button><button onClick={() => this.delItem(item.id)}>X</button></th>
  241. </tr>
  242.  
  243. )
  244. })
  245. }
  246.  
  247.  
  248. </tbody>
  249. </table>
  250.  
  251. </div>
  252. );
  253. }
  254. }
  255.  
  256. export default EditItems;
  257.  
  258. import React, { Component } from 'react';
  259. import ItemAuto from '../ItemAuto/ItemAuto'
  260.  
  261.  
  262. class ListAuto extends Component {
  263. render() {
  264.  
  265. return (
  266. <>
  267. {
  268. this.props.ArrayAuto.map((item, index) => {
  269. return (
  270. <div className="ListAuto">
  271. <ItemAuto key={index} itemAuto={item} />
  272. </div>
  273. )
  274. })
  275. }
  276. </>
  277. );
  278. }
  279. }
  280.  
  281. export default ListAuto;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement