All files / src/ui/components FilterComponent.tsx

100% Statements 8/8
100% Branches 0/0
100% Functions 4/4
100% Lines 8/8

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                                                                  4x 4x 4x       3x 3x       4x 4x                     1x                                                          
/*
 * Copyright 2019 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import * as React from "react";
import { TextInput, Button, ButtonVariant } from "@patternfly/react-core";
import './FilterComponent.css';
import { Predicate } from "types";
import { WithTranslation, withTranslation } from "react-i18next";
 
export interface FilterProps<T> {
  filter: (filter: string) => Predicate<T>;
  onChange: (filter: Predicate<T>) => void;
}
 
export interface FilterState {
  filterText: string;
}
 
export class FilterComponent<T> extends React.Component<FilterProps<T> & WithTranslation, FilterState> {
 
  constructor(props: FilterProps<T> & WithTranslation) {
    super(props);
    this.state = {filterText: ""};
    this.updateFilter = this.updateFilter.bind(this);
  }
 
  updateFilter(filterText: string) {
    this.props.onChange(this.props.filter(filterText));
    this.setState({filterText});
  }
 
  render() {
    const { t } = this.props;
    return (
      <div className="search-icons">
        <TextInput
          aria-label="Search"
          placeholder={t("search")}
          value={this.state.filterText}
          onChange={this.updateFilter} 
        />
        <Button
          variant={ButtonVariant.plain}
          isDisabled={this.state.filterText.length === 0} 
          onClick={() => this.updateFilter("")}
        >
          <svg 
            style={{verticalAlign: "-0.125em"}}
            fill="currentColor"
            height="1em"
            width="1em"
            viewBox="0 0 512 512"
            aria-hidden="true"
            role="img"
          >
            <path
              d={
                "M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 " +
                "256 8zm121.6 313.1c4.7 4.7 4.7 12.3 0 17L338 377.6c-4.7 4.7-12.3 4.7-17 " +
                "0L256 312l-65.1 65.6c-4.7 4.7-12.3 4.7-17 0L134.4 338c-4.7-4.7-4.7-12.3 " +
                "0-17l65.6-65-65.6-65.1c-4.7-4.7-4.7-12.3 0-17l39.6-39.6c4.7-4.7 12.3-4.7 " +
                "17 0l65 65.7 65.1-65.6c4.7-4.7 12.3-4.7 17 0l39.6 39.6c4.7 4.7 4.7 12.3 0 17L312 "+
                "256l65.6 65.1z"
              }
              transform=""
            />
          </svg>
        </Button>
      </div>
    );
  }
}
 
export default withTranslation("FilterComponent")(FilterComponent);