Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/file-selector/src/lib/FileSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,48 @@ describe('FileSelector', () => {

expect(getByText('Upload file')).toBeTruthy();
});

test('renders default HeaderMessage when customHeaderMessage is not provided and enableDropArea is false', () => {
const client = new QueryClient();
const { getByText } = render(
<QueryClientProvider client={client}>
<TestForm>
<FileSelector
name="test"
bucketId="test"
customerId="123"
clientId="test"
maxSize={10485760}
maxFiles={10}
enableDropArea={false}
/>
</TestForm>
</QueryClientProvider>
);

expect(getByText(/Attach up to 10 file\(s\)/)).toBeTruthy();
});

test('renders customHeaderMessage instead of default HeaderMessage when provided', () => {
const client = new QueryClient();
const { getByText, queryByText } = render(
<QueryClientProvider client={client}>
<TestForm>
<FileSelector
name="test"
bucketId="test"
customerId="123"
clientId="test"
maxSize={10485760}
maxFiles={10}
enableDropArea={false}
customHeaderMessage="Attach up to 10 file(s), max 5 MB per file, 50 MB total"
/>
</TestForm>
</QueryClientProvider>
);

expect(getByText('Attach up to 10 file(s), max 5 MB per file, 50 MB total')).toBeTruthy();
expect(queryByText(/maximum individual size/)).toBeNull();
});
});
17 changes: 15 additions & 2 deletions packages/file-selector/src/lib/FileSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ export type FileSelectorProps = {
* @returns Custom error message string, or undefined to use default
*/
getErrorMessage?: GetErrorMessageFn;
/**
* Custom header message to replace the auto-generated "Attach up to X file(s)..." text.
* Only used when `enableDropArea` is `false`. When provided, the default `HeaderMessage`
* is not rendered.
*/
customHeaderMessage?: ReactNode;
};

export const FileSelector = ({
Expand Down Expand Up @@ -184,6 +190,7 @@ export const FileSelector = ({
validator,
disableRemove,
getErrorMessage,
customHeaderMessage,
}: FileSelectorProps) => {
const [totalSize, setTotalSize] = useState(0);
const [fileRejections, setFileRejections] = useState<(FileRejection & { id: number })[]>([]);
Expand Down Expand Up @@ -283,7 +290,11 @@ export const FileSelector = ({
) : (
<Grid container rowSpacing={3} flexDirection="column">
<Grid>
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} maxTotalSize={maxTotalSize} />
{customHeaderMessage ? (
<Typography variant="h6">{customHeaderMessage}</Typography>
) : (
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} maxTotalSize={maxTotalSize} />
)}
<FileTypesMessage
allowedFileTypes={allowedFileTypes}
customTotalSizeMessage={customTotalSizeMessage}
Expand Down Expand Up @@ -316,7 +327,9 @@ export const FileSelector = ({
{tooManyFilesRejections.length > 0 && (
<Alert
severity="error"
onClose={() => { for (const rejection of tooManyFilesRejections) handleRemoveRejection(rejection.id) }}
onClose={() => {
for (const rejection of tooManyFilesRejections) handleRemoveRejection(rejection.id);
}}
>
<AlertTitle>Items not allowed.</AlertTitle>
Too many files are selected for upload, maximum {maxFiles} allowed.
Expand Down
38 changes: 38 additions & 0 deletions packages/file-selector/src/lib/FileSelector2.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,42 @@ describe('FileSelector2', () => {

expect(getByText('Upload file')).toBeTruthy();
});

test('renders default HeaderMessage when customHeaderMessage is not provided and enableDropArea is false', () => {
const { getByText } = render(
<TestForm>
<FileSelector2
name="test"
bucketId="test"
customerId="123"
clientId="test"
maxSize={10485760}
maxFiles={10}
enableDropArea={false}
/>
</TestForm>
);

expect(getByText(/Attach up to 10 file\(s\)/)).toBeTruthy();
});

test('renders customHeaderMessage instead of default HeaderMessage when provided', () => {
const { getByText, queryByText } = render(
<TestForm>
<FileSelector2
name="test"
bucketId="test"
customerId="123"
clientId="test"
maxSize={10485760}
maxFiles={10}
enableDropArea={false}
customHeaderMessage="Attach up to 10 file(s), max 5 MB per file, 50 MB total"
/>
</TestForm>
);

expect(getByText('Attach up to 10 file(s), max 5 MB per file, 50 MB total')).toBeTruthy();
expect(queryByText(/maximum individual size/)).toBeNull();
});
});
11 changes: 9 additions & 2 deletions packages/file-selector/src/lib/FileSelector2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const FileSelector2 = ({
validator,
disableRemove,
getErrorMessage,
customHeaderMessage,
}: FileSelector2Props) => {
const [totalSize, setTotalSize] = useState(0);
const [fileRejections, setFileRejections] = useState<(FileRejection & { id: number })[]>([]);
Expand Down Expand Up @@ -160,7 +161,11 @@ export const FileSelector2 = ({
) : (
<Grid container rowSpacing={3} flexDirection="column">
<Grid>
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} maxTotalSize={maxTotalSize} />
{customHeaderMessage ? (
<Typography variant="h6">{customHeaderMessage}</Typography>
) : (
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} maxTotalSize={maxTotalSize} />
)}
<FileTypesMessage
allowedFileTypes={allowedFileTypes}
customSizeMessage={customSizeMessage}
Expand Down Expand Up @@ -193,7 +198,9 @@ export const FileSelector2 = ({
{tooManyFilesRejections.length > 0 && (
<Alert
severity="error"
onClose={() => { for (const rejection of tooManyFilesRejections) handleRemoveRejection(rejection.id) }}
onClose={() => {
for (const rejection of tooManyFilesRejections) handleRemoveRejection(rejection.id);
}}
>
<AlertTitle>Items not allowed.</AlertTitle>
Too many files are selected for upload, maximum {maxFiles} allowed.
Expand Down
Loading