Skip to content

Commit

Permalink
* file-selector: add onChange callback to fileSelector.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Mar 15, 2024
1 parent 6ab83e3 commit edc73ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/file-selector/src/components/file-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class FileSelector<P extends FileSelectorProps = FileSelectorProps, S ext
}

this._data.items.add(file);
this._syncFiles();
this._syncFiles(true);

await this.changeState((prevState) => ({files: [...prevState.files, fileInfo]} as S));

Expand Down Expand Up @@ -289,7 +289,7 @@ export class FileSelector<P extends FileSelectorProps = FileSelectorProps, S ext
this._data.items.remove(dataIndex);
}
this._data.items.add(newFile);
this._syncFiles();
this._syncFiles(true);
fileInfo.file = newFile;
}
fileInfo.name = newName;
Expand Down Expand Up @@ -357,11 +357,17 @@ export class FileSelector<P extends FileSelectorProps = FileSelectorProps, S ext
if (index >= 0) {
this.state.files.splice(index, 1);
this.setState({files: this.state.files});
this._syncFiles(true);
}
}

protected _syncFiles() {
this._file.current!.files = this._data.files;
protected _syncFiles(triggerChange = false) {
const files = this._data.files;
const element = this._file.current!;
element!.files = files;
if (triggerChange) {
$(element!).trigger('change', {files});
}
}

protected _showAlert(tip: string | ModalAlertOptions, formatData?: Record<string, unknown>) {
Expand Down Expand Up @@ -458,8 +464,8 @@ export class FileSelector<P extends FileSelectorProps = FileSelectorProps, S ext
}

protected _renderForForm(props: RenderableProps<P>) {
const {name, accept} = props;
return <input key="form" ref={this._file} type="file" name={name} multiple={this.multiple} accept={accept} style="display:none" />;
const {name, accept, onChange} = props;
return <input key="form" ref={this._file} type="file" name={name} multiple={this.multiple} accept={accept} style="display:none" onChange={onChange} />;
}

protected _getIcon(file: FileInfo): IconType | undefined {
Expand Down
1 change: 1 addition & 0 deletions lib/file-selector/src/types/file-selector-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface FileSelectorProps extends HElementProps {
onSelect?: (files: File[] | FileList) => void | false;
onAdd?: (file: FileInfo) => void | false;
onRemove?: (file: FileInfo) => void | Promise<void | false>;
onChange?: (event: Event) => void;
onRename?: (newName: string, oldName: string, file: FileInfo) => void | false | Promise<void | false>;
onDuplicated?: (name: string, currentFile: FileInfo, existFile: FileInfo) => void | true;
onExceededSize?: (limit: number, file: FileInfo) => void | true;
Expand Down

0 comments on commit edc73ca

Please sign in to comment.