TableOptions.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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(num) {
  107. return (e) => {
  108. const { active } = e.target.dataset;
  109. if (active === 'false') return null;
  110. return this.props.getUrlsList({ page: this.props.url.page + num });
  111. }
  112. }
  113. render() {
  114. const { count, countAll, page } = this.props.url;
  115. return (
  116. <Tr>
  117. <Th>
  118. {!this.props.nosearch && (
  119. <form onSubmit={this.submitSearch}>
  120. <TextInput
  121. id="search"
  122. name="search"
  123. value={this.state.search}
  124. placeholder="Search..."
  125. onChange={this.handleSearch}
  126. tiny
  127. />
  128. </form>
  129. )}
  130. </Th>
  131. <Th>
  132. <ListCount>
  133. <Ul>
  134. <li>
  135. <Button active={count === 10} onClick={this.handleCount}>
  136. 10
  137. </Button>
  138. </li>
  139. <li>
  140. <Button active={count === 25} onClick={this.handleCount}>
  141. 25
  142. </Button>
  143. </li>
  144. <li>
  145. <Button active={count === 50} onClick={this.handleCount}>
  146. 50
  147. </Button>
  148. </li>
  149. </Ul>
  150. </ListCount>
  151. <Divider />
  152. <TableNav handleNav={this.handleNav} next={page * count < countAll} prev={page > 1} />
  153. </Th>
  154. </Tr>
  155. );
  156. }
  157. }
  158. TableOptions.propTypes = {
  159. getUrlsList: PropTypes.func.isRequired,
  160. nosearch: PropTypes.bool,
  161. url: PropTypes.shape({
  162. page: PropTypes.number.isRequired,
  163. count: PropTypes.number.isRequired,
  164. countAll: PropTypes.number.isRequired,
  165. }).isRequired,
  166. };
  167. TableOptions.defaultProps = {
  168. nosearch: false,
  169. };
  170. const mapStateToProps = ({ url }) => ({ url });
  171. const mapDispatchToProps = dispatch => ({
  172. getUrlsList: bindActionCreators(getUrlsList, dispatch),
  173. });
  174. export default connect(
  175. mapStateToProps,
  176. mapDispatchToProps
  177. )(TableOptions);