-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Discover] Deangularize the hits counter and create a react component #65631
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
Merged
stratoula
merged 6 commits into
elastic:master
from
stratoula:2020-06-05-deangulrize-result-counter
May 8, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
954e568
Deangularize the hits counter and create a react component
stratoula 5acf857
Merge branch 'master' into 2020-06-05-deangulrize-result-counter
elasticmachine e22ab4e
Add aria-label to button for accessibility
stratoula 6220232
Add icon to the link button and use EuiText
stratoula 5dac30f
Remove snapshots and test with findTestSubject
stratoula c0ed9f4
Change toString with String()
stratoula File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.