All files / src/hocs/components filter.js

13.04% Statements 3/23
0% Branches 0/8
0% Functions 0/6
13.04% Lines 3/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 130 131 132 133 134 135 136 137 138 139                        3x               3x           3x                                                                                                                                                                                                                                
// @flow
import * as React from 'react';
import styled from 'styled-components';
import SelectFilter from './select';
import NumberFilter from './number';
import TextFilter from './text';
// import DateRangeFilter from './dateRange';
import {Button, Row, Col} from 'antd';
import isEmpty from 'lodash/isEmpty';
import {FormattedMessage} from 'react-intl';
import defaultMessage from './locale';
 
const FilterRow = styled(Row)`
  margin-bottom: 30px;
  border: 1px #f8f8f8 solid;
  padding: 15px;
  margin-top: 20px;
  box-shadow: 1px 1px 4px #eee;
`;
 
const FilterComponent = styled.div`
  flex: 1;
  margin-right: 15px;
  min-width: 100px;
`;
 
const ButtonCol = styled(Col)`
  text-align: right;
  padding-top: 16px;
`;
 
type Props = {
  changeFilter: Object => void,
  fields: Array<{
    key: string,
    type: string,
    options: Array<{
      key: string,
      title: string
    }>,
    label: string
  }>,
  where: Object
}
 
type State = {
  condition: Object
}
 
class FilterGroup extends React.Component<Props, State> {
  constructor(props) {
    super(props);
    this.state = {
      condition: {
      },
    };
  }
 
  onChange = (cond: Object) => {
    let {condition} = this.state;
    if (isEmpty(cond)) {
      this.setState({
        condition: {}
      });
    } else {
      Object.keys(cond).forEach(key => {
        const newCond = cond[key];
        if (newCond === undefined) {
          delete condition[key];
        } else {
          condition[key] = cond[key]
        }
      });
      this.setState({
        condition
      });
    }
  }
 
  submit = () => {
    const {condition} = this.state;
    this.props.changeFilter(condition);
  }
 
  render() {
    const {fields, where} = this.props;
    const filters = fields.map((val) => {
      switch (val.type) {
        case 'select':
          return <SelectFilter onChange={this.onChange} options={val.options} where={where} label={val.label}/>;
        case 'number':
          return <NumberFilter onChange={this.onChange} name={val.key} label={val.label} where={where}/>;
        /*
        case 'dateRange':
          return <DateRangeFilter onChange={this.onChange} schema={{[val.key]: val}}/>
        */
        case 'text':
        default:
          return <TextFilter onChange={this.onChange} name={val.key} label={val.label} where={where}/>;
      }
    });
    return (
      <FilterRow type="flex" justify="space-between" align="bottom">
        <Col span={20}>
          <FilterComponent>
            {[filters]}
          </FilterComponent>
        </Col>
        <ButtonCol span={4}>
          <Button type="primary" icon="search" size="large" onClick={this.submit}>
            <FormattedMessage
              id="query.filter.search"
              defaultMessage={defaultMessage.en['query.filter.search']}
            />
          </Button>
        </ButtonCol>
      </FilterRow>
    );
  }
}
 
export default styled(FilterGroup)`
  .ant-input,
  .ant-select-selection {
    height: 32px;
    line-height: 32px;
    border-radius: 20px;
  }
 
  .ant-select-selection__placeholder {
    height: 28px;
    line-height: 28px;      
  }
 
  .ant-select-dropdown {
    border-radius: 2px !important;
  }
`