Skip to content

Commit 7b0562d

Browse files
authored
Add onFileRemoved callback to useImperativeFilePicker (#80)
* Add onFileRemoved callback to useImperativeFilePicker * fix validator example
1 parent 65bd6cc commit 7b0562d

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ export default function App() {
255255
// this callback is called when there were no validation errors
256256
console.log('onFilesSuccessfullySelected', plainFiles, filesContent);
257257
},
258+
onClear: () => {
259+
// this callback is called when the selection is cleared
260+
console.log('onClear');
261+
},
262+
});
263+
}
264+
```
265+
266+
`useImperativePicker` hook also accepts the callbacks listed above. Additionally, it accepts the `onFileRemoved` callback, which is called when a file is removed from the list of selected files.
267+
268+
```ts
269+
import { useImperativeFilePicker } from 'use-file-picker';
270+
271+
export default function App() {
272+
const { openFilePicker, filesContent, loading, errors, plainFiles, clear } = useImperativeFilePicker({
273+
onFileRemoved: (removedFile, removedIndex) => {
274+
// this callback is called when a file is removed from the list of selected files
275+
console.log('onFileRemoved', removedFile, removedIndex);
276+
},
258277
});
259278
}
260279
```
@@ -404,9 +423,10 @@ Since version 2.0, validators also have optional callback handlers that will be
404423
/**
405424
* This method is called when file is removed from the list of selected files.
406425
* Invoked only by the useImperativeFilePicker hook
426+
* @param _removedFile removed file
407427
* @param _removedIndex index of removed file
408428
*/
409-
onFileRemoved(_removedIndex: number): Promise<void> | void {}
429+
onFileRemoved(_removedFile: File, _removedIndex: number): Promise<void> | void {}
410430
```
411431

412432
#### Example validator

example/imperative.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const Imperative = () => {
2525
// this callback is called when there were no validation errors
2626
console.log('onFilesSuccessfullySelected', plainFiles, filesContent);
2727
},
28+
onFileRemoved(file, removedIndex) {
29+
// this callback is called when file is removed
30+
console.log('onFileRemoved', file, removedIndex);
31+
},
2832
});
2933

3034
if (loading) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "use-file-picker",
33
"description": "Simple react hook to open browser file selector.",
4-
"version": "2.0.0",
4+
"version": "2.1.0",
55
"license": "MIT",
66
"author": "Milosz Jankiewicz",
77
"homepage": "https://github.com/Jaaneek/useFilePicker",

src/interfaces.ts

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ export type ExtractContentTypeFromConfig<Config> = Config extends { readAs: 'Arr
118118
export type UseFilePickerConfig<CustomErrors = unknown> = UseFilePickerConfigCommon &
119119
ReadFileContentConfig<CustomErrors>;
120120

121+
export type useImperativeFilePickerConfig<CustomErrors = unknown> = UseFilePickerConfig<CustomErrors> & {
122+
onFileRemoved?: (file: FileWithPath, removedIndex: number) => void | Promise<void>;
123+
};
124+
121125
export interface FileContent<ContentType> extends Blob {
122126
lastModified: number;
123127
name: string;

src/useImperativeFilePicker.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ImperativeFilePickerReturnTypes,
66
SelectedFiles,
77
SelectedFilesOrErrors,
8-
UseFilePickerConfig,
8+
useImperativeFilePickerConfig,
99
} from './interfaces';
1010
import useFilePicker from './useFilePicker';
1111

@@ -14,9 +14,9 @@ import useFilePicker from './useFilePicker';
1414
*/
1515
function useImperativeFilePicker<
1616
CustomErrors = unknown,
17-
ConfigType extends UseFilePickerConfig<CustomErrors> = UseFilePickerConfig<CustomErrors>
17+
ConfigType extends useImperativeFilePickerConfig<CustomErrors> = useImperativeFilePickerConfig<CustomErrors>
1818
>(props: ConfigType): ImperativeFilePickerReturnTypes<ExtractContentTypeFromConfig<ConfigType>, CustomErrors> {
19-
const { readFilesContent, onFilesSelected, onFilesSuccessfullySelected } = props;
19+
const { readFilesContent, onFilesSelected, onFilesSuccessfullySelected, validators, onFileRemoved } = props;
2020

2121
const [allPlainFiles, setAllPlainFiles] = useState<File[]>([]);
2222
const [allFilesContent, setAllFilesContent] = useState<FileContent<ExtractContentTypeFromConfig<ConfigType>>[]>([]);
@@ -59,17 +59,18 @@ function useImperativeFilePicker<
5959

6060
const removeFileByIndex = useCallback(
6161
(index: number) => {
62-
setAllPlainFiles(previousPlainFiles => [
63-
...previousPlainFiles.slice(0, index),
64-
...previousPlainFiles.slice(index + 1),
65-
]);
6662
setAllFilesContent(previousFilesContent => [
6763
...previousFilesContent.slice(0, index),
6864
...previousFilesContent.slice(index + 1),
6965
]);
70-
props.validators?.forEach(validator => validator.onFileRemoved?.(index));
66+
setAllPlainFiles(previousPlainFiles => {
67+
const removedFile = previousPlainFiles[index];
68+
validators?.forEach(validator => validator.onFileRemoved?.(removedFile, index));
69+
onFileRemoved?.(removedFile, index);
70+
return [...previousPlainFiles.slice(0, index), ...previousPlainFiles.slice(index + 1)];
71+
});
7172
},
72-
[props.validators]
73+
[validators, onFileRemoved]
7374
);
7475

7576
const removeFileByReference = useCallback(

src/validators/persistentFileAmountLimitValidator/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PersistentFileAmountLimitValidator extends Validator {
1212
this.previousPlainFiles = [];
1313
}
1414

15-
onFileRemoved(removedIndex: number): void {
15+
onFileRemoved(_removedFile: File, removedIndex: number): void {
1616
this.previousPlainFiles.splice(removedIndex, 1);
1717
}
1818

src/validators/validatorBase.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export abstract class Validator<
4949
/**
5050
* This method is called when file is removed from the list of selected files.
5151
* Invoked only by the useImperativeFilePicker hook
52+
* @param _removedFile removed file
5253
* @param _removedIndex index of removed file
5354
*/
54-
onFileRemoved(_removedIndex: number): Promise<void> | void {}
55+
onFileRemoved(_removedFile: File, _removedIndex: number): Promise<void> | void {}
5556
}

0 commit comments

Comments
 (0)