Skip to content

Commit cac182a

Browse files
fix(file): add error cleanup on file removal
ref: MANAGER-15030 Signed-off-by: Jacques Larique <jacques.larique.ext@ovhcloud.com>
1 parent 633022c commit cac182a

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

packages/apps/workshop/stories/design-system/components/file/file-webcomponent.stories.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ DragDropWithPreview.storyName = 'Drag & Drop with preview';
129129
export const Validation = forModule(moduleName).createElement(
130130
() => compileTemplate(
131131
`
132-
<form novalidate name="form">
132+
<form novalidate name="$ctrl.form">
133133
<oui-field label="Upload file" help-text="image/jpeg, image/png, image/gif (max size 150 KB)">
134134
<oui-file name="fileUpload"
135135
accept="image/jpeg,image/png,image/gif"
@@ -140,7 +140,7 @@ export const Validation = forModule(moduleName).createElement(
140140
</oui-file>
141141
</oui-field>
142142
143-
<oui-form-actions></oui-form-actions>
143+
<oui-form-actions disabled="$ctrl.form.fileUpload.$invalid"></oui-form-actions>
144144
</form>`,
145145
{
146146
$ctrl: {

packages/components/file/src/js/file.controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ export default class {
103103
removeFile(file) {
104104
if (angular.isArray(this.model)) {
105105
remove(this.model, (item) => item === file);
106+
if (file.errors && this.model.every((item) => !item.errors)
107+
&& this.form && this.form[this.name]) {
108+
this.form[this.name].$setValidity('maxsize', true);
109+
}
106110
this.onRemove({ modelValue: this.model });
107111
}
108112
}

packages/components/file/src/js/file.spec.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,18 @@ describe('ouiFile', () => {
196196
onSelectSpy = jasmine.createSpy('onSelectSpy');
197197
onRemoveSpy = jasmine.createSpy('onRemoveSpy');
198198
element = TestUtils.compileTemplate(`
199-
<oui-file
200-
model="$ctrl.model"
201-
on-select="$ctrl.onSelectSpy(modelValue)"
202-
on-remove="$ctrl.onRemoveSpy(modelValue)"
203-
>
204-
</oui-file>`, {
199+
<form name="form">
200+
<oui-file
201+
model="$ctrl.model"
202+
on-select="$ctrl.onSelectSpy(modelValue)"
203+
on-remove="$ctrl.onRemoveSpy(modelValue)"
204+
>
205+
</oui-file>
206+
</form>`, {
205207
onSelectSpy,
206208
onRemoveSpy,
207209
});
208-
controller = element.controller('ouiFile');
210+
controller = element.find('oui-file').controller('ouiFile');
209211

210212
$timeout.flush();
211213
});
@@ -261,6 +263,14 @@ describe('ouiFile', () => {
261263
expect(controller.model.length).toBe(0);
262264
expect(onRemoveSpy).toHaveBeenCalledWith(controller.model);
263265
});
266+
267+
it('should clear errors after file removal', () => {
268+
controller.maxsize = 100000;
269+
controller.addFiles(mockFiles);
270+
expect(controller.form[controller.name].$invalid).toBe(true);
271+
controller.removeFile(mockFile);
272+
expect(controller.form[controller.name].$invalid).toBe(false);
273+
});
264274
});
265275

266276
describe('Form controls', () => {

0 commit comments

Comments
 (0)