Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FacetTabs Component #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

nikhilj13
Copy link
Contributor

Adds a FacetTabs component that shows facet as a tab. The main use for facet tabs would be to show the search results based on the data source. Below is an example:

facetTabs

Also modifies FacetResults to take in a list of facets that should be hidden. If we're showing a facet as tabs we most likely would not want the facet to show up in facet results. It is configurable from SearchUI's configuration properties file. Here's an example:

FacetResults: {
	// facets that should be hidden from the FacetResults
	hideFacet: [ 'table' ],
  },

/**
* An optional list of facets that should not be shown but are needed for other reasons
*/
hideFacet: Array<string>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add ? to indicate this is optional. Make the prop name plural to indicate it's an array, not a boolean.

Suggested change
hideFacet: Array<string>;
hiddenFacets?: Array<string>;

@@ -57,6 +62,7 @@ type FacetResultsDefaultProps = {
orderHint: Array<string>;
entityColors: Map<string, string>;
showEmptyFacets: boolean;
hideFacet: Array<string>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename prop to indicate this is an array not a boolean.

Suggested change
hideFacet: Array<string>,
hiddenFacets: Array<string>,

@@ -81,6 +87,7 @@ export default class FacetResults extends React.Component<FacetResultsDefaultPro
orderHint: [],
entityColors: new Map(),
showEmptyFacets: false,
hideFacet: [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hideFacet: [],
hiddenFacets: [],

@@ -129,6 +136,11 @@ export default class FacetResults extends React.Component<FacetResultsDefaultPro
}

shouldShow(facet: SearchFacet): boolean {
if (this.props.hideFacet && this.props.hideFacet.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this.props.hideFacet && this.props.hideFacet.length > 0) {
if (this.props.hiddenFacets && this.props.hiddenFacets.length > 0) {

@@ -129,6 +136,11 @@ export default class FacetResults extends React.Component<FacetResultsDefaultPro
}

shouldShow(facet: SearchFacet): boolean {
if (this.props.hideFacet && this.props.hideFacet.length > 0) {
if (this.props.hideFacet.includes(facet.field)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this.props.hideFacet.includes(facet.field)) {
if (this.props.hiddenFacets.includes(facet.field)) {

(this: any).handleTabClick = this.handleTabClick.bind(this);
}

componentWillMount() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using componentWillMount


render() {
const tabData = this.state.tabsList;
const tabs =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull tabs out into renderTabs

);
}
});
this.setState({ tabsList: tabs });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From react docs, ("Thinking in React")[https://reactjs.org/docs/thinking-in-react.html#step-3-identify-the-minimal-but-complete-representation-of-ui-state]

Ask three questions about each piece of data:

Is it passed in from a parent via props? If so, it probably isn’t state.
Does it remain unchanged over time? If so, it probably isn’t state.
Can you compute it based on any other state or props in your component? If so, it isn’t state.

With that in mind, if you're storing html as state you should reconsider.

Copy link
Contributor

@klk1010 klk1010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address feedback.

Copy link
Collaborator

@luigivaldez luigivaldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a change to add the style changes to the less file in SUIT too (that the style guide uses)?

/**
* Name of the facet that should be displayed as Tabs, default is 'table'
*/
facetName: string; //eslint-disable-line
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the eslint-disable-line needed here?

/**
* Optional callback to be used when a tab is clicked instead of default (which applies the facet filter)
*/
handleClick?: () => void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this callback take information about the facet whose tab is clicked? Perhaps the entire SearchFacetBucket object? How will the callback know what was clicked and what to do?

Oh, I just got down further to where you call this and you are passing the bucket... so you should change the type of this to (bucket: SearchFacetBucket) => void and also add to the comment so someone using this knows what they need to pass in...


//eslint-disable-next-line
numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use x.toLocaleString() to do this without needing a special function.

47626.toLocaleString() gives 47,626.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString


getTabsFromFacet(facet: SearchFacet) {
const tabs = [];
facet.buckets.forEach((bucket: SearchFacetBucket) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to have a limit on the number of tabs? What if there are 70 buckets?

populateTabs(props: FacetTabsProps) {
const { facetName, facet } = props;

if (facet && facet.buckets) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the facet had buckets before but doesn't any more (e.g. because of a different search term)? Will the old tabs not be replaced?

const searcher = this.context.searcher;
if (searcher && searcher.state.response) {
if (searcher.state.response.facets) {
searcher.state.response.facets.forEach((currentFacet: SearchFacet) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really a find() operation on the array.... it would be clearer to call find() instead of forEach()....

className="facet-tab"
>
<div className="facet-tab-text">
{value} <span style={{ color: 'gray' }}>({count})</span>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to have a CSS style for the tab and the text of the tab, you should use another one for the count instead of hardcoding the gray color here.

{tabData}
</div>
) : (
<BusyIndicator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are just no buckets for the facet? I don't think we want to show the spinny in that case...

@klk1010 klk1010 added this to the v5.6.5 milestone Oct 8, 2019
@klk1010 klk1010 added merge conflicts This PR has a merge conflict that needs to be resolved. help wanted Extra attention is needed labels Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed merge conflicts This PR has a merge conflict that needs to be resolved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants