You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2022/11/30 15:58:19 UTC

[myfaces-tobago] branch main updated: T5 autocomplete (#3475)

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/main by this push:
     new c0a7a05114 T5 autocomplete (#3475)
c0a7a05114 is described below

commit c0a7a05114b6bc7fcb66142c04f8d4d3b65dc244
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Wed Nov 30 16:58:13 2022 +0100

    T5 autocomplete (#3475)
    
    * feat: autocomplete
    
    issue: TOBAGO-2175
    issue: TOBAGO-2176
---
 .../SupportsAutocomplete.java}                     | 33 +++++++++++++++-------
 .../tobago/internal/component/AbstractUIIn.java    |  4 ++-
 .../internal/component/AbstractUITextarea.java     |  3 +-
 .../internal/renderkit/renderer/InRenderer.java    |  1 +
 .../renderkit/renderer/TextareaRenderer.java       |  1 +
 .../taglib/component/TextareaTagDeclaration.java   |  4 ++-
 .../taglib/declaration/HasAutocomplete.java        |  2 +-
 .../renderkit/renderer/InRendererUnitTest.java     | 11 ++++++++
 .../renderer/TextareaRendererUnitTest.java         | 10 +++++++
 .../resources/renderer/in/autocomplete-on.html     | 20 +++++++++++++
 .../renderer/textarea/autocomplete-impp.html       | 20 +++++++++++++
 .../main/webapp/content/010-input/10-in/In.xhtml   | 12 ++++++++
 .../content/010-input/30-textarea/Textarea.xhtml   | 12 ++++++++
 13 files changed, 119 insertions(+), 14 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITextarea.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/component/SupportsAutocomplete.java
similarity index 54%
copy from tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITextarea.java
copy to tobago-core/src/main/java/org/apache/myfaces/tobago/component/SupportsAutocomplete.java
index 47298d39e4..bbfce0b73e 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITextarea.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/component/SupportsAutocomplete.java
@@ -17,18 +17,31 @@
  * under the License.
  */
 
-package org.apache.myfaces.tobago.internal.component;
+package org.apache.myfaces.tobago.component;
 
-import org.apache.myfaces.tobago.sanitizer.SanitizeMode;
+import java.util.Objects;
 
-/**
- * {@link org.apache.myfaces.tobago.internal.taglib.component.TextareaTagDeclaration}
- */
-public abstract class AbstractUITextarea extends AbstractUIInput {
-
-  public abstract String getPlaceholder();
+public interface SupportsAutocomplete {
 
-  public abstract SanitizeMode getSanitize();
+  default String getAutocompleteString() {
+    final Object object = getAutocomplete();
+    if (object == null) {
+      return null;
+    } else if (object instanceof Boolean) {
+      return ((Boolean) object) ? "on" : "off";
+    } else if (object instanceof String) {
+      final String string = (String) object;
+      if (string.equals("true")) {
+        return "on";
+      } else if (string.equals("false")) {
+        return "off";
+      } else {
+        return string;
+      }
+    } else {
+      return Objects.toString(object);
+    }
+  }
 
-  public abstract Integer getRows();
+  Object getAutocomplete();
 }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIIn.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIIn.java
index 00bb977792..9298fdada4 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIIn.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIIn.java
@@ -19,10 +19,12 @@
 
 package org.apache.myfaces.tobago.internal.component;
 
+import org.apache.myfaces.tobago.component.SupportsAutocomplete;
+
 /**
  * {@link org.apache.myfaces.tobago.internal.taglib.component.InTagDeclaration}
  */
-public abstract class AbstractUIIn extends AbstractUIInput {
+public abstract class AbstractUIIn extends AbstractUIInput implements SupportsAutocomplete {
 
   public abstract String getPlaceholder();
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITextarea.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITextarea.java
index 47298d39e4..93061a9804 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITextarea.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUITextarea.java
@@ -19,12 +19,13 @@
 
 package org.apache.myfaces.tobago.internal.component;
 
+import org.apache.myfaces.tobago.component.SupportsAutocomplete;
 import org.apache.myfaces.tobago.sanitizer.SanitizeMode;
 
 /**
  * {@link org.apache.myfaces.tobago.internal.taglib.component.TextareaTagDeclaration}
  */
-public abstract class AbstractUITextarea extends AbstractUIInput {
+public abstract class AbstractUITextarea extends AbstractUIInput implements SupportsAutocomplete {
 
   public abstract String getPlaceholder();
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRenderer.java
index af9c112dc8..38214b2d50 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRenderer.java
@@ -141,6 +141,7 @@ public class InRenderer<T extends AbstractUIIn> extends MessageLayoutRendererBas
     if (!disabled && !readonly) {
       writer.writeAttribute(HtmlAttributes.PLACEHOLDER, component.getPlaceholder(), true);
     }
+    writer.writeAttribute(HtmlAttributes.AUTOCOMPLETE, component.getAutocompleteString(), true);
 
     writer.writeClassAttribute(
         markup != null && markup.contains(Markup.NUMBER) ? TobagoClass.NUMBER : null,
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRenderer.java
index 33ccc0c82a..1658633cde 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRenderer.java
@@ -96,6 +96,7 @@ public class TextareaRenderer<T extends AbstractUITextarea> extends MessageLayou
     writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
     writer.writeAttribute(HtmlAttributes.REQUIRED, component.isRequired());
     writer.writeAttribute(HtmlAttributes.TABINDEX, component.getTabIndex());
+    writer.writeAttribute(HtmlAttributes.AUTOCOMPLETE, component.getAutocompleteString(), true);
 
     if (component.getAccessKey() != null) {
       writer.writeAttribute(HtmlAttributes.ACCESSKEY, Character.toString(component.getAccessKey()), false);
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/TextareaTagDeclaration.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/TextareaTagDeclaration.java
index 9f590bdeb7..ec8cb3ef4f 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/TextareaTagDeclaration.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/TextareaTagDeclaration.java
@@ -28,6 +28,7 @@ import org.apache.myfaces.tobago.apt.annotation.UIComponentTagAttribute;
 import org.apache.myfaces.tobago.component.ClientBehaviors;
 import org.apache.myfaces.tobago.component.RendererTypes;
 import org.apache.myfaces.tobago.internal.taglib.declaration.HasAccessKey;
+import org.apache.myfaces.tobago.internal.taglib.declaration.HasAutocomplete;
 import org.apache.myfaces.tobago.internal.taglib.declaration.HasConverter;
 import org.apache.myfaces.tobago.internal.taglib.declaration.HasConverterMessage;
 import org.apache.myfaces.tobago.internal.taglib.declaration.HasHelp;
@@ -92,7 +93,8 @@ import jakarta.faces.component.UIInput;
 public interface TextareaTagDeclaration
     extends HasIdBindingAndRendered, HasConverter, IsReadonly, IsDisabled, IsRequired, HasLabel, HasLabelLayout, HasTip,
     HasHelp, HasAccessKey, HasValidator, HasValue, HasValueChangeListener, HasTabIndex, IsFocus, IsVisual,
-    HasValidatorMessage, HasConverterMessage, HasRequiredMessage, HasSanitize, HasPlaceholder, HasAutoSpacing {
+    HasValidatorMessage, HasConverterMessage, HasRequiredMessage, HasSanitize, HasPlaceholder, HasAutoSpacing,
+    HasAutocomplete {
 
   /**
    * The row count for this component.
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/declaration/HasAutocomplete.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/declaration/HasAutocomplete.java
index 5dfb561053..30c0be3241 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/declaration/HasAutocomplete.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/declaration/HasAutocomplete.java
@@ -25,6 +25,6 @@ import org.apache.myfaces.tobago.apt.annotation.UIComponentTagAttribute;
 public interface HasAutocomplete {
 
   @TagAttribute
-  @UIComponentTagAttribute(type = "boolean", defaultValue = "true")
+  @UIComponentTagAttribute(type = {"java.lang.Boolean", "java.lang.String"})
   void setAutocomplete(String action);
 }
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRendererUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRendererUnitTest.java
index 1c8e7f44c3..d1d791b68b 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRendererUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/InRendererUnitTest.java
@@ -544,4 +544,15 @@ public class InRendererUnitTest extends RendererTestBase {
 
     Assertions.assertEquals(loadHtml("renderer/in/required-label-segmentRight.html"), formattedResult());
   }
+
+  @Test
+  public void autocompleteTrue() throws IOException {
+
+    final UIIn c = (UIIn) ComponentUtils.createComponent(
+        facesContext, Tags.in.componentType(), RendererTypes.In, "id");
+    c.setAutocomplete(true);
+    c.encodeAll(facesContext);
+
+    Assertions.assertEquals(loadHtml("renderer/in/autocomplete-on.html"), formattedResult());
+  }
 }
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRendererUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRendererUnitTest.java
index 2502e3fd99..191aceeea0 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRendererUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/TextareaRendererUnitTest.java
@@ -40,4 +40,14 @@ public class TextareaRendererUnitTest extends RendererTestBase {
     Assertions.assertEquals(loadHtml("renderer/textarea/with-label.html"), formattedResult());
   }
 
+  @Test
+  public void autocomplete() throws IOException {
+    final UITextarea c = (UITextarea) ComponentUtils.createComponent(
+        facesContext, Tags.textarea.componentType(), RendererTypes.Textarea, "id");
+    c.setAutocomplete("impp");
+    c.encodeAll(facesContext);
+
+    Assertions.assertEquals(loadHtml("renderer/textarea/autocomplete-impp.html"), formattedResult());
+  }
+
 }
diff --git a/tobago-core/src/test/resources/renderer/in/autocomplete-on.html b/tobago-core/src/test/resources/renderer/in/autocomplete-on.html
new file mode 100644
index 0000000000..076a9cd014
--- /dev/null
+++ b/tobago-core/src/test/resources/renderer/in/autocomplete-on.html
@@ -0,0 +1,20 @@
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+-->
+
+<tobago-in id='id' class='tobago-auto-spacing'>
+  <input type='text' name='id' id='id::field' autocomplete='on' class='form-control'>
+</tobago-in>
diff --git a/tobago-core/src/test/resources/renderer/textarea/autocomplete-impp.html b/tobago-core/src/test/resources/renderer/textarea/autocomplete-impp.html
new file mode 100644
index 0000000000..177e59a0fb
--- /dev/null
+++ b/tobago-core/src/test/resources/renderer/textarea/autocomplete-impp.html
@@ -0,0 +1,20 @@
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+-->
+
+<tobago-textarea id='id' class='tobago-auto-spacing'>
+  <textarea name='id' id='id::field' autocomplete='impp' class='form-control'></textarea>
+</tobago-textarea>
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/10-in/In.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/10-in/In.xhtml
index 832264f151..74608df3c2 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/10-in/In.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/10-in/In.xhtml
@@ -71,6 +71,18 @@
     <tc:in id="i7" label="Placeholder" placeholder="Short description of the field." />
   </tc:section>
 
+  <tc:section label="Autocomplete">
+    <p>The <code>autocomplete</code> attribute can be used for explaining a user the meaning of a field.</p>
+
+    <tc:in id="a1" label="on" autocomplete="true" />
+    <tc:in id="a2" label="off" autocomplete="false" />
+    <tc:in id="a3" label="name" autocomplete="name" />
+
+    The values are documented in
+    <tc:link label="MDN" image="bi-box-arrow-up-right"
+             link="https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete#values"/>.
+  </tc:section>
+
   <tc:section label="Ajax">
     <p>The output field in this example, displays the given value on the server.
       With <code class="language-markup">&lt;f:ajax render="outputAjax" listener="\#{inController.update}"/></code>,
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/30-textarea/Textarea.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/30-textarea/Textarea.xhtml
index 65551719ab..0c6fce9ed2 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/30-textarea/Textarea.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/010-input/30-textarea/Textarea.xhtml
@@ -56,6 +56,18 @@
     <tc:button label="Submit" action="#{textareaController.submit}"/>
   </tc:section>
 
+  <tc:section label="Autocomplete">
+    <p>The <code>autocomplete</code> attribute can be used for explaining a user the meaning of a field.</p>
+
+    <tc:textarea id="a1" label="on" autocomplete="true" />
+    <tc:textarea id="a2" label="off" autocomplete="false" />
+    <tc:textarea id="a3" label="name" autocomplete="name" />
+
+    The values are documented in
+    <tc:link label="MDN" image="bi-box-arrow-up-right"
+             link="https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete#values"/>.
+  </tc:section>
+
   <tc:section label="Ajax">
     <p>Ajax update by the change event:</p>
     <tc:textarea id="tajax" label="Text Area" value="#{textareaController.ajaxValue}">