dvDocumentField function

String dvDocumentField({
  1. required String type,
  2. required String label,
  3. required String id,
  4. String? autocomplete,
})

One field, as a paragraph holding its label and its input.

type is an <input> type (text, email, password, ...) or textarea; anything that is not a plain word is written as text, since it goes into an attribute. id must be unique in the document. autocomplete is the token a password manager matches on; a password field defaults to current-password.

Implementation

String dvDocumentField({
  required String type,
  required String label,
  required String id,
  String? autocomplete,
}) {
  final String name = _attribute.convert(label);
  final StringBuffer out = StringBuffer(
      '<p><label for="$id">${_text.convert(label)}</label> ');
  if (type == 'textarea') {
    out.write('<textarea id="$id" name="$name" data-dv-field="$name">'
        '</textarea></p>');
    return out.toString();
  }
  final String safeType = RegExp(r'^[a-z-]+$').hasMatch(type) ? type : 'text';
  final String? complete =
      autocomplete ?? (safeType == 'password' ? 'current-password' : null);
  out.write('<input id="$id" type="$safeType" name="$name" '
      'data-dv-field="$name"');
  if (complete != null) {
    out.write(' autocomplete="${_attribute.convert(complete)}"');
  }
  out.write('>');
  if (safeType == 'password') {
    final String eye = _attribute.convert(dvRevealName);
    out.write('<button type="button" class="dv-reveal" aria-controls="$id" '
        'aria-pressed="false" title="$eye" aria-label="$eye" '
        'onclick="$dvRevealScript">${_text.convert(dvRevealName)}</button>');
  }
  out.write('</p>');
  return out.toString();
}