-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deprecate Label and add NativeLabel as replacement (#16708)
Adds NativeLabel as direct replacement for the HTML-based Label component / element. Fixes #4384 Fixes #3532
- Loading branch information
Showing
38 changed files
with
354 additions
and
86 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
29 changes: 29 additions & 0 deletions
29
...-testbench/src/main/java/com/vaadin/flow/component/html/testbench/NativeLabelElement.java
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,29 @@ | ||
/* | ||
* Copyright 2000-2023 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.html.testbench; | ||
|
||
import com.vaadin.testbench.TestBenchElement; | ||
import com.vaadin.testbench.elementsbase.Element; | ||
|
||
/** | ||
* A TestBench element representing a <code><label></code> element. | ||
* | ||
* @since 24.1 | ||
*/ | ||
@Element("label") | ||
public class NativeLabelElement extends TestBenchElement { | ||
|
||
} |
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
120 changes: 120 additions & 0 deletions
120
flow-html-components/src/main/java/com/vaadin/flow/component/html/NativeLabel.java
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,120 @@ | ||
/* | ||
* Copyright 2000-2023 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.html; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.Html; | ||
import com.vaadin.flow.component.HtmlContainer; | ||
import com.vaadin.flow.component.PropertyDescriptor; | ||
import com.vaadin.flow.component.PropertyDescriptors; | ||
import com.vaadin.flow.component.Tag; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Component for a <code><label></code> element, which represents a | ||
* caption for an input field in a user interface. | ||
* <p> | ||
* Note that Label components are not meant for loose text in the page - they | ||
* should be coupled with another component by using the | ||
* {@link #setFor(Component)} or by adding them to it with the | ||
* {@link #add(Component...)} method. | ||
* <p> | ||
* Clicking on a label automatically transfers the focus to the associated | ||
* component. This is especially helpful when building forms with | ||
* {@link Input}s. | ||
* <p> | ||
* For adding texts to the page without linking them to other components, | ||
* consider using a {@link Span} or a {@link Div} instead. If the text should be | ||
* interpreted as HTML, use a {@link Html} (but remember to guard against | ||
* cross-site scripting attacks). | ||
* | ||
* @author Vaadin Ltd | ||
* @see <a href= | ||
* "https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label</a> | ||
* @since 24.1 | ||
*/ | ||
@Tag(Tag.LABEL) | ||
public class NativeLabel extends HtmlContainer { | ||
private static final PropertyDescriptor<String, Optional<String>> forDescriptor = PropertyDescriptors | ||
.optionalAttributeWithDefault("for", ""); | ||
|
||
/** | ||
* Creates a new empty label. | ||
*/ | ||
public NativeLabel() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Creates a new label with the given text content. | ||
* | ||
* @param text | ||
* the text content | ||
*/ | ||
public NativeLabel(String text) { | ||
this(); | ||
setText(text); | ||
} | ||
|
||
/** | ||
* Sets the component that this label describes. The component (or its id) | ||
* should be defined in case the described component is not an ancestor of | ||
* the label. | ||
* <p> | ||
* The provided component must have an id set. This component will still use | ||
* the old id if the id of the provided component is changed after this | ||
* method has been called. | ||
* | ||
* @param forComponent | ||
* the component that this label describes, not <code>null</code> | ||
* , must have an id | ||
* @throws IllegalArgumentException | ||
* if the provided component has no id | ||
*/ | ||
public void setFor(Component forComponent) { | ||
if (forComponent == null) { | ||
throw new IllegalArgumentException( | ||
"The provided component cannot be null"); | ||
} | ||
setFor(forComponent.getId() | ||
.orElseThrow(() -> new IllegalArgumentException( | ||
"The provided component must have an id"))); | ||
} | ||
|
||
/** | ||
* Sets the id of the component that this label describes. The id should be | ||
* defined in case the described component is not an ancestor of the label. | ||
* | ||
* @param forId | ||
* the id of the described component, or <code>null</code> if | ||
* there is no value | ||
*/ | ||
public void setFor(String forId) { | ||
set(forDescriptor, forId); | ||
} | ||
|
||
/** | ||
* Gets the id of the component that this label describes. | ||
* | ||
* @see #setFor(String) | ||
* | ||
* @return an optional id of the described component, or an empty optional | ||
* if the attribute has not been set | ||
*/ | ||
public Optional<String> getFor() { | ||
return get(forDescriptor); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
flow-html-components/src/test/java/com/vaadin/flow/component/html/NativeLabelTest.java
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,39 @@ | ||
/* | ||
* Copyright 2000-2023 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.html; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class NativeLabelTest extends ComponentTest { | ||
|
||
// Actual test methods in super class | ||
|
||
@Override | ||
protected void addProperties() { | ||
addOptionalStringProperty("for"); | ||
} | ||
|
||
@Test | ||
public void setForComponent() { | ||
NativeLabel otherComponent = new NativeLabel(); | ||
otherComponent.setId("otherC"); | ||
NativeLabel l = (NativeLabel) getComponent(); | ||
l.setFor(otherComponent); | ||
Assert.assertEquals(otherComponent.getId().get(), l.getFor().get()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.