-
Notifications
You must be signed in to change notification settings - Fork 9
Feature: File Upload #24
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
Open
KaiSpencer
wants to merge
12
commits into
NHSDigital:main
Choose a base branch
from
KaiSpencer:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d92ed5b
Initial files copied from Portal repo
f088d22
Add stories
KaiSpencer 02d6411
Add File Upload style import
KaiSpencer 9b67655
Add File Upload export
KaiSpencer cc8fdc7
File Upload Tests
KaiSpencer 645e49f
File Upload component
KaiSpencer 60b0aa9
Remove Portal specific readme
KaiSpencer 20cf41c
File Upload hint test
KaiSpencer 6738615
Tidy imports
KaiSpencer 148cb63
Merge pull request #1 from KaiSpencer/feature/file-upload
KaiSpencer de28c3d
File Upload review comments, simplification using existing components.
KaiSpencer c0f17cf
Review comments: adding more tests for conditionals
KaiSpencer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,3 +39,7 @@ | |
| .ribbonlink-demo { | ||
| padding: 20px; | ||
| } | ||
|
|
||
| .fileupload-demo { | ||
| padding: 20px; | ||
| } | ||
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
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,38 @@ | ||
| $nhsuk-input-border-colour: #0b0c0c; | ||
| $component-padding: nhsuk-spacing(1); | ||
|
|
||
| .nhsuk-file-upload { | ||
| @include nhsuk-font($size: 19); | ||
| margin-left: -$component-padding; | ||
| padding: $component-padding; | ||
|
|
||
| // The default file upload button in Safari does not | ||
| // support setting a custom font-size. Set `-webkit-appearance` | ||
| // to `button` to drop out of the native appearance so the | ||
| // font-size is set to 19px | ||
| // https://bugs.webkit.org/show_bug.cgi?id=224746 | ||
| &::-webkit-file-upload-button { | ||
| -webkit-appearance: button; | ||
| color: inherit; | ||
| font: inherit; | ||
| } | ||
|
|
||
| &:focus { | ||
| outline: $nhsuk-focus-width solid $nhsuk-focus-color; | ||
| // Use `box-shadow` to add border instead of changing `border-width` | ||
| // (which changes element size) and since `outline` is already used for the | ||
| // yellow focus state. | ||
| box-shadow: inset 0 0 0 4px $nhsuk-input-border-colour; | ||
| } | ||
|
|
||
| // Set "focus-within" to fix https://bugzilla.mozilla.org/show_bug.cgi?id=1430196 | ||
| // so that component receives focus in Firefox. | ||
| // This can't be set together with `:focus` as all versions of IE fail | ||
| // to recognise `focus-within` and don't set any styles from the block | ||
| // when it's a selector. | ||
| &:focus-within { | ||
| outline: $nhsuk-focus-width solid $nhsuk-focus-color; | ||
|
|
||
| box-shadow: inset 0 0 0 4px $nhsuk-input-border-colour; | ||
| } | ||
| } |
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 @@ | ||
| import classNames from 'classnames'; | ||
| import { ErrorMessage, Hint, Label } from 'nhsuk-react-components'; | ||
| import React, { HTMLProps } from 'react'; | ||
|
|
||
| interface FileUploadProps extends HTMLProps<HTMLDivElement> { | ||
| error?: string; | ||
| hint?: string; | ||
| } | ||
|
|
||
| const FileUpload: React.FC<FileUploadProps> = ({ error, hint, children, id, ...rest }) => { | ||
| return ( | ||
| <div className={classNames('nhsuk-form-group', { 'nhsuk-form-group--error': error })} {...rest}> | ||
| <Label htmlFor={id}>{children}</Label> | ||
| {error && <ErrorMessage>{error}</ErrorMessage>} | ||
| {hint && <Hint>{hint}</Hint>} | ||
| <input aria-describedby={id} id="file-upload" className="nhsuk-file-upload" type="file" /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default FileUpload; |
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,28 @@ | ||
| import React from 'react'; | ||
| import { mount, shallow } from 'enzyme'; | ||
| import FileUpload from '../FileUpload'; | ||
|
|
||
| describe('FileUpload', () => { | ||
| it('Matches snapshot', () => { | ||
| const component = shallow(<FileUpload>Upload</FileUpload>); | ||
| expect(component).toMatchSnapshot(); | ||
| component.unmount(); | ||
| }); | ||
| it('With Error', () => { | ||
| const component = mount(<FileUpload error="something wrong">Upload</FileUpload>); | ||
| expect(component).toMatchSnapshot(); | ||
|
|
||
| const renderedComponent = component.render(); | ||
| expect(renderedComponent.find('span').prop('class')).toBe('nhsuk-error-message'); | ||
|
|
||
| component.unmount(); | ||
| }); | ||
| it('With Hint', () => { | ||
| const component = mount(<FileUpload hint="Format: JPG">Upload</FileUpload>); | ||
| expect(component).toMatchSnapshot(); | ||
|
|
||
| const renderedComponent = component.render(); | ||
| expect(renderedComponent.find('span').prop('class')).toBe('nhsuk-hint'); | ||
| component.unmount(); | ||
| }); | ||
| }); | ||
89 changes: 89 additions & 0 deletions
89
src/components/file-upload/__tests__/__snapshots__/FileUpload.test.tsx.snap
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,89 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`FileUpload Matches snapshot 1`] = ` | ||
| <div | ||
| className="nhsuk-form-group" | ||
| > | ||
| <Label> | ||
| Upload | ||
| </Label> | ||
| <input | ||
| className="nhsuk-file-upload" | ||
| id="file-upload" | ||
| type="file" | ||
| /> | ||
| </div> | ||
| `; | ||
|
|
||
| exports[`FileUpload With Error 1`] = ` | ||
| <FileUpload | ||
| error="something wrong" | ||
| > | ||
| <div | ||
| className="nhsuk-form-group nhsuk-form-group--error" | ||
| > | ||
| <Label> | ||
| <BaseLabel> | ||
| <label | ||
| className="nhsuk-label" | ||
| > | ||
| Upload | ||
| </label> | ||
| </BaseLabel> | ||
| </Label> | ||
| <ErrorMessage | ||
| role="alert" | ||
| visuallyHiddenText="Error: " | ||
| > | ||
| <span | ||
| className="nhsuk-error-message" | ||
| role="alert" | ||
| > | ||
| <span | ||
| className="nhsuk-u-visually-hidden" | ||
| > | ||
| Error: | ||
| </span> | ||
| something wrong | ||
| </span> | ||
| </ErrorMessage> | ||
| <input | ||
| className="nhsuk-file-upload" | ||
| id="file-upload" | ||
| type="file" | ||
| /> | ||
| </div> | ||
| </FileUpload> | ||
| `; | ||
|
|
||
| exports[`FileUpload With Hint 1`] = ` | ||
| <FileUpload | ||
| hint="Format: JPG" | ||
| > | ||
| <div | ||
| className="nhsuk-form-group" | ||
| > | ||
| <Label> | ||
| <BaseLabel> | ||
| <label | ||
| className="nhsuk-label" | ||
| > | ||
| Upload | ||
| </label> | ||
| </BaseLabel> | ||
| </Label> | ||
| <Hint> | ||
| <span | ||
| className="nhsuk-hint" | ||
| > | ||
| Format: JPG | ||
| </span> | ||
| </Hint> | ||
| <input | ||
| className="nhsuk-file-upload" | ||
| id="file-upload" | ||
| type="file" | ||
| /> | ||
| </div> | ||
| </FileUpload> | ||
| `; |
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,3 @@ | ||
| import FileUpload from './FileUpload'; | ||
|
|
||
| export default FileUpload; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React from 'react'; | ||
| import { storiesOf } from '@storybook/react'; | ||
| import { FileUpload } from '../src'; | ||
|
|
||
| const stories = storiesOf('File Upload', module); | ||
|
|
||
| stories | ||
| .add('Standard', () => ( | ||
| <div className="fileupload-demo"> | ||
| <FileUpload>Upload a file</FileUpload> | ||
| </div> | ||
| )) | ||
| .add('With Error', () => ( | ||
| <div className="fileupload-demo"> | ||
| <FileUpload error="Invalid file type">Upload a file</FileUpload> | ||
| </div> | ||
| )) | ||
| .add('With Hint', () => ( | ||
| <div className="fileupload-demo"> | ||
| <FileUpload hint="Maximum file size 2GB">Upload a File</FileUpload> | ||
| </div> | ||
| )); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd want a couple more tests just to check that certain parts of the conditional rendering is working properly - i.e. check for a
span.nhsuk-error-messageto exist etc.