-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement file registry on filepicker (#1246)
* feat: implement file registry on filepicker * fix: fix file registry key * fix: delete file when dynamic list item is deleted * test: fix filepicker tests * chore: revert unnecessary change * fix: properly handle dynamic lists deletion * fix: set input file value after expanding dynamic list items * test: fix tests * fix: trim all files from a dynamic list subtree * fix: refactor form field instance registry * chore: added `useBooleanExpressionEvaluation` hook * fix: improve file picker reliability Related to #1239 * chore: changed filepicker prefix to `files::` Related to #1239 * fix: ensure the hidden filepicker gets disabled as well Related to #1239 * chore: adjust filepicker tests * chore: cleanup formFieldInstanceRegistry tests Related to #1239 * refactor: Improve typing * refactor: Improve variable naming * fix: fix dynamic list expand button label * fix: prevent collapsed dynamic list items from unmounting * refactor: Remove unnecessary method, improve types and delete files on hide if events * test: remove duplicated test * fix: clear file references to deleted files Related to #1239 --------- Co-authored-by: Skaiir <[email protected]>
- Loading branch information
Showing
20 changed files
with
504 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { extractFileReferencesFromRemovedData } from '../util/extractFileReferencesFromRemovedData'; | ||
|
||
const fileRegistry = Symbol('fileRegistry'); | ||
const eventBusSymbol = Symbol('eventBus'); | ||
const formFieldRegistrySymbol = Symbol('formFieldRegistry'); | ||
const formFieldInstanceRegistrySymbol = Symbol('formFieldInstanceRegistry'); | ||
const EMPTY_ARRAY = []; | ||
|
||
class FileRegistry { | ||
/** | ||
* @param {import('../core/EventBus').EventBus} eventBus | ||
* @param {import('../core/FormFieldRegistry').FormFieldRegistry} formFieldRegistry | ||
* @param {import('../core/FormFieldInstanceRegistry').FormFieldInstanceRegistry} formFieldInstanceRegistry | ||
*/ | ||
constructor(eventBus, formFieldRegistry, formFieldInstanceRegistry) { | ||
/** @type {Map<string, File[]>} */ | ||
this[fileRegistry] = new Map(); | ||
/** @type {import('../core/EventBus').EventBus} */ | ||
this[eventBusSymbol] = eventBus; | ||
/** @type {import('../core/FormFieldRegistry').FormFieldRegistry} */ | ||
this[formFieldRegistrySymbol] = formFieldRegistry; | ||
/** @type {import('../core/FormFieldInstanceRegistry').FormFieldInstanceRegistry} */ | ||
this[formFieldInstanceRegistrySymbol] = formFieldInstanceRegistry; | ||
|
||
const removeFileHandler = ({ item }) => { | ||
const fileReferences = extractFileReferencesFromRemovedData(item); | ||
|
||
// Remove all file references from the registry | ||
fileReferences.forEach((fileReference) => { | ||
this.deleteFiles(fileReference); | ||
}); | ||
}; | ||
|
||
eventBus.on('form.clear', () => this.clear()); | ||
eventBus.on('conditionChecker.remove', removeFileHandler); | ||
eventBus.on('repeatRenderManager.remove', removeFileHandler); | ||
} | ||
|
||
/** | ||
* @param {string} id | ||
* @param {File[]} files | ||
*/ | ||
setFiles(id, files) { | ||
this[fileRegistry].set(id, files); | ||
} | ||
|
||
/** | ||
* @param {string} id | ||
* @returns {File[]} | ||
*/ | ||
getFiles(id) { | ||
return this[fileRegistry].get(id) || EMPTY_ARRAY; | ||
} | ||
|
||
/** | ||
* @returns {string[]} | ||
*/ | ||
getKeys() { | ||
return Array.from(this[fileRegistry].keys()); | ||
} | ||
|
||
/** | ||
* @param {string} id | ||
* @returns {boolean} | ||
*/ | ||
hasKey(id) { | ||
return this[fileRegistry].has(id); | ||
} | ||
|
||
/** | ||
* @param {string} id | ||
*/ | ||
deleteFiles(id) { | ||
this[fileRegistry].delete(id); | ||
} | ||
|
||
/** | ||
* @returns {Map<string, File[]>} | ||
*/ | ||
getAllFiles() { | ||
return new Map(this[fileRegistry]); | ||
} | ||
|
||
clear() { | ||
this[fileRegistry].clear(); | ||
} | ||
} | ||
|
||
FileRegistry.$inject = ['eventBus', 'formFieldRegistry', 'formFieldInstanceRegistry']; | ||
|
||
export { FileRegistry }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.