Skip to content

Commit

Permalink
fix #517 allow the slider to show the current value
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Nov 10, 2024
1 parent c496ae8 commit cbe309b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public interface SlidersConfig extends ComponentConfig {
default CssClass getDefaultSliderThumbStyle() {
return SliderStyles.dui_slider_thumb_rounded;
}

/**
* @return a boolean to set if the slider thumb disappears or not after finishes dragging the
* slider
*/
default boolean autoHideThumb() {
return true;
}
}
49 changes: 39 additions & 10 deletions domino-ui/src/main/java/org/dominokit/domino/ui/sliders/Slider.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import elemental2.dom.HTMLDivElement;
import java.util.HashSet;
import java.util.Set;
import org.dominokit.domino.ui.config.HasComponentConfig;
import org.dominokit.domino.ui.config.SlidersConfig;
import org.dominokit.domino.ui.elements.DivElement;
import org.dominokit.domino.ui.elements.InputElement;
import org.dominokit.domino.ui.elements.SpanElement;
Expand Down Expand Up @@ -58,7 +60,7 @@
* @see BaseDominoElement
*/
public class Slider extends BaseDominoElement<HTMLDivElement, Slider>
implements HasChangeListeners<Slider, Double>, SliderStyles {
implements HasChangeListeners<Slider, Double>, SliderStyles, HasComponentConfig<SlidersConfig> {
private final DivElement root;
private final InputElement input;
private final SpanElement thumb;
Expand All @@ -70,6 +72,7 @@ public class Slider extends BaseDominoElement<HTMLDivElement, Slider>
private boolean changeListenersPaused;
private SwapCssClass dui_thumb_style =
SwapCssClass.of(DominoUIConfig.CONFIG.getUIConfig().getDefaultSliderThumbStyle());
private boolean autoHideThumb = DominoUIConfig.CONFIG.getUIConfig().autoHideThumb();

/**
* Creates a slider with a specified maximum value.
Expand Down Expand Up @@ -128,13 +131,7 @@ public Slider(double max, double min, double value) {
setValue(value);
EventListener downEvent =
mouseDownEvent -> {
this.oldValue = getValue();
input.addCss(dui_active);
this.mouseDown = true;
if (withThumb) {
showThumb();
evaluateThumbPosition();
}
startSliding();
};
EventListener moveMouseListener =
mouseMoveEvent -> {
Expand All @@ -145,13 +142,20 @@ public Slider(double max, double min, double value) {
}
}
};
EventListener leaveMouseListener = evt -> hideThumb();
EventListener leaveMouseListener =
evt -> {
if (isAutoHideThumb()) {
hideThumb();
}
};

EventListener upEvent =
mouseUpEvent -> {
mouseDown = false;
input.removeCss(dui_active);
hideThumb();
if (isAutoHideThumb()) {
hideThumb();
}
};
input.addEventListener(change.getName(), evt -> triggerChangeListeners(oldValue, getValue()));

Expand All @@ -170,6 +174,22 @@ public Slider(double max, double min, double value) {
input.addEventListener(blur.getName(), leaveMouseListener);

init(this);
onAttached(
mutationRecord -> {
if (!autoHideThumb) {
startSliding();
}
});
}

private void startSliding() {
this.oldValue = getValue();
input.addCss(dui_active);
this.mouseDown = true;
if (withThumb) {
showThumb();
evaluateThumbPosition();
}
}

/**
Expand Down Expand Up @@ -408,6 +428,15 @@ public Slider setShowThumb(boolean withThumb) {
return this;
}

public Slider setAutoHideThumb(boolean autoHideThumb) {
this.autoHideThumb = autoHideThumb;
return this;
}

public boolean isAutoHideThumb() {
return this.autoHideThumb;
}

/**
* Returns the root HTML div element of the slider.
*
Expand Down

0 comments on commit cbe309b

Please sign in to comment.