TableOptions.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { bindActionCreators } from 'redux';
  4. import { connect } from 'react-redux';
  5. import styled, { css } from 'styled-components';
  6. import TableNav from './TableNav';
  7. import TextInput from '../TextInput';
  8. import { getUrlsList } from '../../actions';
  9. const Tr = styled.tr`
  10. display: flex;
  11. align-items: center;
  12. thead & {
  13. border-bottom: 1px solid #ddd !important;
  14. }
  15. `;
  16. const Th = styled.th`
  17. display: flex;
  18. align-items: center;
  19. ${({ flex }) =>
  20. flex &&
  21. css`
  22. flex: ${`${flex} ${flex}`} 0;
  23. `};
  24. `;
  25. const Divider = styled.div`
  26. margin: 0 16px 0 24px;
  27. width: 1px;
  28. height: 20px;
  29. background-color: #ccc;
  30. @media only screen and (max-width: 768px) {
  31. margin: 0 4px 0 12px;
  32. }
  33. @media only screen and (max-width: 510px) {
  34. display: none;
  35. }
  36. `;
  37. const ListCount = styled.div`
  38. display: flex;
  39. align-items: center;
  40. `;
  41. const Ul = styled.ul`
  42. display: flex;
  43. margin: 0;
  44. padding: 0;
  45. list-style: none;
  46. li {
  47. display: flex;
  48. margin: 0 0 0 12px;
  49. list-style: none;
  50. @media only screen and (max-width: 768px) {
  51. margin-left: 8px;
  52. }
  53. }
  54. @media only screen and (max-width: 510px) {
  55. display: none;
  56. }
  57. `;
  58. const Button = styled.button`
  59. display: flex;
  60. padding: 4px 8px;
  61. border: none;
  62. font-size: 12px;
  63. border-radius: 4px;
  64. box-shadow: 0 0px 10px rgba(100, 100, 100, 0.1);
  65. background-color: white;
  66. cursor: pointer;
  67. transition: all 0.2s ease-out;
  68. box-sizing: border-box;
  69. ${({ active }) =>
  70. !active &&
  71. css`
  72. border: 1px solid #ddd;
  73. background-color: #f5f5f5;
  74. box-shadow: 0 0px 5px rgba(150, 150, 150, 0.1);
  75. :hover {
  76. border-color: 1px solid #ccc;
  77. background-color: white;
  78. }
  79. `};
  80. @media only screen and (max-width: 768px) {
  81. font-size: 10px;
  82. }
  83. `;
  84. class TableOptions extends Component {
  85. constructor() {
  86. super();
  87. this.state = {
  88. search: '',
  89. };
  90. this.submitSearch = this.submitSearch.bind(this);
  91. this.handleSearch = this.handleSearch.bind(this);
  92. this.handleCount = this.handleCount.bind(this);
  93. this.handleNav = this.handleNav.bind(this);
  94. }
  95. submitSearch(e) {
  96. e.preventDefault();
  97. this.props.getUrlsList({ search: this.state.search });
  98. }
  99. handleSearch(e) {
  100. this.setState({ search: e.currentTarget.value });
  101. }
  102. handleCount(e) {
  103. const count = Number(e.target.textContent);
  104. this.props.getUrlsList({ count });
  105. }
  106. handleNav(e) {
  107. const { active, type } = e.target.dataset;
  108. if (active === 'false') return null;
  109. const number = type === 'next' ? 1 : -1;
  110. return this.props.getUrlsList({ page: this.props.url.page + number });
  111. }
  112. render() {
  113. const { count, countAll, page } = this.props.url;
  114. return (
  115. <Tr>
  116. <Th>
  117. {!this.props.nosearch && (
  118. <form onSubmit={this.submitSearch}>
  119. <TextInput
  120. id="search"
  121. name="search"
  122. value={this.state.search}
  123. placeholder="Search..."
  124. onChange={this.handleSearch}
  125. tiny
  126. />
  127. </form>
  128. )}
  129. </Th>
  130. <Th>
  131. <ListCount>
  132. <Ul>
  133. <li>
  134. <Button active={count === 10} onClick={this.handleCount}>
  135. 10
  136. </Button>
  137. </li>
  138. <li>
  139. <Button active={count === 25} onClick={this.handleCount}>
  140. 25
  141. </Button>
  142. </li>
  143. <li>
  144. <Button active={count === 50} onClick={this.handleCount}>
  145. 50
  146. </Button>
  147. </li>
  148. </Ul>
  149. </ListCount>
  150. <Divider />
  151. <TableNav handleNav={this.handleNav} next={page * count < countAll} prev={page > 1} />
  152. </Th>
  153. </Tr>
  154. );
  155. }
  156. }
  157. TableOptions.propTypes = {
  158. getUrlsList: PropTypes.func.isRequired,
  159. nosearch: PropTypes.bool,
  160. url: PropTypes.shape({
  161. page: PropTypes.number.isRequired,
  162. count: PropTypes.number.isRequired,
  163. countAll: PropTypes.number.isRequired,
  164. }).isRequired,
  165. };
  166. TableOptions.defaultProps = {
  167. nosearch: false,
  168. };
  169. const mapStateToProps = ({ url }) => ({ url });
  170. const mapDispatchToProps = dispatch => ({
  171. getUrlsList: bindActionCreators(getUrlsList, dispatch),
  172. });
  173. export default connect(mapStateToProps, mapDispatchToProps)(TableOptions);