Package Exports
- @dreamworld/dw-input
- @dreamworld/dw-input/dw-input
- @dreamworld/dw-input/dw-input.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@dreamworld/dw-input) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
dw-input
A material input element made with lit-html. For more detail visit https://material.io/develop/web/components/input-controls/text-field/.
Installation
npm install @dreamworld/dw-inputUsage
@import '@dreamworld/dw-input/dw-input';Demo
Features
- It follows material design outlines input style and provides all features of it. know more
- It auto select's text if
autoSelectproperty is true - Provides a
validatorproperty to add custom validations - Set
multilineto true to show input as text area - Provide
typeproperty to set type of input e.g ("text", "email", "number"). List of all input types - Set
iconandiconTrailingto show prefix and suffix icon - Performs validation on blur. It also performs validation on User type if input is invalid.
Value parsing & Text Formatting
By default value property is exactly the text written in the text-field. But, When creating custom input elements
by extending dw-input we need to change this behavior. e.g. For we want to create a date-input, whose value will always be in yyyy-mm-dd format. But, allows user to choose input format. e.g. For American user it allows to enter date in mm/dd/yyyy format and for Indian users it allows to enter date in dd/mm/yyyy format. In addition, we need
1 more feature that date input can be done without / or -. And even partial dates can be entered. e.g.
- If I enter
12, then the date will be come 12th of the current month and year. (assumed input format is dd/mmy/yyyy) - If I enter
125, then the date will become 12th May of the current year. For such features, we need to do custom parsing of value from the input text and when user sets value property, we need to compute corresponding text representation.
This can be easily done by extending this element and then overriding following 2 functions:
parseValue(text), Receives input-text as argument and returns parsed value. e.g. for the above case it receives user inputted string and returns string date in formatyyyy-mm-dd.formatText(value), Receives value property as argument and returns formatted text to be shown in the input field. e.g. For the above exmaple, it receives date inyyyy-mm-ddformat and it's output is date representation indd/mm/yyyyformat for indian user.
How exactly parseValue and formatText function is used internally?
- Input text is formatted on 2 events: a. On the blur of the input b. User changes
valueproperty explicitly. In other word,valueproperty isn't changed due to user interaction in the text field. So,formatTextis used on these 2 events. - When user interacts with input text-field, at that time on each change in the text-field (as user types) this function
is invoked to parse the current text as value. When this function returns
undefined, thenvalueproperty isn't changed (stays to it's last value). In any other cases, includingnullvalue and blank-string returned, value is changed to the return value. This could be used when user is typing & parsing of the currently typed text isn't possible, so you can returnundefinedin such case.
Future extension
parseValue(text, userEditing): this function can receive one more argument userEditing. While user is editing the text-field it's value will be true, but when user leaves text-field this function is called again with false value.
This fact might be used by integrator to throw any error if user has completed editing, OR return null instead of undefined when user has finished editing.
Events
Triggers value-changed event on value change.
Methods
focus- Focuses the inputselectText- Selected input's textvalidate- Call this to validate input. Returns false if value is invalid.
Theme
Configure color of the icon using --dw-icon-color css variable.
Example css to change icon color
dw-input{
--dw-icon-color: green;
}Custom input
Override dwInput class to create a custom input
class CustomInput extends DwInput {
static get styles() {
return [
DwInput.styles,
css`
.mdc-text-field{
border-radius: 8px;
}`
];
}
}
customElements.define('custom-input', CustomInput);
<custom-input></custom-input>Examples
<dw-input label="Name" validator="<VALIDATION_FN>" placeholder="Enter name here" autoSelect required hint="Hint text"></dw-input>
<dw-input label="Number" disabled allowedPattern="[0-9]" value="12"></dw-input>
<dw-input label="Number" readOnly icon='search' iconTrailing='add_comment'></dw-input>
<dw-input noLabel multiline></dw-input>
<dw-input value="12" originalValue="12" highLightOnChanged></dw-input>dw-textarea
- The element provide way to autogrow textarea with non decore style.
Install
npm install @dw/dw-inputUsage
import '@dreamworld/dw-input/dw-textarea';Events
value-changedevent with input value.- Fires this event on input value is changed
enterevent with input value and event object.- Fires this user press
enterkey on input.
- Fires this user press
escevent with input value and event object.- Fires this event if user press
esckey on input.
- Fires this event if user press
blurevent with input value and event object.- Fires this event on textarea blur event.
Methods
focus- Focus in the inputfocusToEnd- Focus in the input at lastblur- Remove the input focusvalidate- Call this to validate input. Returns false if value is invalid.
Theme
- Configure padding of the textarea
--dw-textarea-paddingcss variable. - For Borders, direclty apply to
dw-textareaelement at the time of usage. Default border hasn't provided as it's raw element to be used for the custom purposes/UI. - It has no (transparent) background-color, so set background-color to
dw-textareaas per your need. - For typo-graphy, set relevant typography class from your theme (e.g.
material-styles) todw-textarea. No default, typography is applied. When used indw-input, it applies typo graphy as per the input fonts. - Font colors:
--mdc-theme-text-primary,--mdc-theme-text-hintand--mdc-theme-text-disabledare used for the font colors. So, change these css properties as per your need.
Features
- Auto grow input.
- Fixed height input with scroll
- Disabled enter
Auto grow input.
- Provide auto grow input based on
minHeightandmaxHeightproperty. - Input auto grows from
minHeighttomaxHeightafter that they show scroll.
Example with Auto grow input:
<dw-textarea .minHeight=${80} .maxHeight=${200}></dw-textarea>Fixed height input with scroll
- Provide input with fixed height after that they show scroll.
- Passed to
minHeightto input to show fix height
Example Fixed height input with scroll:
<dw-textarea .minHeight=${70} .maxHeight=${70}></dw-textarea>Disabled enter
- Provide way enter not allowed in input.
- Set
disabledEnterproperty set as atrue.
Example Disabled enter:
<dw-textarea .minHeight=${70} .maxHeight=${70} disabledEnter></dw-textarea>Other examples
- Read only input
<dw-textarea .minHeight=${80} .maxHeight=${200} .readOnly=${true}></dw-textarea>