Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TextArea support to DelayedTextInput #952

Closed
howudodat opened this issue Aug 6, 2024 · 1 comment
Closed

add TextArea support to DelayedTextInput #952

howudodat opened this issue Aug 6, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@howudodat
Copy link

/*
 * Copyright © 2019 Dominokit
 *
 * 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 org.dominokit.domino.ui.utils;

import static java.util.Objects.isNull;

import org.dominokit.domino.ui.events.EventType;
import org.gwtproject.timer.client.Timer;

import elemental2.dom.HTMLTextAreaElement;
import jsinterop.base.Js;

/**
 * The {@code DelayedTextInput} class provides a utility for capturing text input events in an HTML
 * TextArea element and triggering actions with a specified delay.
 *
 * <p>Example Usage:
 *
 * <pre>
 * HTMLTextAreaElement inputElement = // Get the input element from the DOM
 * DelayedTextInput delayedTextInput = DelayedTextInput.create(inputElement, 1000, () -> {
 *   // Perform an action after a delay of 1000 milliseconds (1 second) when text input changes.
 *   // This action is specified using a lambda expression.
 *   // You can replace this with your custom action.
 * });
 * </pre>
 */
public class DelayedTextAreaInput {

  private int delay;
  private final HTMLTextAreaElement inputElement;
  private Timer autoActionTimer;
  private DelayedAction delayedAction = () -> {};
  private DelayedAction onEnterAction = () -> delayedAction.doAction();

  /**
   * Creates a {@code DelayedTextInput} instance for the given HTML input element with a specified
   * delay and an action to execute on text input changes.
   *
   * @param inputElement The HTML input element to monitor for text input changes.
   * @param delay The delay in milliseconds before triggering the action.
   * @param delayedAction The action to execute when text input changes after the specified delay.
   * @return A {@code DelayedTextInput} instance.
   */
  public static DelayedTextAreaInput create(
      HTMLTextAreaElement inputElement, int delay, DelayedAction delayedAction) {
    return new DelayedTextAreaInput(inputElement, delay, delayedAction);
  }

  /**
   * Creates a {@code DelayedTextInput} instance for the given HTML input element with a default
   * delay and an action to execute on text input changes.
   *
   * @param inputElement The HTML input element to monitor for text input changes.
   * @param delayedAction The action to execute when text input changes after the specified delay.
   * @return A {@code DelayedTextInput} instance.
   */
  public static DelayedTextAreaInput create(
      HTMLTextAreaElement inputElement, DelayedAction delayedAction) {
    return new DelayedTextAreaInput(
        inputElement,
        DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay(),
        delayedAction);
  }

  /**
   * Creates a {@code DelayedTextInput} instance for the given HTML input element with a specified
   * delay.
   *
   * @param inputElement The HTML input element to monitor for text input changes.
   * @param delay The delay in milliseconds before triggering the action.
   * @return A {@code DelayedTextInput} instance.
   */
  public static DelayedTextAreaInput create(HTMLTextAreaElement inputElement, int delay) {
    return new DelayedTextAreaInput(inputElement, delay);
  }

  /**
   * Creates a {@code DelayedTextInput} instance for the given HTML input element with a default
   * delay.
   *
   * @param inputElement The HTML input element to monitor for text input changes.
   * @return A {@code DelayedTextInput} instance.
   */
  public static DelayedTextAreaInput create(HTMLTextAreaElement inputElement) {
    return new DelayedTextAreaInput(
        inputElement, DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay());
  }

  /**
   * Creates a {@code DelayedTextInput} instance for the given DominoElement with a specified delay.
   *
   * @param inputElement The DominoElement wrapping the HTML input element to monitor for text input
   *     changes.
   * @param delay The delay in milliseconds before triggering the action.
   * @return A {@code DelayedTextInput} instance.
   */
  public static DelayedTextAreaInput create(DominoElement<HTMLTextAreaElement> inputElement, int delay) {
    return create(inputElement.element(), delay);
  }

