From 7382911126fa59f5225d75d06564a8d51af00497 Mon Sep 17 00:00:00 2001 From: Nikhil Date: Thu, 25 Apr 2019 13:57:49 -0400 Subject: [PATCH] Added FacetTabs Component --- documentation/components/FacetTabs.md | 37 +++++++ src/components/FacetResults.js | 16 ++- src/components/FacetTabs.js | 153 ++++++++++++++++++++++++++ src/index.js | 1 + styleguide.config.js | 1 + 5 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 documentation/components/FacetTabs.md create mode 100644 src/components/FacetTabs.js diff --git a/documentation/components/FacetTabs.md b/documentation/components/FacetTabs.md new file mode 100644 index 0000000..be9d13f --- /dev/null +++ b/documentation/components/FacetTabs.md @@ -0,0 +1,37 @@ +#### Examples: + + +__1:__ A simple example for showing facet as tabs. + +```jsx + const DummySearcher = require('../../src/components/DummySearcher').default; + const QueryResponse = require('../../src/api/QueryResponse').default; + const sampleFacets = require('../sampleData/Facets').default; + + const queryResponse = new QueryResponse(); + queryResponse.facets = [ + sampleFacets.regionFacet, + ]; + + + + +``` + +__2:__ Example for showing facet tabs by passing in a facet instead of facet name. + +```jsx + const sampleFacets = require('../sampleData/Facets').default; + + +``` + +__3:__ Example for showing facet tabs by passing in a facet and a background color. + +```jsx + const sampleFacets = require('../sampleData/Facets').default; + + +``` \ No newline at end of file diff --git a/src/components/FacetResults.js b/src/components/FacetResults.js index ed022c1..c39f026 100644 --- a/src/components/FacetResults.js +++ b/src/components/FacetResults.js @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'; import Facet from './Facet'; import SearchFacet from '../api/SearchFacet'; +import Configurable from './Configurable'; type FacetResultsProps = { /** The facet field names that should be displayed as pie charts */ @@ -42,6 +43,10 @@ type FacetResultsProps = { * buckets. By default, facets with no buckets will be hidden. */ showEmptyFacets: boolean; + /** + * An optional list of facets that should not be shown but are needed for other reasons + */ + hideFacet: Array; }; type FacetResultsDefaultProps = { @@ -57,6 +62,7 @@ type FacetResultsDefaultProps = { orderHint: Array; entityColors: Map; showEmptyFacets: boolean; + hideFacet: Array, }; /** @@ -67,7 +73,7 @@ type FacetResultsDefaultProps = { * not covered by one of these property's lists will be displayed * in a standard "More…" list. */ -export default class FacetResults extends React.Component { +class FacetResults extends React.Component { static defaultProps = { pieChartFacets: null, barChartFacets: null, @@ -81,6 +87,7 @@ export default class FacetResults extends React.Component 0) { + if (this.props.hideFacet.includes(facet.field)) { + return false; + } + } if (this.props.showEmptyFacets || (facet && facet.buckets && facet.buckets.length > 0)) { return true; } @@ -186,3 +198,5 @@ export default class FacetResults extends React.Component void; + /** + * Optional facet that should be used instead of facetName + */ + facet?: SearchFacet | null; //eslint-disable-line + /** + * The color that should be used for the background of facet tabs + */ + backgroundColor?: string; +}; + +type FacetTabsState = { + tabsList: Array; +}; + +class FacetTabs extends React.Component { + static contextTypes = { + searcher: PropTypes.any, + }; + + static defaultProps = { + facetName: 'table', + facet: null, + backgroundColor: '#add8e6', + }; + + static displayName = 'FacetTabs'; + + constructor(props: FacetTabsProps) { + super(props); + this.state = { + tabsList: [], + }; + + (this: any).getTabsFromFacet = this.getTabsFromFacet.bind(this); + (this: any).populateTabs = this.populateTabs.bind(this); + (this: any).handleTabClick = this.handleTabClick.bind(this); + } + + componentWillMount() { + this.populateTabs(this.props); + } + + componentWillReceiveProps(newProps: FacetTabsProps) { + this.populateTabs(newProps); + } + + //eslint-disable-next-line + numberWithCommas(x) { + return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); + } + + handleTabClick(bucket: SearchFacetBucket, label: string) { + this.context.searcher.addFacetFilter(label, bucket.value, bucket.filter); + } + + getTabsFromFacet(facet: SearchFacet) { + const tabs = []; + facet.buckets.forEach((bucket: SearchFacetBucket) => { + const value = bucket.value; + const count = this.numberWithCommas(bucket.count); + if (this.props.handleClick) { + tabs.push( +
{ + this.props.handleClick(bucket); + }} + className="facet-tab" + > +
+ {value} ({count}) +
+
, + ); + } else { + tabs.push( +
{ + this.handleTabClick(bucket, facet.label); + }} + className="facet-tab" + > +
+ {value} ({count}) +
+
, + ); + } + }); + this.setState({ tabsList: tabs }); + } + + populateTabs(props: FacetTabsProps) { + const { facetName, facet } = props; + + if (facet && facet.buckets) { + this.getTabsFromFacet(facet); + } else if (facetName) { + const searcher = this.context.searcher; + if (searcher && searcher.state.response) { + if (searcher.state.response.facets) { + searcher.state.response.facets.forEach((currentFacet: SearchFacet) => { + if (currentFacet.field === facetName && currentFacet.buckets) { + this.getTabsFromFacet(currentFacet); + } + }); + } + } + } + } + + render() { + const tabData = this.state.tabsList; + const tabs = + tabData.length > 0 ? ( +
+ {tabData} +
+ ) : ( + + ); + return
{tabs}
; + } +} + +export default Configurable(FacetTabs); diff --git a/src/index.js b/src/index.js index 4a05f52..a86d07b 100644 --- a/src/index.js +++ b/src/index.js @@ -77,6 +77,7 @@ export Facet from './components/Facet'; export FacetInsights from './components/FacetInsights'; export FacetResults from './components/FacetResults'; export FacetSearchBar from './components/FacetSearchBar'; +export FacetTabs from './components/FacetTabs'; export FormattedDate from './components/FormattedDate'; export GridLayout from './components/GridLayout'; export Header360 from './components/Header360'; diff --git a/styleguide.config.js b/styleguide.config.js index db42c40..24b463e 100644 --- a/styleguide.config.js +++ b/styleguide.config.js @@ -163,6 +163,7 @@ module.exports = { 'src/components/FacetInsights.js', 'src/components/FacetResults.js', 'src/components/FacetSearchBar.js', + 'src/components/FacetTabs.js', 'src/components/HierarchicalFacetContents.js', 'src/components/ListWithBarsFacetContents.js', 'src/components/MapFacetContents.js',