Skip to content

Commit e2cceae

Browse files
committed
feat: allow hiding the floating label
Close #89
1 parent 72f755b commit e2cceae

4 files changed

Lines changed: 92 additions & 2 deletions

File tree

src/main/java/com/flowingcode/vaadin/addons/chipfield/ChipField.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* ChipField Addon
44
* %%
5-
* Copyright (C) 2018 - 2022 Flowing Code
5+
* Copyright (C) 2018 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -275,6 +275,26 @@ public void setLabel(String label) {
275275
getElement().setProperty("label", label);
276276
}
277277

278+
/**
279+
* Sets whether the label is displayed as a static placeholder that is hidden when the input has a
280+
* value, instead of floating above the input. When the label does not float, no space is reserved
281+
* for it above the input.
282+
*
283+
* @param noLabelFloat {@code true} to prevent the label from floating, {@code false} otherwise.
284+
*/
285+
public void setNoLabelFloat(boolean noLabelFloat) {
286+
getElement().setProperty("noLabelFloat", noLabelFloat);
287+
}
288+
289+
/**
290+
* Returns whether the label is prevented from floating above the input.
291+
*
292+
* @return {@code true} if the label does not float, {@code false} otherwise.
293+
*/
294+
public boolean isNoLabelFloat() {
295+
return getElement().getProperty("noLabelFloat", false);
296+
}
297+
278298
public String[] getChipsAsStrings() {
279299
return super.getValue().stream().map(itemLabelGenerator).toArray(String[]::new);
280300
}

src/main/resources/META-INF/frontend/paper-chip-input-autocomplete.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ class PaperChipInputAutocomplete extends ThemableMixin(PolymerElement) {
8484
value: ''
8585
},
8686

87+
/**
88+
* If true, the label is displayed as a static placeholder that is hidden
89+
* when the input has a value, instead of floating above the input.
90+
*/
91+
noLabelFloat: {
92+
type: Boolean,
93+
value: false
94+
},
95+
8796
/**
8897
* If true, the paper-chip-input-autocomplete is focused.
8998
*/
@@ -274,6 +283,7 @@ class PaperChipInputAutocomplete extends ThemableMixin(PolymerElement) {
274283
disabled$="[[disabled]]"
275284
readonly$="[[readonly]]"
276285
label="[[label]]"
286+
no-label-float$="[[noLabelFloat]]"
277287
on-keyup="_findItems"
278288
value="{{_inputValue}}"
279289
autofocus="{{autofocus}}"

src/test/java/com/flowingcode/vaadin/addons/chipfield/ChipfieldDemoView.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* ChipField Addon
44
* %%
5-
* Copyright (C) 2018 - 2022 Flowing Code
5+
* Copyright (C) 2018 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -40,5 +40,6 @@ public ChipfieldDemoView() {
4040
addDemo(BinderDemo.class);
4141
addDemo(ReadonlyDemo.class);
4242
addDemo(StyledDemo.class);
43+
addDemo(NoLabelFloatDemo.class);
4344
}
4445
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*-
2+
* #%L
3+
* ChipField Addon
4+
* %%
5+
* Copyright (C) 2018 - 2026 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.chipfield;
21+
22+
import com.flowingcode.vaadin.addons.demo.DemoSource;
23+
import com.vaadin.flow.component.formlayout.FormLayout;
24+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
25+
import com.vaadin.flow.router.PageTitle;
26+
import com.vaadin.flow.router.Route;
27+
28+
@Route(value = "chipfield/no-label-float", layout = ChipfieldDemoView.class)
29+
@PageTitle("No Label Float")
30+
@DemoSource
31+
@SuppressWarnings("serial")
32+
public class NoLabelFloatDemo extends VerticalLayout {
33+
34+
public NoLabelFloatDemo() {
35+
// by default, space is reserved above the input for the floating label,
36+
// which misaligns the field from the label of the enclosing form item
37+
ChipField<String> floating = newChipField();
38+
39+
// when the label does not float, no space is reserved for it above the input
40+
ChipField<String> notFloating = newChipField();
41+
notFloating.setNoLabelFloat(true);
42+
43+
FormLayout formLayout = new FormLayout();
44+
formLayout.setResponsiveSteps(new FormLayout.ResponsiveStep("0", 1));
45+
formLayout.setWidth("500px");
46+
formLayout.addFormItem(floating, "Floating label");
47+
formLayout.addFormItem(notFloating, "No label float");
48+
49+
add(formLayout);
50+
}
51+
52+
private static ChipField<String> newChipField() {
53+
ChipField<String> chf = new ChipField<>("", "Mercury", "Venus", "Earth", "Mars", "Jupiter");
54+
chf.addSelectedItem("Earth");
55+
chf.setClosable(true);
56+
chf.setWidthFull();
57+
return chf;
58+
}
59+
}

0 commit comments

Comments
 (0)