Skip to content

Commit

Permalink
feat: improve content state management
Browse files Browse the repository at this point in the history
Improve content state management by adding a state change listener and
storing the state as an internal variable
Fixes #4
  • Loading branch information
mlopezFC authored and javier-godoy committed Oct 19, 2024
1 parent adc788c commit 026f1db
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.component.react.ReactAdapterComponent;
import com.vaadin.flow.function.SerializableConsumer;

/**
* Base class for Markdown based Components.
Expand All @@ -47,13 +48,16 @@ public class BaseMarkdownComponent extends ReactAdapterComponent implements HasS
*/
public enum DataColorMode {DARK,LIGTH,AUTO};

private String content;

/**
* Base constructor that receives the content of the markdown component.
*
* @param content content to be used in the Markdown component
*/
public BaseMarkdownComponent(String content) {
setContent(content);
this.addContentChangeListener(c->this.content = c);
}

/**
Expand All @@ -62,7 +66,7 @@ public BaseMarkdownComponent(String content) {
* @return the content of the Markdown component
*/
public String getContent() {
return getState("content", String.class);
return content;
}

/**
Expand All @@ -71,9 +75,19 @@ public String getContent() {
* @param content retrieved from the state of the component
*/
public void setContent(String content) {
this.content = content;
setState("content", content);
}

/**
* Adds the specified listener for the content change event.
*
* @param listener the listener callback for receiving content changes
*/
public void addContentChangeListener(SerializableConsumer<String> listener) {
addStateChangeListener("content", String.class, listener);
}

/**
* Sets the color mode of the Markdown component.
*
Expand Down

0 comments on commit 026f1db

Please sign in to comment.