-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'edge' into EXEC-655-store-commands-error-list-in-db
- Loading branch information
Showing
26 changed files
with
1,110 additions
and
67 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
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,74 @@ | ||
import { css } from 'styled-components' | ||
|
||
import { | ||
ALIGN_CENTER, | ||
BORDERS, | ||
Btn, | ||
COLORS, | ||
DIRECTION_COLUMN, | ||
Flex, | ||
Icon, | ||
JUSTIFY_SPACE_BETWEEN, | ||
SPACING, | ||
LegacyStyledText, | ||
truncateString, | ||
} from '@opentrons/components' | ||
|
||
const FILE_UPLOAD_STYLE = css` | ||
&:hover > svg { | ||
background: ${COLORS.black90}${COLORS.opacity20HexCode}; | ||
} | ||
&:active > svg { | ||
background: ${COLORS.black90}${COLORS.opacity20HexCode}}; | ||
} | ||
` | ||
|
||
const FILE_UPLOAD_FOCUS_VISIBLE = css` | ||
&:focus-visible { | ||
border-radius: ${BORDERS.borderRadius4}; | ||
box-shadow: 0 0 0 ${SPACING.spacing2} ${COLORS.blue50}; | ||
} | ||
` | ||
|
||
interface FileUploadProps { | ||
file: File | ||
fileError: string | null | ||
handleClick: () => unknown | ||
} | ||
|
||
export function FileUpload({ | ||
file, | ||
fileError, | ||
handleClick, | ||
}: FileUploadProps): JSX.Element { | ||
return ( | ||
<Flex flexDirection={DIRECTION_COLUMN} gridGap={SPACING.spacing4}> | ||
<Btn | ||
onClick={handleClick} | ||
aria-label="remove_file" | ||
css={FILE_UPLOAD_FOCUS_VISIBLE} | ||
> | ||
<Flex | ||
alignItems={ALIGN_CENTER} | ||
backgroundColor={fileError == null ? COLORS.grey20 : COLORS.red30} | ||
borderRadius={BORDERS.borderRadius4} | ||
height={SPACING.spacing44} | ||
justifyContent={JUSTIFY_SPACE_BETWEEN} | ||
padding={SPACING.spacing8} | ||
css={FILE_UPLOAD_STYLE} | ||
> | ||
<LegacyStyledText as="p"> | ||
{truncateString(file.name, 34, 19)} | ||
</LegacyStyledText> | ||
<Icon name="close" size="1.5rem" borderRadius="50%" /> | ||
</Flex> | ||
</Btn> | ||
{fileError != null ? ( | ||
<LegacyStyledText as="label" color={COLORS.red50}> | ||
{fileError} | ||
</LegacyStyledText> | ||
) : null} | ||
</Flex> | ||
) | ||
} |
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
168 changes: 168 additions & 0 deletions
168
opentrons-ai-client/src/molecules/UploadInput/index.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,168 @@ | ||
import * as React from 'react' | ||
import styled, { css } from 'styled-components' | ||
import { useTranslation } from 'react-i18next' | ||
import { | ||
ALIGN_CENTER, | ||
BORDERS, | ||
COLORS, | ||
CURSOR_POINTER, | ||
DIRECTION_COLUMN, | ||
DISPLAY_FLEX, | ||
Flex, | ||
Icon, | ||
JUSTIFY_CENTER, | ||
LegacyStyledText, | ||
POSITION_FIXED, | ||
PrimaryButton, | ||
SPACING, | ||
TYPOGRAPHY, | ||
} from '@opentrons/components' | ||
|
||
const StyledLabel = styled.label` | ||
display: ${DISPLAY_FLEX}; | ||
cursor: ${CURSOR_POINTER}; | ||
flex-direction: ${DIRECTION_COLUMN}; | ||
align-items: ${ALIGN_CENTER}; | ||
width: 100%; | ||
padding: ${SPACING.spacing32}; | ||
border: 2px dashed ${COLORS.grey30}; | ||
border-radius: ${BORDERS.borderRadius4}; | ||
text-align: ${TYPOGRAPHY.textAlignCenter}; | ||
background-color: ${COLORS.white}; | ||
&:hover { | ||
border: 2px dashed ${COLORS.blue50}; | ||
} | ||
` | ||
const DRAG_OVER_STYLES = css` | ||
border: 2px dashed ${COLORS.blue50}; | ||
` | ||
|
||
const StyledInput = styled.input` | ||
position: ${POSITION_FIXED}; | ||
clip: rect(1px 1px 1px 1px); | ||
` | ||
|
||
export interface UploadInputProps { | ||
/** Callback function that is called when a file is uploaded. */ | ||
onUpload: (file: File) => unknown | ||
/** Optional callback function that is called when the upload button is clicked. */ | ||
onClick?: () => void | ||
/** Optional text for the upload button. If undefined, the button displays Upload */ | ||
uploadButtonText?: string | ||
/** Optional text or JSX element that is displayed above the upload button. */ | ||
uploadText?: string | JSX.Element | ||
/** Optional text or JSX element that is displayed in the drag and drop area. */ | ||
dragAndDropText?: string | JSX.Element | ||
} | ||
|
||
export function UploadInput(props: UploadInputProps): JSX.Element | null { | ||
const { | ||
dragAndDropText, | ||
onClick, | ||
onUpload, | ||
uploadButtonText, | ||
uploadText, | ||
} = props | ||
const { t } = useTranslation('protocol_info') | ||
|
||
const fileInput = React.useRef<HTMLInputElement>(null) | ||
const [isFileOverDropZone, setIsFileOverDropZone] = React.useState<boolean>( | ||
false | ||
) | ||
const [isHover, setIsHover] = React.useState<boolean>(false) | ||
const handleDrop: React.DragEventHandler<HTMLLabelElement> = e => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
Array.from(e.dataTransfer.files).forEach(f => onUpload(f)) | ||
setIsFileOverDropZone(false) | ||
} | ||
const handleDragEnter: React.DragEventHandler<HTMLLabelElement> = e => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
} | ||
const handleDragLeave: React.DragEventHandler<HTMLLabelElement> = e => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
setIsFileOverDropZone(false) | ||
setIsHover(false) | ||
} | ||
const handleDragOver: React.DragEventHandler<HTMLLabelElement> = e => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
setIsFileOverDropZone(true) | ||
setIsHover(true) | ||
} | ||
|
||
const handleClick: React.MouseEventHandler<HTMLButtonElement> = _event => { | ||
onClick != null ? onClick() : fileInput.current?.click() | ||
} | ||
|
||
const onChange: React.ChangeEventHandler<HTMLInputElement> = event => { | ||
;[...(event.target.files ?? [])].forEach(f => onUpload(f)) | ||
if ('value' in event.currentTarget) event.currentTarget.value = '' | ||
} | ||
|
||
return ( | ||
<Flex | ||
height="100%" | ||
flexDirection={DIRECTION_COLUMN} | ||
justifyContent={JUSTIFY_CENTER} | ||
alignItems={ALIGN_CENTER} | ||
gridGap={SPACING.spacing24} | ||
> | ||
{uploadText != null ? ( | ||
<> | ||
{typeof uploadText === 'string' ? ( | ||
<LegacyStyledText | ||
as="p" | ||
textAlign={TYPOGRAPHY.textAlignCenter} | ||
marginTop={SPACING.spacing16} | ||
> | ||
{uploadText} | ||
</LegacyStyledText> | ||
) : ( | ||
<>{uploadText}</> | ||
)} | ||
</> | ||
) : null} | ||
<PrimaryButton | ||
onClick={handleClick} | ||
id="UploadInput_protocolUploadButton" | ||
> | ||
{uploadButtonText ?? t('upload')} | ||
</PrimaryButton> | ||
|
||
<StyledLabel | ||
data-testid="file_drop_zone" | ||
onDrop={handleDrop} | ||
onDragOver={handleDragOver} | ||
onDragEnter={handleDragEnter} | ||
onDragLeave={handleDragLeave} | ||
onMouseEnter={() => { | ||
setIsHover(true) | ||
}} | ||
onMouseLeave={() => { | ||
setIsHover(false) | ||
}} | ||
css={isFileOverDropZone ? DRAG_OVER_STYLES : undefined} | ||
> | ||
<Icon | ||
width="4rem" | ||
color={isHover ? COLORS.blue50 : COLORS.grey60} | ||
name="upload" | ||
marginBottom={SPACING.spacing24} | ||
/> | ||
{dragAndDropText} | ||
<StyledInput | ||
id="file_input" | ||
data-testid="file_input" | ||
ref={fileInput} | ||
type="file" | ||
onChange={onChange} | ||
multiple | ||
/> | ||
</StyledLabel> | ||
</Flex> | ||
) | ||
} |
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
Oops, something went wrong.