import {autobind} from "core-decorators"; import i18n from "i18next"; import {uniqueId} from "lodash"; import {observable} from "mobx"; import {observer} from "mobx-react"; import * as React from "react"; import {injectStyle} from "../../../../theming"; import {FacetData, FacetValue} from "./facet-data"; import * as styles from "./style/facet.css"; export type FacetStyle = Partial; export interface FacetProps { classNames?: FacetStyle; expandHandler: (facetKey: string, expand: boolean) => void; facet: {code: string, label: string, values: FacetValue[]}; facetKey: string; isExpanded: boolean; nbDefaultDataList: number; selectedDataKey: string | undefined; selectHandler: (facetKey: string, dataKey: string | undefined) => void; } @injectStyle("facet") @autobind @observer export class Facet extends React.Component { @observable private isExpanded: boolean; @observable private isShowAll = false; private facetDataSelectionHandler(dataKey: string) { this.props.expandHandler(this.props.facetKey, false); this.props.selectHandler(this.props.facetKey, dataKey); } private facetTitleClickHandler() { this.props.expandHandler(this.props.facetKey, !this.props.isExpanded); if (this.props.selectedDataKey) { this.props.selectHandler(this.props.facetKey, undefined); } this.isExpanded = !this.props.isExpanded; this.isShowAll = false; } private showAllHandler() { this.isShowAll = !this.isShowAll; } private renderFacetTitle() { const {facetKey, selectedDataKey, facet, classNames} = this.props; let facetTitle = i18n.t("live.filter.facets." + facetKey); // Default facet translation path is live.filter.facets. if (selectedDataKey) { const selectedFacet = facet.values.filter(value => value.code === selectedDataKey); const facetLabel = selectedFacet.length ? selectedFacet[0].label : ""; facetTitle = `${facetTitle} : ${facetLabel}`; } return (

{facetTitle}

); } private renderShowAllDataList() { if (!this.isShowAll && this.props.facet.values.length > this.props.nbDefaultDataList) { return ( {i18n.t("show.all")} ); } else { return null; } } private renderFacetDataList() { const {isExpanded, selectedDataKey, facet, nbDefaultDataList, classNames} = this.props; if (!isExpanded || selectedDataKey) { return null; } const facetValues = this.isShowAll ? facet.values : facet.values.slice(0, nbDefaultDataList); return (
    {facetValues.map(facetValue => (
  • ))}
{this.renderShowAllDataList()}
); } render() { const {selectedDataKey, isExpanded, classNames} = this.props; return (
{this.renderFacetTitle()} {this.renderFacetDataList()}
); } };