Skip to content

Commit 4ef8c06

Browse files
feat: add HasLabel support with label constructors
1 parent c0b5dc5 commit 4ef8c06

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/main/frontend/fc-toggle-button.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LitElement, html, css } from 'lit';
77
class FCToggleButton extends LitElement {
88
static properties = {
99
checked: { type: Boolean, reflect: true },
10+
label: { type: String },
1011
leftLabel: { type: String },
1112
rightLabel: { type: String },
1213
disabled: { type: Boolean, reflect: true },
@@ -17,15 +18,23 @@ class FCToggleButton extends LitElement {
1718
static styles = css`
1819
:host {
1920
display: inline-flex;
20-
align-items: center;
21-
gap: var(--lumo-space-s, 8px);
21+
flex-direction: column;
22+
align-items: flex-start;
23+
gap: var(--lumo-space-xs, 4px);
2224
font-family: var(--lumo-font-family);
2325
color: var(--lumo-body-text-color);
2426
cursor: pointer;
2527
user-select: none;
2628
transition: opacity 0.2s;
2729
}
2830
31+
.field-label {
32+
font-size: var(--lumo-font-size-s, 14px);
33+
font-weight: 500;
34+
color: var(--lumo-secondary-text-color);
35+
line-height: 1;
36+
}
37+
2938
:host([disabled]) {
3039
opacity: 0.5;
3140
cursor: not-allowed;
@@ -239,6 +248,7 @@ class FCToggleButton extends LitElement {
239248
constructor() {
240249
super();
241250
this.checked = false;
251+
this.label = '';
242252
this.leftLabel = '';
243253
this.rightLabel = '';
244254
this.disabled = false;
@@ -259,6 +269,7 @@ class FCToggleButton extends LitElement {
259269

260270
render() {
261271
return html`
272+
${this.label ? html`<span class="field-label">${this.label}</span>` : ''}
262273
<div class="container">
263274
<slot name="left"></slot>
264275
${this.leftLabel ? html`<span class="label ${this.highlightLabel && !this.checked ? 'active' : this.highlightLabel ? 'inactive' : ''}">${this.leftLabel}</span>` : ''}

src/main/java/com/flowingcode/vaadin/addons/fctogglebutton/FCToggleButton.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.vaadin.flow.component.Component;
2424
import com.vaadin.flow.component.HasAriaLabel;
2525
import com.vaadin.flow.component.HasComponents;
26+
import com.vaadin.flow.component.HasLabel;
2627
import com.vaadin.flow.component.HasSize;
2728
import com.vaadin.flow.component.ItemLabelGenerator;
2829
import com.vaadin.flow.component.Tag;
@@ -46,6 +47,7 @@
4647
public class FCToggleButton extends AbstractSinglePropertyField<FCToggleButton, Boolean>
4748
implements HasSize,
4849
HasComponents,
50+
HasLabel,
4951
HasAriaLabel,
5052
HasTooltip,
5153
HasThemeVariant<FCToggleButtonVariant> {
@@ -61,6 +63,7 @@ public FCToggleButton() {
6163
* Creates a new toggle button with the given initial value.
6264
*
6365
* @param initialValue the initial checked state
66+
* @since 1.0.0
6467
*/
6568
public FCToggleButton(boolean initialValue) {
6669
super("checked", initialValue, false);
@@ -70,6 +73,29 @@ public FCToggleButton(boolean initialValue) {
7073
setSynchronizedEvent("checked-changed");
7174
}
7275

76+
/**
77+
* Creates a new toggle button with the given label and an initial value of {@code false}.
78+
*
79+
* @param label the label text shown above the toggle
80+
* @since 1.0.0
81+
*/
82+
public FCToggleButton(String label) {
83+
this(false);
84+
setLabel(label);
85+
}
86+
87+
/**
88+
* Creates a new toggle button with the given label and initial value.
89+
*
90+
* @param label the label text shown above the toggle
91+
* @param initialValue the initial checked state
92+
* @since 1.0.0
93+
*/
94+
public FCToggleButton(String label, boolean initialValue) {
95+
this(initialValue);
96+
setLabel(label);
97+
}
98+
7399
@Override
74100
public void setReadOnly(boolean readOnly) {
75101
getElement().setProperty("readonly", readOnly);

0 commit comments

Comments
 (0)