Skip to content

Commit

Permalink
✨ Added a way to provide a pre-filled value when creating a text input
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Dec 28, 2024
1 parent 2aa2f2f commit b053982
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,40 @@ public class ExprNewInput extends SimpleExpression<TextInput.Builder> {
ExprNewInput.class,
TextInput.Builder.class,
ExpressionType.COMBINED,
"[a] [new] text[( |-)]input [with] [the] [id] %string% (named|with name) %string%",
"[a] [new] short text[( |-)]input [with] [the] [id] %string% (named|with name) %string%"
"[a] [new] [(:required)] text[( |-)]input [with] [the] [id] %string% (named|with name) %string% [with [the] value %-string%]",
"[a] [new] [(:required)] short text[( |-)]input [with] [the] [id] %string% (named|with name) %string% [with [the] value %-string%]"
);
}

private boolean required;
private Expression<String> exprId;
private Expression<String> exprName;
private TextInputStyle style;
private Expression<String> exprValue;

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
required = parseResult.hasTag("required");
exprId = (Expression<String>) exprs[0];
exprName = (Expression<String>) exprs[1];
style = matchedPattern == 0 ? TextInputStyle.PARAGRAPH : TextInputStyle.SHORT;
exprValue = (Expression<String>) exprs[2];
return true;
}

@Override
protected TextInput.Builder @NotNull [] get(@NotNull Event e) {
final String id = EasyElement.parseSingle(exprId, e, null);
final String name = EasyElement.parseSingle(exprName, e, null);
final @Nullable String value = EasyElement.parseSingle(exprValue, e, null);
if (EasyElement.anyNull(this, id, name))
return new TextInput.Builder[0];
return new TextInput.Builder[] {TextInput.create(id, name, style)};
final var input = TextInput.create(id, name, style)
.setRequired(required);
if (value != null)
input.setValue(value);

return new TextInput.Builder[] {input};
}

@Override
Expand All @@ -58,7 +68,10 @@ public boolean isSingle() {

@Override
public @NotNull String toString(@Nullable Event e, boolean debug) {
return "new modal with id " + exprId.toString(e, debug) + " named " + exprName.toString(e, debug);
return "new " + (required ? "required " : "") + style.name().toLowerCase()
+ " text input with id " + exprId.toString(e, debug)
+ " named " + exprName.toString(e, debug)
+ (exprValue == null ? "" : " with value " + exprValue.toString(e, debug));
}

}

0 comments on commit b053982

Please sign in to comment.