Skip to content

Commit 1fa91c6

Browse files
authored
feat(FileUploader): add onDelete hook to standard uploader (#5935)
1 parent d865f82 commit 1fa91c6

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,9 @@ Map {
23492349
"onClick": Object {
23502350
"type": "func",
23512351
},
2352+
"onDelete": Object {
2353+
"type": "func",
2354+
},
23522355
"size": Object {
23532356
"args": Array [
23542357
Array [

packages/react/src/components/FileUploader/FileUploader-story.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ const props = {
9494
'Close button icon description (iconDescription)',
9595
'Clear file'
9696
),
97+
onChange: action('onChange'),
98+
onClick: action('onClick'),
99+
onDelete: action('onDelete'),
97100
};
98101
},
99102
fileUploaderItem: () => ({

packages/react/src/components/FileUploader/FileUploader.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import Loading from '../Loading';
1919
import uid from '../../tools/uniqueId';
2020
import { ButtonKinds } from '../../prop-types/types';
21+
import { keys, matches } from '../../internal/keyboard';
2122

2223
const { prefix } = settings;
2324

@@ -308,8 +309,14 @@ export default class FileUploader extends Component {
308309
onChange: PropTypes.func,
309310

310311
/**
311-
* Provide an optional `onClick` hook that is called each time the button is
312-
* clicked
312+
* Provide an optional `onDelete` hook that is called when an uploaded item
313+
* is removed
314+
*/
315+
onDelete: PropTypes.func,
316+
317+
/**
318+
* Provide an optional `onClick` hook that is called each time the
319+
* FileUploader is clicked
313320
*/
314321
onClick: PropTypes.func,
315322

@@ -372,12 +379,18 @@ export default class FileUploader extends Component {
372379
}
373380
};
374381

375-
handleClick = (evt, index) => {
376-
const filteredArray = this.state.filenames.filter(
377-
filename => filename !== this.nodes[index].innerText.trim()
378-
);
379-
this.setState({ filenames: filteredArray });
380-
this.props.onClick(evt);
382+
handleClick = (evt, { index, filenameStatus }) => {
383+
if (filenameStatus === 'edit') {
384+
evt.stopPropagation();
385+
const filteredArray = this.state.filenames.filter(
386+
filename => filename !== this.nodes[index].innerText.trim()
387+
);
388+
this.setState({ filenames: filteredArray });
389+
if (this.props.onDelete) {
390+
this.props.onDelete(evt);
391+
}
392+
this.props.onClick(evt);
393+
}
381394
};
382395

383396
clearFiles = () => {
@@ -398,6 +411,7 @@ export default class FileUploader extends Component {
398411
accept,
399412
name,
400413
size,
414+
onDelete, // eslint-disable-line no-unused-vars
401415
...other
402416
} = this.props;
403417

@@ -440,15 +454,13 @@ export default class FileUploader extends Component {
440454
iconDescription={iconDescription}
441455
status={filenameStatus}
442456
onKeyDown={evt => {
443-
if (evt.which === 13 || evt.which === 32) {
444-
this.handleClick(evt, index);
445-
}
446-
}}
447-
onClick={evt => {
448-
if (filenameStatus === 'edit') {
449-
this.handleClick(evt, index);
457+
if (matches(evt, [keys.Enter, keys.Space])) {
458+
this.handleClick(evt, { index, filenameStatus });
450459
}
451460
}}
461+
onClick={evt =>
462+
this.handleClick(evt, { index, filenameStatus })
463+
}
452464
/>
453465
</span>
454466
</span>

0 commit comments

Comments
 (0)