Skip to content

Commit

Permalink
Fix(next-app): FileUploaderInput #DS-1508
Browse files Browse the repository at this point in the history
  • Loading branch information
curdaj committed Oct 17, 2024
1 parent dbd2d5d commit 663e00c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
69 changes: 69 additions & 0 deletions examples/next-with-app-router/src/app/fileuploader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use client';

import React, { useState } from 'react';
import {
FileUploader,
FileUploaderAttachment,
FileUploaderInput,
FileUploaderList,
SpiritFileUploaderAttachmentProps,
useFileQueue,
} from '@lmc-eu/spirit-web-react';

const FileuploaderTest = () => {
const [errorMessage, setErrorMessage] = useState<(string | Error)[]>([]);

const {
fileQueue: fileQueueWarning,
addToQueue: addToQueueWarning,
clearQueue: clearQueueWarning,
onDismiss: onDismissWarning,
findInQueue: findInQueueWarning,
updateQueue: updateQueueWarning,
} = useFileQueue();

const attachmentComponent = ({ id, ...props }: SpiritFileUploaderAttachmentProps) => (
<FileUploaderAttachment key={id} id={id} {...props} />
);

return (
<FileUploader
addToQueue={(key, file) => {
return addToQueueWarning(key, file);
}}
clearQueue={clearQueueWarning}
fileQueue={fileQueueWarning}
findInQueue={findInQueueWarning}
id="file-uploader-validation-states-warning"
onDismiss={onDismissWarning}
updateQueue={updateQueueWarning}
>
<FileUploaderInput
helperText="Max file size is 50 kb"
id="file-uploader-validation-states-warning-input"
label="Label"
labelText="or drag and drop here"
linkText="Upload your file"
name="attachmentsWarning"
onError={(error) => {
setErrorMessage((prevErrors) => [...prevErrors, error]);
}}
validationText={`${errorMessage.join(', ')}`}
validationState="warning"
isRequired
isMultiple
maxFileSize={51200} // 50 kb
// add onInput callback should solve issue if upload files by select them (not for drag and drop)
// onInput={() => setErrorMessage([])}
/>
<FileUploaderList
attachmentComponent={attachmentComponent}
id="file-uploader-validation-states-warning-attachment"
inputName="attachmentsWarning"
label="Attachments"
/>
</FileUploader>
);
};

export default FileuploaderTest;
4 changes: 2 additions & 2 deletions examples/next-with-app-router/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Heading } from '@lmc-eu/spirit-web-react';
import { NextPage } from 'next';
import FileuploaderTest from '@/app/fileuploader';

const Home: NextPage = () => <Heading size="large">Spirit App Router</Heading>;
const Home: NextPage = () => <FileuploaderTest />;

export default Home;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import classNames from 'classnames';
import React from 'react';
import React, { useEffect, useState } from 'react';
import { useDeprecationMessage, useStyleProps } from '../../hooks';
import { SpiritFileUploaderInputProps } from '../../types';
import { HelperText, ValidationText, useAriaIds } from '../Field';
Expand All @@ -11,6 +11,7 @@ import { useFileUploaderInput } from './useFileUploaderInput';
import { useFileUploaderStyleProps } from './useFileUploaderStyleProps';

const FileUploaderInput = (props: SpiritFileUploaderInputProps) => {
const [isDragAndDropSupported, setIsDragAndDropSupported] = useState(false);
const {
accept,
'aria-describedby': ariaDescribedBy = '',
Expand All @@ -34,10 +35,6 @@ const FileUploaderInput = (props: SpiritFileUploaderInputProps) => {
validationText,
...restProps
} = props;

const isDragAndDropSupported =
typeof document !== 'undefined' ? 'draggable' in document.createElement('span') : false;

const {
isDisabledByQueueLimitBehavior,
isDragging,
Expand Down Expand Up @@ -67,9 +64,12 @@ const FileUploaderInput = (props: SpiritFileUploaderInputProps) => {
validationState,
});
const { styleProps, props: transferProps } = useStyleProps(restProps);

const [ids, register] = useAriaIds(ariaDescribedBy);

useEffect(() => {
setIsDragAndDropSupported('draggable' in document.createElement('span'));
}, []);

useDeprecationMessage({
method: 'custom',
trigger: !id,
Expand Down

0 comments on commit 663e00c

Please sign in to comment.