Skip to content

Commit

Permalink
CC-5556: update filter bar results text (#52)
Browse files Browse the repository at this point in the history
* Update filter bar results text

* Remove duplicate test

* Add optional name type to Cut::FilterBar
  • Loading branch information
WenInCode committed Aug 29, 2023
1 parent 93f689e commit edad51f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-pianos-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hashicorp/consul-ui-toolkit': minor
---

Add @name argument to Cut::FilterBar and update default result text
2 changes: 1 addition & 1 deletion documentation/app/templates/components/filter-bar.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<Cut::FilterBar
@config={{this.filters}}
@name="service"
@count={{this.count}}
@onChange={{this.handleFilterChange}}
as |FB|
Expand Down Expand Up @@ -221,7 +222,6 @@

<Cut::FilterBar
@config={{this.filters}}
@count={{this.count}}
@onChange={{this.handleFilterChange}}
as |FB|
>
Expand Down
68 changes: 54 additions & 14 deletions documentation/tests/integration/components/filter-bar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import {
import { hbs } from 'ember-cli-htmlbars';
import sinon from 'sinon';

async function setupTest(config) {
async function setupTest({ config, name, count, totalCount } = {}) {
this.set('name', name);

this.set('count', count);

this.set('totalCount', totalCount);

this.set(
'config',
config || {
Expand Down Expand Up @@ -44,7 +50,7 @@ async function setupTest(config) {
this.onChange = onChange;

await render(hbs`
<Cut::FilterBar @config={{this.config}} @onChange={{this.onChange}} as |FB|>
<Cut::FilterBar @config={{this.config}} @name={{this.name}} @count={{this.count}} @totalCount={{this.totalCount}} @onChange={{this.onChange}} as |FB|>
<FB.Search placeholder="Search for services" data-test-search />
<FB.FilterGroup as |Filters|>
<Filters.Filter
Expand Down Expand Up @@ -159,19 +165,21 @@ module('Integration | Component | cut/filter-bar', function (hooks) {

test("clear filters doesn't show when there are only required filters", async function (assert) {
await setupTest.call(this, {
search: {
value: '',
},
filters: {
juice: {
text: 'Orange',
value: 'oj',
isRequired: true,
config: {
search: {
value: '',
},
filters: {
juice: {
text: 'Orange',
value: 'oj',
isRequired: true,
},
},
sort: {
text: 'critical to healthy',
value: 'health',
},
},
sort: {
text: 'critical to healthy',
value: 'health',
},
});

Expand Down Expand Up @@ -319,6 +327,38 @@ module('Integration | Component | cut/filter-bar', function (hooks) {
);
});

test('shows a default results text if no count is passed in', async function (assert) {
await setupTest.call(this);

assert.dom('[data-test-filter-bar-results]').hasText('Showing all results');
});

test('shows a default results text with the name if the name is passed in with no count', async function (assert) {
await setupTest.call(this, { name: 'nacho' });

assert.dom('[data-test-filter-bar-results]').hasText('Showing all nachos');
});

test('shows a result count with a default text for the name if you pass in a count but no name', async function (assert) {
await setupTest.call(this, { count: 3 });

assert.dom('[data-test-filter-bar-results]').hasText('Showing 3 results');
});

test('shows a result count with a name if you pass in a count and a name', async function (assert) {
await setupTest.call(this, { count: 1, name: 'song' });

assert.dom('[data-test-filter-bar-results]').hasText('Showing 1 song');
});

test('shows a result count with a total if you pass in a total and a count', async function (assert) {
await setupTest.call(this, { count: 5, totalCount: 10, name: 'song' });

assert
.dom('[data-test-filter-bar-results]')
.hasText('Showing 5 songs of 10');
});

test('search updates the search values', async function (assert) {
const { onChange } = await setupTest.call(this);

Expand Down
15 changes: 10 additions & 5 deletions toolkit/src/components/cut/filter-bar/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,18 @@
</div>

<div class='cut-filter-bar-results'>
{{#let (if @count @count 0) as |count|}}
<p class='cut-filter-bar-results-count'>Showing
{{pluralize count 'result'}}{{if
<p class='cut-filter-bar-results-count' data-test-filter-bar-results>
{{#if this.hasCount}}
Showing
{{pluralize @count (if @name @name 'result')}}{{if
@totalCount
(concat ' of ' @totalCount)
}}</p>
{{/let}}
}}
{{else}}
Showing all
{{if @name (pluralize @name) 'results'}}
{{/if}}
</p>

{{#each this.appliedFilters as |filter|}}
<p class='cut-filter-bar-applied-filter-label'>{{titlecase
Expand Down
4 changes: 4 additions & 0 deletions toolkit/src/components/cut/filter-bar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export default class FilterBarComponent extends Component<FilterBarSignature> {
);
}

get hasCount(): boolean {
return typeof this.args.count === 'number';
}

isChecked(localConfig: FilterConfig, filter: string, value: unknown) {
if (Array.isArray(localConfig?.filters?.[filter])) {
return !!(localConfig?.filters?.[filter] as Filter[]).find(
Expand Down
1 change: 1 addition & 0 deletions toolkit/src/components/cut/filter-bar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
export interface FilterBarSignature {
Args: {
config: FilterConfig;
name?: string;
count?: number;
totalCount?: number;
onChange: (config: FilterConfig) => void;
Expand Down

1 comment on commit edad51f

@vercel
Copy link

@vercel vercel bot commented on edad51f Aug 29, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.