-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] Deangularize the hits counter and create a react component (…
…#65631) (#65881) * Deangularize the hits counter and create a react component * Add aria-label to button for accessibility * Add icon to the link button and use EuiText * Remove snapshots and test with findTestSubject * Change toString with String() Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
f99cc49
commit 7fd09b3
Showing
9 changed files
with
236 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/plugins/discover/public/application/components/hits_counter/hits_counter.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 React from 'react'; | ||
import { mountWithIntl } from 'test_utils/enzyme_helpers'; | ||
import { ReactWrapper } from 'enzyme'; | ||
import { HitsCounter, HitsCounterProps } from './hits_counter'; | ||
// @ts-ignore | ||
import { findTestSubject } from '@elastic/eui/lib/test'; | ||
|
||
describe('hits counter', function() { | ||
let props: HitsCounterProps; | ||
let component: ReactWrapper<HitsCounterProps>; | ||
|
||
beforeAll(() => { | ||
props = { | ||
onResetQuery: jest.fn(), | ||
showResetButton: true, | ||
hits: 2, | ||
}; | ||
}); | ||
|
||
it('HitsCounter renders a button by providing the showResetButton property', () => { | ||
component = mountWithIntl(<HitsCounter {...props} />); | ||
expect(findTestSubject(component, 'resetSavedSearch').length).toBe(1); | ||
}); | ||
|
||
it('HitsCounter not renders a button when the showResetButton property is false', () => { | ||
component = mountWithIntl( | ||
<HitsCounter hits={2} showResetButton={false} onResetQuery={jest.fn()} /> | ||
); | ||
expect(findTestSubject(component, 'resetSavedSearch').length).toBe(0); | ||
}); | ||
|
||
it('expect to render the number of hits', function() { | ||
component = mountWithIntl(<HitsCounter {...props} />); | ||
const hits = findTestSubject(component, 'discoverQueryHits'); | ||
expect(hits.text()).toBe('2'); | ||
}); | ||
|
||
it('expect to render 1,899 hits if 1899 hits given', function() { | ||
component = mountWithIntl( | ||
<HitsCounter hits={1899} showResetButton={false} onResetQuery={jest.fn()} /> | ||
); | ||
const hits = findTestSubject(component, 'discoverQueryHits'); | ||
expect(hits.text()).toBe('1,899'); | ||
}); | ||
|
||
it('should reset query', function() { | ||
component = mountWithIntl(<HitsCounter {...props} />); | ||
findTestSubject(component, 'resetSavedSearch').simulate('click'); | ||
expect(props.onResetQuery).toHaveBeenCalled(); | ||
}); | ||
}); |
83 changes: 83 additions & 0 deletions
83
src/plugins/discover/public/application/components/hits_counter/hits_counter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 React from 'react'; | ||
import { EuiButtonEmpty, EuiFlexGroup, EuiFlexItem, EuiText } from '@elastic/eui'; | ||
import { FormattedMessage, I18nProvider } from '@kbn/i18n/react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { formatNumWithCommas } from '../../helpers'; | ||
|
||
export interface HitsCounterProps { | ||
/** | ||
* the number of query hits | ||
*/ | ||
hits: number; | ||
/** | ||
* displays the reset button | ||
*/ | ||
showResetButton: boolean; | ||
/** | ||
* resets the query | ||
*/ | ||
onResetQuery: () => void; | ||
} | ||
|
||
export function HitsCounter({ hits, showResetButton, onResetQuery }: HitsCounterProps) { | ||
return ( | ||
<I18nProvider> | ||
<EuiFlexGroup | ||
gutterSize="s" | ||
className="dscResultCount" | ||
responsive={false} | ||
justifyContent="center" | ||
alignItems="center" | ||
> | ||
<EuiFlexItem grow={false}> | ||
<EuiText> | ||
<strong data-test-subj="discoverQueryHits">{formatNumWithCommas(hits)}</strong>{' '} | ||
<FormattedMessage | ||
id="discover.hitsPluralTitle" | ||
defaultMessage="{hits, plural, one {hit} other {hits}}" | ||
values={{ | ||
hits, | ||
}} | ||
/> | ||
</EuiText> | ||
</EuiFlexItem> | ||
{showResetButton && ( | ||
<EuiFlexItem grow={false}> | ||
<EuiButtonEmpty | ||
iconType="refresh" | ||
data-test-subj="resetSavedSearch" | ||
onClick={onResetQuery} | ||
size="s" | ||
aria-label={i18n.translate('discover.reloadSavedSearchButton', { | ||
defaultMessage: 'Reset search', | ||
})} | ||
> | ||
<FormattedMessage | ||
id="discover.reloadSavedSearchButton" | ||
defaultMessage="Reset search" | ||
/> | ||
</EuiButtonEmpty> | ||
</EuiFlexItem> | ||
)} | ||
</EuiFlexGroup> | ||
</I18nProvider> | ||
); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/plugins/discover/public/application/components/hits_counter/hits_counter_directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 { HitsCounter } from './hits_counter'; | ||
|
||
export function createHitsCounterDirective(reactDirective: any) { | ||
return reactDirective(HitsCounter, [ | ||
['hits', { watchDepth: 'reference' }], | ||
['showResetButton', { watchDepth: 'reference' }], | ||
['onResetQuery', { watchDepth: 'reference' }], | ||
]); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/plugins/discover/public/application/components/hits_counter/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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. | ||
*/ | ||
|
||
export { HitsCounter } from './hits_counter'; | ||
export { createHitsCounterDirective } from './hits_counter_directive'; |
27 changes: 27 additions & 0 deletions
27
src/plugins/discover/public/application/helpers/format_number_with_commas.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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. | ||
*/ | ||
|
||
const COMMA_SEPARATOR_RE = /(\d)(?=(\d{3})+(?!\d))/g; | ||
|
||
/** | ||
* Converts a number to a string and adds commas | ||
* as thousands separators | ||
*/ | ||
export const formatNumWithCommas = (input: number) => | ||
String(input).replace(COMMA_SEPARATOR_RE, '$1,'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters