Element Framework

Text Field

  • HTML
  • JS
  • CSS
<ef-panel spacing>
  <p>First name</p>
  <ef-text-field placeholder="Must be letters and at least 5 characters" pattern="[a-zA-Z]{5,}"></ef-text-field>
  <br>
  <br>
  <p>Last name</p>
  <ef-text-field placeholder="Must be letters and at least 5 characters" pattern="[a-zA-Z]{5,}"></ef-text-field>
</ef-panel>

                  
ef-panel {
  max-width: 450px;
}

ef-text-field {
  width: 250px;
}

p {
  margin-bottom: 4px;
}

ef-text-field is a form element for text.

Basic usage

Text field is used to accept text input from users and has similar behaviors to the native text input.

<ef-text-field placeholder="Type something..."></ef-text-field>

Getting value

The field's value can be accessed using the value property.

<ef-text-field id="text-input" value="Hello World"></ef-text-field>
var textInput = document.getElementById("text-input");
textInput.value; // "Hello World"

You can also listen for the value-changed event, which triggers when user interactions change the value.

  • HTML
  • JS
  • CSS
<ef-text-field id="input" placeholder="Type something here .."></ef-text-field>
<p>Value: <code id="value-text"></code></p>
var element = document.getElementById("input");
var valueChangedText = document.getElementById('value-text');

element.addEventListener("value-changed", function(e) {
  valueChangedText.innerHTML = e.detail.value;
});

                  
<ef-text-field id="input"></ef-text-field>
<p>Value: <code id="value-text"></code></p>
var element = document.getElementById("input");
var valueChangedText = document.getElementById("value-text");

element.addEventListener("value-changed", function (e) {
  valueChangedText.innerHTML = e.detail.value;
});

Input validation

Validation occurs when constraints are provided and the value changes. If the error state changes, it will dispatch an error-changed event along with current error state.

Alternatively, you can check the error property to confirm if the input is valid or not.

Input length

The maxlength attribute limits the number of characters that users can type into the input, and the minlength attribute sets the minimum number of characters required. ef-text-field will show error styles if a condition is not met.

  • HTML
  • JS
  • CSS
<ef-text-field id="textInput" minlength="5" maxlength="8" placeholder="Between 5 to 8 characters"></ef-text-field>
<p id="error-text"></p>
var element = document.getElementById("textInput");
var errorChangedText = document.getElementById('error-text');
element.addEventListener("error-changed", function(e) {
  if (e.detail.value) {
    errorChangedText.innerHTML = "Value length must be between 5 -8 characters";
  } else {
    errorChangedText.innerHTML = "";
  }
});
#error-text {
  color: #d94255;
}

ef-text-field {
  width: 200px;
}
<ef-text-field minlength="5" maxlength="8"></ef-text-field>
<p id="error-text"></p>
var element = document.getElementById("textInput");
var errorChangedText = document.getElementById("error-text");
element.addEventListener("error-changed", function (e) {
  if (e.detail.value) {
    errorChangedText.innerHTML =
      "Value length must be between 5 -8 characters.";
  } else {
    errorChangedText.innerHTML = "";
  }
});

Pattern

You can use a regular expression to validate the input value by setting it with the pattern attribute.

  • HTML
  • JS
  • CSS
<div>Nickname</div>
<ef-text-field id="nickname" pattern="[a-z]{4,8}" placeholder="Must be lowercase letters and 4-8 characters"></ef-text-field>
<p id="error-text"></p>
var element = document.getElementById("nickname");
var errorChangedText = document.getElementById("error-text");
element.addEventListener("error-changed", function(e) {
  if (e.detail.value) {
    errorChangedText.innerHTML = "Nickname must be lowercase letters and 4-8 characters.";
  } else {
    errorChangedText.innerHTML = "";
  }
});
#error-text {
  color: #d94255;
}

ef-text-field {
  width: 275px;
}
Nickname: <ef-text-field id="nickname" pattern="[a-z]{4,8}"></ef-text-field>
<p id="error-text"></>
var element = document.getElementById("nickname");
var errorChangedText = document.getElementById("error-text");
element.addEventListener("error-changed", function (e) {
  if (e.detail.value) {
    errorChangedText.innerHTML = "Nickname must be lowercase letters and 4-8 characters.";
  } else {
    errorChangedText.innerHTML = "";
  }
});

Icon

An inline icon can be set to display inside the input using the icon attribute.

  • HTML
  • JS
  • CSS
<ef-text-field icon="email" placeholder="We appreciate your feedback!"></ef-text-field>

                  
ef-text-field {
  width: 200px;
}
<ef-text-field icon="email"></ef-text-field>

The icon can become actionable by adding the icon-has-action attribute to the element, so that ef-text-field will fire the icon-click event when users click on the icon. You can add an event listener to this event to execute your code.

  • HTML
  • JS
  • CSS
<ef-text-field id="feedback" placeholder="Type your feedback and click the icon" icon="email" icon-has-action></ef-text-field>
var element = document.getElementById("feedback");
element.addEventListener("icon-click", function(e) {
  element.value = ""
  element.placeholder = "Feedback sent. Thanks!";
  element.icon = "tick";
});
ef-text-field {
  width: 250px;
}
<ef-text-field
  id="feedback"
  placeholder="Type your feedback and click the icon"
  icon="email"
  icon-has-action>
</ef-text-field>
var element = document.getElementById("feedback");
element.addEventListener("icon-click", function (e) {
    element.value = ""
    element.placeholder = "Feedback sent. Thanks!";
    element.icon = "tick";
});

API Reference

Attributes

string
pattern
Set regular expression for input validation
string
placeholder
Set place holder text
boolean
error
Set state to error
boolean
warning
Set state to warning
string
icon
Specify icon to display in input. Value can be icon name
boolean
icon-has-action
Specify when icon need to be clickable
boolean
transparent
Disables all other states and border/background styles. Use with advanced composite elements requiring e.g. multi selection in combo-box when parent container handles element states.
number | null
maxlength
Set character max limit
number | null
minlength
Set character min limit
string
value
Input's value
boolean
readonly
Set readonly state
boolean
disabled
Set disabled state

Properties

string
pattern
Set regular expression for input validation
""
string
placeholder
Set place holder text
""
boolean
error
Set state to error
false
boolean
warning
Set state to warning
false
string
icon
Specify icon to display in input. Value can be icon name
""
boolean
iconHasAction
Specify when icon need to be clickable
false
boolean
transparent
Disables all other states and border/background styles. Use with advanced composite elements requiring e.g. multi selection in combo-box when parent container handles element states.
false
number | null
maxLength
Set character max limit
number | null
minLength
Set character min limit
number | null
selectionStart
Selection start index
number | null
selectionEnd
Selection end index
string
value
Input's value
""
boolean
readonly
Set readonly state
false
boolean
disabled
Set disabled state
false

Methods

select()
Select the contents of input
setSelectionRange(startSelection,endSelection)
Set the selection range
startSelection
Start of selection
endSelection
End of the selection

Events

value-changed
Dispatched when value changes
error-changed
Dispatched when error state changes
icon-click
Dispatched only when element has icon-has-action attribute and icon is clicked