001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.corelib.components;
014
015import org.apache.tapestry5.BindingConstants;
016import org.apache.tapestry5.MarkupWriter;
017import org.apache.tapestry5.annotations.Parameter;
018import org.apache.tapestry5.corelib.base.AbstractTextField;
019
020/**
021 * TextField component corresponds to {@code <input>} element. The value parameter will be edited (read when the containing
022 * {@link Form} is rendered, and updated when the form is submitted). TextField
023 * is generally used with string values, but other values are acceptable, as long as they can be freely converted back
024 * and forth to strings.
025 *
026 * Includes the <code>size</code> attribute, if a {@link org.apache.tapestry5.beaneditor.Width} annotation is present on
027 * the property bound to the value parameter.
028 *
029 * @tapestrydoc
030 */
031public class TextField extends AbstractTextField
032{
033    /**
034     * Sets the type attribute of the {@code <input>} element. The default is "text", but this can be overriden
035     * when using <a href="http://www.w3.org/TR/html5/the-input-element.html">HTML5</a> types such as "number".
036     */
037    @Parameter(allowNull = false, value = "text", defaultPrefix = BindingConstants.LITERAL)
038    private String type;
039
040    @Override
041    protected void writeFieldTag(MarkupWriter writer, String value)
042    {
043        writer.element("input",
044
045                       "type", type,
046
047                       "name", getControlName(),
048
049                       "class", cssClass,
050
051                       "id", getClientId(),
052
053                       "value", value,
054
055                       "size", getWidth());
056    }
057
058    final void afterRender(MarkupWriter writer)
059    {
060        writer.end(); // input
061    }
062
063}