Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import "./App.css";
  3.  
  4. class ProductRow extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.handlerrOnMouseOver = this.handlerrOnMouseOver.bind(this);
  8. this.handlerrOnMouseOut = this.handlerrOnMouseOut.bind(this);
  9. this.handlerrOnMouseDown = this.handlerrOnMouseDown.bind(this);
  10. this.state = {
  11. class: ""
  12. };
  13. }
  14.  
  15. handlerrOnMouseOver() {
  16. this.setState({
  17. class: "selected"
  18. });
  19. }
  20.  
  21. handlerrOnMouseOut() {
  22. this.setState({
  23. class: ""
  24. });
  25. }
  26.  
  27. handlerrOnMouseDown() {
  28. this.props.callback(this.props.product.station);
  29. }
  30.  
  31. render() {
  32. const product = this.props.product;
  33. return (
  34. <li
  35. className={this.state.class}
  36. onMouseOver={this.handlerrOnMouseOver}
  37. onMouseOut={this.handlerrOnMouseOut}
  38. >
  39. <a href="javascript:void(0);" onMouseDown={this.handlerrOnMouseDown}>
  40. {product.station}
  41. </a>
  42. </li>
  43. );
  44. }
  45. }
  46.  
  47. class ProductTable extends React.Component {
  48. render() {
  49. const filterText = this.props.filterText;
  50. const rows = [];
  51. if (filterText !== "") {
  52. this.props.products.map((product, key) => {
  53. if (
  54. product.station.toLowerCase().indexOf(filterText.toLowerCase()) ===
  55. -1 ||
  56. rows.length === 20
  57. ) {
  58. return;
  59. } else {
  60. rows.push(
  61. <ProductRow
  62. product={product}
  63. key={key}
  64. callback={this.props.callback}
  65. />
  66. );
  67. }
  68. });
  69. }
  70.  
  71. var style = {
  72. width: "100%",
  73. fontSize: 14
  74. };
  75.  
  76. if (rows.length > 0) {
  77. return (
  78. <div className="sf_suggestion">
  79. <ul tabIndex="-1" style={style}>
  80. {rows}
  81. </ul>
  82. </div>
  83. );
  84. } else {
  85. return <div />;
  86. }
  87. }
  88. }
  89.  
  90. class SearchBar extends React.Component {
  91. constructor(props) {
  92. super(props);
  93. this.handlerFilterTextChange = this.handlerFilterTextChange.bind(this);
  94. this.handlerOnKeyUp = this.handlerOnKeyUp.bind(this);
  95. this.handlerOnBlur = this.handlerOnBlur.bind(this);
  96. this.changeValue = this.changeValue.bind(this);
  97. this.handlerOnFocus = this.handlerOnFocus.bind(this);
  98. this.state = {
  99. visible: true,
  100. filterText: "",
  101. key: ""
  102. };
  103. }
  104.  
  105. handlerFilterTextChange(e) {
  106. e.preventDefault();
  107. this.setState({ filterText: e.target.value });
  108. }
  109.  
  110. handlerOnKeyUp() {
  111. var key = window.event.keyCode;
  112. if (key === 38 || key === 40 || key === 13) {
  113. this.setState({ key: key });
  114. }
  115. if (key === 27) {
  116. this.setState({ visible: false });
  117. }
  118. }
  119.  
  120. changeValue(e) {
  121. this.setState({ filterText: e });
  122. this.handlerOnBlur();
  123. }
  124.  
  125. handlerOnBlur() {
  126. this.setState({ visible: false });
  127. this.setState({ key: "" });
  128. }
  129.  
  130. handlerOnFocus() {
  131. if (!this.state.visible) {
  132. this.setState({ visible: true });
  133. }
  134. }
  135.  
  136. render() {
  137. return (
  138. <p className="inp">
  139. <label>
  140. <input
  141. type="text"
  142. placeholder="&nbsp;"
  143. value={this.state.filterText}
  144. onChange={this.handlerFilterTextChange}
  145. onFocus={this.handlerOnFocus}
  146. onKeyUp={this.handlerOnKeyUp}
  147. onBlur={this.handlerOnBlur}
  148. />
  149. <span className="label">{this.props.name}</span>
  150. <span className="border" />
  151. {this.state.visible && (
  152. <ProductTable
  153. products={this.props.products}
  154. filterText={this.state.filterText}
  155. keyUp={this.state.key}
  156. callback={this.changeValue}
  157. />
  158. )}
  159. </label>
  160. </p>
  161. );
  162. }
  163. }
  164.  
  165. class App extends Component {
  166. render() {
  167. return (
  168. <table>
  169. <tbody>
  170. <td>
  171. <SearchBar products={this.props.products} name={"Имя"} />
  172. </td>
  173. </tbody>
  174. </table>
  175. );
  176. }
  177. }
  178.  
  179. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement