-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LG-6066: Implement print button on FSMv2 Personal Key step (#6205)
* LG-6066: Implement print button on FSMv2 Personal Key step **Why**: - So that we can retain feature parity with the existing screen. - To reduce size and scope of common application bundle - To create a more rigid connection between print JavaScript functionality and server-side render logic - To improve test coverage for existing print button behavior changelog: Upcoming Features, Identity Verification, Add personal key step screen * Create README.md
- Loading branch information
Showing
24 changed files
with
206 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class PrintButtonComponent < ButtonComponent | ||
attr_reader :tag_options | ||
|
||
def initialize(**tag_options) | ||
super(**tag_options, type: :button, icon: :print) | ||
end | ||
|
||
def call | ||
content_tag(:'lg-print-button', super) | ||
end | ||
|
||
def content | ||
t('components.print_button.label') | ||
end | ||
end |
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 @@ | ||
import '@18f/identity-print-button'; |
This file was deleted.
Oops, something went wrong.
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,35 @@ | ||
# `@18f/identity-print-button` | ||
|
||
Custom element and React implementation for a print button component. | ||
|
||
## Usage | ||
|
||
### Custom Element | ||
|
||
Importing the package will register the `<lg-print-button>` custom element: | ||
|
||
```ts | ||
import '@18f/identity-print-button'; | ||
``` | ||
|
||
The custom element will implement the behavior to show a print dialog upon click, but all markup must already exist, rendered server-side or by the included React component. | ||
|
||
```html | ||
<lg-print-button> | ||
<button type="button">Print</button> | ||
</lg-print-button> | ||
``` | ||
|
||
### React | ||
|
||
The package exports a `PrintButton` component, which extends the `Button` component from `@18f/identity-components`. | ||
|
||
```tsx | ||
import { PrintButton } from '@18f/identity-print-button'; | ||
|
||
export function Example() { | ||
return ( | ||
<PrintButton isOutline /> | ||
); | ||
} | ||
``` |
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,3 @@ | ||
import './print-button-element'; | ||
|
||
export { default as PrintButton } from './print-button'; |
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,13 @@ | ||
{ | ||
"name": "@18f/identity-print-button", | ||
"version": "1.0.0", | ||
"private": true, | ||
"peerDependencies": { | ||
"react": "*" | ||
}, | ||
"peerDependenciesMeta": { | ||
"react": { | ||
"optional": true | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/javascript/packages/print-button/print-button-element.spec.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,25 @@ | ||
import sinon from 'sinon'; | ||
import { screen } from '@testing-library/dom'; | ||
import userEvent from '@testing-library/user-event'; | ||
import './print-button-element'; | ||
|
||
describe('PrintButtonElement', () => { | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
sandbox.stub(window, 'print'); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('prints when clicked', () => { | ||
document.body.innerHTML = `<lg-print-button><button type="button">Print</button><lg-print-button>`; | ||
const button = screen.getByRole('button'); | ||
|
||
userEvent.click(button); | ||
|
||
expect(window.print).to.have.been.called(); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
app/javascript/packages/print-button/print-button-element.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,17 @@ | ||
class PrintButtonElement extends HTMLElement { | ||
connectedCallback() { | ||
this.addEventListener('click', () => window.print()); | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'lg-print-button': PrintButtonElement; | ||
} | ||
} | ||
|
||
if (!customElements.get('lg-print-button')) { | ||
customElements.define('lg-print-button', PrintButtonElement); | ||
} | ||
|
||
export default PrintButtonElement; |
35 changes: 35 additions & 0 deletions
35
app/javascript/packages/print-button/print-button.spec.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,35 @@ | ||
import sinon from 'sinon'; | ||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import PrintButton from './print-button'; | ||
|
||
describe('PrintButton', () => { | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
sandbox.stub(window, 'print'); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('renders a button that prints when clicked', () => { | ||
const { getByRole } = render(<PrintButton />); | ||
|
||
const button = getByRole('button', { name: 'components.print_button.label' }); | ||
|
||
userEvent.click(button); | ||
|
||
expect(window.print).to.have.been.called(); | ||
}); | ||
|
||
it('forwards all other props to the button child', () => { | ||
const { getByRole } = render(<PrintButton isOutline />); | ||
|
||
const button = getByRole('button', { name: 'components.print_button.label' }); | ||
|
||
expect(button.closest('lg-print-button')).to.exist(); | ||
expect(button.classList.contains('usa-button--outline')).to.be.true(); | ||
}); | ||
}); |
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,24 @@ | ||
import type { HTMLAttributes } from 'react'; | ||
import { t } from '@18f/identity-i18n'; | ||
import { Button } from '@18f/identity-components'; | ||
import type { ButtonProps } from '@18f/identity-components'; | ||
import type PrintButtonElement from './print-button-element'; | ||
import './print-button-element'; | ||
|
||
declare global { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
'lg-print-button': HTMLAttributes<PrintButtonElement> & { class?: string }; | ||
} | ||
} | ||
} | ||
|
||
function PrintButton(buttonProps: ButtonProps) { | ||
return ( | ||
<lg-print-button> | ||
<Button {...buttonProps}>{t('components.print_button.label')}</Button> | ||
</lg-print-button> | ||
); | ||
} | ||
|
||
export default PrintButton; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
require('../app/components/index'); | ||
require('../app/print-personal-key'); | ||
require('../app/i18n-dropdown'); |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe PrintButtonComponent, type: :component do | ||
let(:tag_options) { {} } | ||
|
||
subject(:rendered) do | ||
render_inline PrintButtonComponent.new(**tag_options) | ||
end | ||
|
||
it 'renders custom element with button' do | ||
expect(rendered).to have_css( | ||
'lg-print-button button[type="button"]', | ||
text: t('components.print_button.label'), | ||
) | ||
end | ||
|
||
context 'with tag options' do | ||
let(:tag_options) { { outline: true, data: { foo: 'bar' } } } | ||
|
||
it 'renders with tag options forwarded to button' do | ||
expect(rendered).to have_css( | ||
'lg-print-button button.usa-button--outline:not([outline])[data-foo="bar"]', | ||
) | ||
end | ||
end | ||
end |