Skip to content

Commit 7a97cd8

Browse files
authored
feat: implement HasEnabled for Upload (#6787)
1 parent 42721c6 commit 7a97cd8

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/Upload.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.vaadin.flow.component.AttachEvent;
2828
import com.vaadin.flow.component.Component;
2929
import com.vaadin.flow.component.ComponentEventListener;
30+
import com.vaadin.flow.component.HasEnabled;
3031
import com.vaadin.flow.component.HasSize;
3132
import com.vaadin.flow.component.HasStyle;
3233
import com.vaadin.flow.component.Tag;
@@ -63,7 +64,7 @@
6364
@JsModule("@vaadin/polymer-legacy-adapter/style-modules.js")
6465
@NpmPackage(value = "@vaadin/upload", version = "24.6.0-alpha8")
6566
@JsModule("@vaadin/upload/src/vaadin-upload.js")
66-
public class Upload extends Component implements HasSize, HasStyle {
67+
public class Upload extends Component implements HasEnabled, HasSize, HasStyle {
6768

6869
/**
6970
* Server-side component for the default {@code <vaadin-upload>} icon.
@@ -193,7 +194,7 @@ public Registration addAllFinishedListener(
193194
return addListener(AllFinishedEvent.class, listener);
194195
}
195196

196-
private StreamVariable getStreamVariable() {
197+
StreamVariable getStreamVariable() {
197198
if (streamVariable == null) {
198199
streamVariable = new DefaultStreamVariable(this);
199200
}
@@ -425,6 +426,10 @@ public Component getDropLabelIcon() {
425426
* accepted on same component.
426427
*/
427428
private void startUpload() {
429+
if (!isEnabled()) {
430+
throw new IllegalStateException(
431+
"Cannot start upload because the Upload component is disabled");
432+
}
428433
if (getMaxFiles() != 0 && getMaxFiles() <= activeUploads) {
429434
throw new IllegalStateException(
430435
"Maximum supported amount of uploads already started");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2000-2024 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.upload;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
public class DisabledUploadTest {
22+
@Test
23+
public void preventsStartingUploads() {
24+
Upload upload = new Upload();
25+
upload.setEnabled(false);
26+
27+
IllegalStateException exception = Assert
28+
.assertThrows(IllegalStateException.class, () -> {
29+
upload.getStreamVariable().streamingStarted(null);
30+
});
31+
Assert.assertTrue(exception.getMessage().contains(
32+
"Cannot start upload because the Upload component is disabled"));
33+
}
34+
}

vaadin-upload-flow-parent/vaadin-upload-flow/src/test/java/com/vaadin/flow/component/upload/tests/UploadTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.Assert;
1919
import org.junit.Test;
2020

21+
import com.vaadin.flow.component.HasEnabled;
2122
import com.vaadin.flow.component.upload.Upload;
2223
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
2324
import com.vaadin.flow.component.upload.receivers.MultiFileMemoryBuffer;
@@ -30,6 +31,11 @@ public void uploadNewUpload() {
3031
Upload upload = new Upload();
3132
}
3233

34+
@Test
35+
public void implementsHasEnabled() {
36+
Assert.assertTrue(HasEnabled.class.isAssignableFrom(Upload.class));
37+
}
38+
3339
@Test
3440
public void switchBetweenSingleAndMultiFileReceiver_assertMaxFilesProperty() {
3541
Upload upload = new Upload();

0 commit comments

Comments
 (0)