-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enterprise Search] Added reusable HiddenText component to Credentials (
- Loading branch information
1 parent
40afb87
commit 933a023
Showing
7 changed files
with
254 additions
and
35 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
56 changes: 56 additions & 0 deletions
56
...earch/public/applications/app_search/components/credentials/credentials_list/key.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,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { EuiButtonIcon } from '@elastic/eui'; | ||
|
||
import { Key } from './key'; | ||
|
||
describe('Key', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const props = { | ||
copy: jest.fn(), | ||
toggleIsHidden: jest.fn(), | ||
isHidden: true, | ||
text: 'some-api-key', | ||
}; | ||
|
||
it('renders', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
expect(wrapper.find(EuiButtonIcon).length).toEqual(2); | ||
}); | ||
|
||
it('will call copy when the first button is clicked', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
wrapper.find(EuiButtonIcon).first().simulate('click'); | ||
expect(props.copy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('will call hide when the second button is clicked', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
wrapper.find(EuiButtonIcon).last().simulate('click'); | ||
expect(props.toggleIsHidden).toHaveBeenCalled(); | ||
}); | ||
|
||
it('will render the "eye" icon when isHidden is true', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
expect(wrapper.find(EuiButtonIcon).last().prop('iconType')).toBe('eye'); | ||
}); | ||
|
||
it('will render the "eyeClosed" icon when isHidden is false', () => { | ||
const wrapper = shallow(<Key {...{ ...props, isHidden: false }} />); | ||
expect(wrapper.find(EuiButtonIcon).last().prop('iconType')).toBe('eyeClosed'); | ||
}); | ||
|
||
it('will render the provided text', () => { | ||
const wrapper = shallow(<Key {...props} />); | ||
expect(wrapper.text()).toContain('some-api-key'); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
...ise_search/public/applications/app_search/components/credentials/credentials_list/key.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,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { EuiButtonIcon } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
interface IProps { | ||
copy: () => void; | ||
toggleIsHidden: () => void; | ||
isHidden: boolean; | ||
text: React.ReactNode; | ||
} | ||
|
||
export const Key: React.FC<IProps> = ({ copy, toggleIsHidden, isHidden, text }) => { | ||
const hideIcon = isHidden ? 'eye' : 'eyeClosed'; | ||
const hideIconLabel = isHidden | ||
? i18n.translate('xpack.enterpriseSearch.appSearch.credentials.showApiKey', { | ||
defaultMessage: 'Show API Key', | ||
}) | ||
: i18n.translate('xpack.enterpriseSearch.appSearch.credentials.hideApiKey', { | ||
defaultMessage: 'Hide API Key', | ||
}); | ||
|
||
return ( | ||
<> | ||
<EuiButtonIcon | ||
onClick={copy} | ||
iconType="copyClipboard" | ||
aria-label={i18n.translate('xpack.enterpriseSearch.appSearch.credentials.copyApiKey', { | ||
defaultMessage: 'Copy API Key to clipboard', | ||
})} | ||
/> | ||
<EuiButtonIcon | ||
onClick={toggleIsHidden} | ||
iconType={hideIcon} | ||
aria-label={hideIconLabel} | ||
aria-pressed={!isHidden} | ||
/> | ||
{text} | ||
</> | ||
); | ||
}; |
61 changes: 61 additions & 0 deletions
61
x-pack/plugins/enterprise_search/public/applications/shared/hidden_text/hidden_text.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,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { HiddenText } from '.'; | ||
|
||
describe('HiddenText', () => { | ||
it('provides the passed "text" in a "hiddenText" field, with all characters obfuscated', () => { | ||
const wrapper = shallow( | ||
<HiddenText text="hidden_test"> | ||
{({ hiddenText, isHidden, toggle }) => <div>{hiddenText}</div>} | ||
</HiddenText> | ||
); | ||
expect(wrapper.text()).toEqual('•••••••••••'); | ||
}); | ||
|
||
it('provides a "toggle" function, which when called, changes "hiddenText" to the original unobfuscated text', () => { | ||
let toggleFn = () => {}; | ||
|
||
const wrapper = shallow( | ||
<HiddenText text="hidden_test"> | ||
{({ hiddenText, isHidden, toggle }) => { | ||
toggleFn = toggle; | ||
return <div>{hiddenText}</div>; | ||
}} | ||
</HiddenText> | ||
); | ||
|
||
expect(wrapper.text()).toEqual('•••••••••••'); | ||
toggleFn(); | ||
expect(wrapper.text()).toEqual('hidden_test'); | ||
toggleFn(); | ||
expect(wrapper.text()).toEqual('•••••••••••'); | ||
}); | ||
|
||
it('provides a "hidden" boolean, which which tracks whether or not the text is obfuscated or not', () => { | ||
let toggleFn = () => {}; | ||
let isHiddenBool = false; | ||
|
||
shallow( | ||
<HiddenText text="hidden_test"> | ||
{({ hiddenText, isHidden, toggle }) => { | ||
isHiddenBool = isHidden; | ||
toggleFn = toggle; | ||
return <div>{hiddenText}</div>; | ||
}} | ||
</HiddenText> | ||
); | ||
|
||
expect(isHiddenBool).toEqual(true); | ||
toggleFn(); | ||
expect(isHiddenBool).toEqual(false); | ||
toggleFn(); | ||
expect(isHiddenBool).toEqual(true); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
x-pack/plugins/enterprise_search/public/applications/shared/hidden_text/hidden_text.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,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useState, ReactElement } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
interface IChildrenProps { | ||
toggle: () => void; | ||
isHidden: boolean; | ||
hiddenText: React.ReactNode; | ||
} | ||
|
||
interface IProps { | ||
text: string; | ||
children(props: IChildrenProps): ReactElement; | ||
} | ||
|
||
export const HiddenText: React.FC<IProps> = ({ text, children }) => { | ||
const [isHidden, toggleIsHidden] = useState(true); | ||
|
||
const hiddenLabel = i18n.translate('xpack.enterpriseSearch.hiddenText', { | ||
defaultMessage: 'Hidden text', | ||
}); | ||
const hiddenText = isHidden ? ( | ||
<span aria-label={hiddenLabel}>{text.replace(/./g, '•')}</span> | ||
) : ( | ||
text | ||
); | ||
|
||
return children({ | ||
hiddenText, | ||
isHidden, | ||
toggle: () => toggleIsHidden(!isHidden), | ||
}); | ||
}; |
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/enterprise_search/public/applications/shared/hidden_text/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,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export { HiddenText } from './hidden_text'; |