All files / posts-table/components/PostsTable/components/Searchbox index.jsx

50% Statements 11/22
0% Branches 0/2
0% Functions 0/8
55% Lines 11/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                    1x                 1x                             1x                   1x                   1x             1x                 1x   1x             1x                                                                             1x       1x            
import React from 'react';
import PropTypes from 'prop-types';
import TagsInput from 'react-tagsinput';
import styled from 'styled-components';
 
import { geyser, curiousBlue, shuttleGray } from '@bufferapp/components/style/color';
import { fontFamily, fontSizeMini } from '@bufferapp/components/style/font';
import { CloseIcon } from '@bufferapp/components/Icon/Icons';
import { Text, Button } from '@bufferapp/components';
 
const Tag = styled.span`
  border-radius: 2px;
  padding: .125rem .5rem .25rem;
  color: white;
  font-weight: bold;
  background-color: ${curiousBlue};
  margin-right: .25rem;
`;
 
const Input = styled.input`
  font-family: ${fontFamily};
  font-size: ${fontSizeMini};
  color: ${shuttleGray};
  border: none;
  margin-left: .25rem;
  max-width: 100%;
  width: 100%;
  padding: .25rem;
 
  &:focus {
    outline: none;
  }
`;
 
const TagContent = styled.span`
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
 
  & > *:first-child {
    margin-right: .5rem;
  }
`;
 
const searchTag = ({ key, onRemove, getTagDisplayValue, tag, ...other }) =>
  <Tag key={key} {...other}>
    <Button noStyle onClick={() => onRemove(key)}>
      <TagContent>
        <Text color="white" size="mini">{getTagDisplayValue(tag)}</Text>
        <CloseIcon size="small" color="white"/>
      </TagContent>
    </Button>
  </Tag>;
 
searchTag.propTypes = {
  key: PropTypes.string.isRequired,
  onRemove: PropTypes.func.isRequired,
  getTagDisplayValue: PropTypes.func.isRequired,
  tag: PropTypes.string.isRequired,
};
 
const SearchWrapper = styled.div`
  overflow: hidden;
  border: 1px solid ${geyser};
  padding: .25rem;
  border-radius: 3px;
  width: 26rem;
  display: inline-flex;
`;
 
const layout = (tags, input) => <SearchWrapper>{tags} {input}</SearchWrapper>;
 
const searchInput = props => (<Input
  {...props}
  value={props.value}
  type="text"
  placeholder={props.hasTags ? null : 'Filter posts by keywords or #hashtags'}
/>);
 
searchInput.propTypes = {
  value: PropTypes.string.isRequired,
  hasTags: PropTypes.bool.isRequired,
};
 
class Searchbox extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      tags: [],
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleChangeInput = this.handleChangeInput.bind(this);
  }
 
  handleChange(tags) {
    this.props.search(tags);
  }
 
  handleChangeInput(tags) {
    this.setState({ tags });
  }
 
  render() {
    return (<TagsInput
      value={this.props.searchTerms}
      inputValue={this.state.tags}
      onChange={this.handleChange}
      onChangeInput={this.handleChangeInput}
      inputProps={{
        hasTags: this.props.searchTerms.length > 0,
      }}
      renderInput={searchInput}
      renderTag={searchTag}
      renderLayout={layout}
    />);
  }
}
 
Searchbox.defaultProps = {
  searchTerms: [],
};
 
Searchbox.propTypes = {
  search: PropTypes.func.isRequired,
  searchTerms: PropTypes.arrayOf(PropTypes.string),
};
 
export default Searchbox;