Skip to content

Commit

Permalink
fix: fix Dropzone layout (#338)
Browse files Browse the repository at this point in the history
* test: fix dragZone stories

* refactor: use style const

* refactor: update dropZone style
  • Loading branch information
wadjih-bencheikh18 authored Nov 14, 2022
1 parent 225526d commit f2c23f8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
34 changes: 16 additions & 18 deletions src/components/drop-zone/DropZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export interface DropZoneProps {
) => void;
fileValidator?: <T extends File>(file: T) => FileError | FileError[] | null;
}

const centerStyle = css`
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
`;
export function DropZoneContainer(
props: DropZoneProps & {
children: JSX.Element;
Expand All @@ -30,7 +35,11 @@ export function DropZoneContainer(
);
}

export function DropZone(props: DropZoneProps) {
export function DropZone(
props: DropZoneProps & {
emptyText?: string;
},
) {
return <DropZoneContent {...props} />;
}

Expand All @@ -39,12 +48,14 @@ function DropZoneContent(
children?: JSX.Element;
isContainer?: boolean;
onClick?: MouseEventHandler<HTMLDivElement>;
emptyText?: string;
},
) {
const {
color = 'black',
children,
onDrop,
emptyText = 'Drag and drop your files here, or click to select files',
isContainer = false,
onClick,
fileValidator,
Expand All @@ -60,7 +71,6 @@ function DropZoneContent(
validator: fileValidator,
onDrop: handleOnDrop,
});

const getPropsOptions = useMemo(() => {
if (onClick) {
return { onClick };
Expand Down Expand Up @@ -102,14 +112,7 @@ function DropZoneContent(

<div style={{ fontSize: '1.5em' }}>
{isDragActive ? (
<div
css={css`
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
`}
>
<div css={centerStyle}>
<FaCloudUploadAlt
size={70}
css={css`
Expand All @@ -120,19 +123,14 @@ function DropZoneContent(
<p>Drop the files here</p>
</div>
) : isContainer ? null : (
<p>Drag and drop your files here, or click to select files</p>
<p css={centerStyle}>{emptyText}</p>
)}
</div>
</div>
<input
type="file"
css={css`
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
test-align: center;
`}
{...getInputProps()}
/>
Expand Down
11 changes: 9 additions & 2 deletions stories/components/drop-zone.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface DropZoneStoryProps {
}

function fileValidator(file: File) {
if (file.name.length > 20) {
if (file?.name?.length > 20) {
return {
message: 'File name is larger than 20 characters',
code: 'name-too-large',
Expand All @@ -26,13 +26,19 @@ function fileValidator(file: File) {
return null;
}

export function Control({ color }: DropZoneStoryProps) {
export function Control({
color,
emptyText,
}: DropZoneStoryProps & {
emptyText?: string;
}) {
const [files, setFiles] = useState<FileWithPath[]>([]);
return (
<div>
<DropZone
fileValidator={fileValidator}
color={color}
emptyText={emptyText}
onDrop={(files: FileWithPath[]) => {
setFiles(files);
}}
Expand All @@ -57,6 +63,7 @@ export function Control({ color }: DropZoneStoryProps) {

Control.args = {
color: 'black',
emptyText: 'Drag and drop your files',
};

export function DropZoneContainerControl({ color }: DropZoneStoryProps) {
Expand Down

0 comments on commit f2c23f8

Please sign in to comment.