  /**
   * Creates a {@code DelayedTextInput} instance for the given DominoElement with a default delay.
   *
   * @param inputElement The DominoElement wrapping the HTML input element to monitor for text input
   *     changes.
   * @return A {@code DelayedTextInput} instance.
   */
  public static DelayedTextAreaInput create(DominoElement<HTMLTextAreaElement> inputElement) {
    return create(
        inputElement.element(),
        DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay());
  }

  /**
   * Constructs a {@code DelayedTextInput} instance for the given HTML input element with a
   * specified delay.
   *
   * @param inputElement The HTML input element to monitor for text input changes.
   * @param delay The delay in milliseconds before triggering the action.
   */
  public DelayedTextAreaInput(HTMLTextAreaElement inputElement, int delay) {
    this.inputElement = inputElement;
    this.delay = delay;
    prepare();
  }

  /**
   * Constructs a {@code DelayedTextInput} instance for the given HTML input element with a
   * specified delay and an action to execute on text input changes.
   *
   * @param inputElement The HTML input element to monitor for text input changes.
   * @param delay The delay in milliseconds before triggering the action.
   * @param delayedAction The action to execute when text input changes after the specified delay.
   */
  public DelayedTextAreaInput(HTMLTextAreaElement inputElement, int delay, DelayedAction delayedAction) {
    this.inputElement = inputElement;
    this.delay = delay;
    this.delayedAction = delayedAction;

    prepare();
  }

  protected void prepare() {
    autoActionTimer =
        new Timer() {
          @Override
          public void run() {
            DelayedTextAreaInput.this.delayedAction.doAction();
          }
        };

    inputElement.addEventListener(
        "input",
        evt -> {
          autoActionTimer.cancel();
          autoActionTimer.schedule(this.delay);
        });

    inputElement.addEventListener(
        EventType.keypress.getName(),
        evt -> {
          if (ElementUtil.isEnterKey(Js.uncheckedCast(evt))) {
            autoActionTimer.cancel();
            DelayedTextAreaInput.this.onEnterAction.doAction();
          }
        });
  }

  /**
   * Sets the action to be executed after the specified delay when text input changes.
   *
   * @param delayedAction The action to execute when text input changes after the specified delay.
   * @return This {@code DelayedTextInput} instance.
   */
  public DelayedTextAreaInput setDelayedAction(DelayedAction delayedAction) {
    this.delayedAction = delayedAction;
    return this;
  }

  /**
   * Gets the action to be executed when the Enter key is pressed after the specified delay.
   *
   * @return The action to execute when Enter key is pressed.
   */
  public DelayedAction getOnEnterAction() {
    return onEnterAction;
  }

  /**
   * Sets the action to be executed when the Enter key is pressed after the specified delay.
   *
   * @param onEnterAction The action to execute when Enter key is pressed.
   * @return This {@code DelayedTextInput} instance.
   */
  public DelayedTextAreaInput setOnEnterAction(DelayedAction onEnterAction) {
    if (isNull(onEnterAction)) {
      this.onEnterAction = delayedAction;
    } else {
      this.onEnterAction = onEnterAction;
    }

    return this;
  }

  /**
   * Gets the current delay in milliseconds before triggering the action.
   *
   * @return The delay in milliseconds.
   */
  public int getDelay() {
    return delay;
  }

  /**
   * Sets the delay in milliseconds before triggering the action.
   *
   * @param delay The delay in milliseconds.
   */
  public void setDelay(int delay) {
    this.delay = delay;
  }

  /** A functional interface representing an action to be executed after a delay. */
  @FunctionalInterface
  public interface DelayedAction {
    /** Performs the action that should be executed after the delay. */
    void doAction();
  }
}

@vegegoku vegegoku self-assigned this Aug 6, 2024
@vegegoku vegegoku added the enhancement New feature or request label Aug 6, 2024
@vegegoku vegegoku added this to the 2.0.3 milestone Aug 6, 2024
@vegegoku
Copy link
Member

vegegoku commented Aug 6, 2024

This can be added but there should be no need to duplicate the whole class, I will see what can be done here.

@vegegoku vegegoku changed the title add DelayedTextAreaInput add TextArea support to DelayedTextInput Aug 8, 2024
@vegegoku vegegoku closed this as completed Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

No branches or pull requests

2 participants