holakit
Version:
Yet another design-driven UI component set.
296 lines (293 loc) • 9.94 kB
JavaScript
/*! Built with http://stenciljs.com */
const { h } = window.HolakitCore;
/*
* Thanks to Ionic team, we stole this file from ionic.
* https://github.com/ionic-team/ionic/blob/master/core/src/components/input/input.tsx
*/
class Input {
constructor() {
this.didBlurAfterEdit = false;
this.hasFocus = false;
/**
* Indicates whether and how the text value should be automatically capitalized as it is entered/edited by the user.
*/
this.autocapitalize = 'off';
/**
* Indicates whether the value of the control can be automatically completed by the browser.
*/
this.autocomplete = 'off';
/**
* Whether auto correction should be enabled when the user is entering/editing the text value.
*/
this.autocorrect = 'off';
/**
* This Boolean attribute lets you specify that a form control should have input focus when the page loads.
*/
this.autofocus = false;
/**
* If `true`, a clear icon will appear in the input when there is a value. Clicking it clears the input.
*/
this.clearInput = false;
/**
* If `true`, the user cannot interact with the input.
*/
this.disabled = false;
/**
* If `true`, the user cannot modify the value.
*/
this.readonly = false;
/**
* If `true`, the user must fill in a value before submitting a form.
*/
this.required = false;
/**
* If `true`, the element will have its spelling and grammar checked.
*/
this.spellcheck = false;
/**
* The type of control to display. The default type is text.
*/
this.type = 'text';
/**
* The value of the input.
*/
this.value = '';
this.onInput = (ev) => {
const input = ev.target;
if (input) {
this.value = input.value || '';
}
this.holaInput.emit(ev);
};
this.onBlur = () => {
this.hasFocus = false;
this.focusChanged();
this.holaBlur.emit();
};
this.onFocus = () => {
this.hasFocus = true;
this.focusChanged();
this.holaFocus.emit();
};
this.onKeydown = () => {
if (this.clearOnEdit) {
// Did the input value change after it was blurred and edited?
if (this.didBlurAfterEdit && this.hasValue()) {
// Clear the input
this.clearTextInput();
}
// Reset the flag
this.didBlurAfterEdit = false;
}
};
this.clearTextInput = () => {
this.value = '';
};
}
/**
* Update the native input element when the value changes
*/
valueChanged() {
this.holaChange.emit({ value: this.value });
}
componentWillLoad() {
// By default, password inputs clear after focus when they have content
if (this.clearOnEdit === undefined && this.type === 'password') {
this.clearOnEdit = true;
}
}
componentDidLoad() {
this.holaInputDidLoad.emit();
}
componentDidUnload() {
this.holaInputDidUnload.emit();
}
/**
* Sets focus on the specified `hola-input`. Use this method instead of the global
* `input.focus()`.
*/
setFocus() {
if (this.nativeInput) {
this.nativeInput.focus();
}
}
getValue() {
return this.value || '';
}
focusChanged() {
// If clearOnEdit is enabled and the input blurred but has a value, set a flag
if (this.clearOnEdit && !this.hasFocus && this.hasValue()) {
this.didBlurAfterEdit = true;
}
}
hasValue() {
return this.getValue().length > 0;
}
hostData() {
return {
'aria-disabled': this.disabled ? 'true' : null,
class: {
'has-value': this.hasValue(),
'has-focus': this.hasFocus
}
};
}
render() {
const value = this.getValue();
return [
h("input", { class: "native-input", ref: input => this.nativeInput = input, disabled: this.disabled, accept: this.accept, autoCapitalize: this.autocapitalize, autoComplete: this.autocomplete, autoCorrect: this.autocorrect, autoFocus: this.autofocus, inputMode: this.inputmode, min: this.min, max: this.max, minLength: this.minlength, maxLength: this.maxlength, multiple: this.multiple, name: this.name, pattern: this.pattern, placeholder: this.placeholder || '', readOnly: this.readonly, required: this.required, spellCheck: this.spellcheck, step: this.step, size: this.size, type: this.type, value: value, onInput: this.onInput, onBlur: this.onBlur, onFocus: this.onFocus, onKeyDown: this.onKeydown }),
(this.clearInput && !this.readonly && !this.disabled) && h("button", { type: "button", class: "input-clear-icon", tabindex: "-1", onTouchStart: this.clearTextInput, onMouseDown: this.clearTextInput })
];
}
static get is() { return "hola-input"; }
static get encapsulation() { return "shadow"; }
static get properties() { return {
"accept": {
"type": String,
"attr": "accept"
},
"autocapitalize": {
"type": String,
"attr": "autocapitalize"
},
"autocomplete": {
"type": String,
"attr": "autocomplete"
},
"autocorrect": {
"type": String,
"attr": "autocorrect"
},
"autofocus": {
"type": Boolean,
"attr": "autofocus"
},
"clearInput": {
"type": Boolean,
"attr": "clear-input"
},
"clearOnEdit": {
"type": Boolean,
"attr": "clear-on-edit",
"mutable": true
},
"disabled": {
"type": Boolean,
"attr": "disabled"
},
"el": {
"elementRef": true
},
"hasFocus": {
"state": true
},
"inputmode": {
"type": String,
"attr": "inputmode"
},
"max": {
"type": String,
"attr": "max"
},
"maxlength": {
"type": Number,
"attr": "maxlength"
},
"min": {
"type": String,
"attr": "min"
},
"minlength": {
"type": Number,
"attr": "minlength"
},
"multiple": {
"type": Boolean,
"attr": "multiple"
},
"name": {
"type": String,
"attr": "name"
},
"pattern": {
"type": String,
"attr": "pattern"
},
"placeholder": {
"type": String,
"attr": "placeholder"
},
"readonly": {
"type": Boolean,
"attr": "readonly"
},
"required": {
"type": Boolean,
"attr": "required"
},
"setFocus": {
"method": true
},
"size": {
"type": Number,
"attr": "size"
},
"spellcheck": {
"type": Boolean,
"attr": "spellcheck"
},
"step": {
"type": String,
"attr": "step"
},
"type": {
"type": String,
"attr": "type"
},
"value": {
"type": String,
"attr": "value",
"mutable": true,
"watchCallbacks": ["valueChanged"]
}
}; }
static get events() { return [{
"name": "holaInput",
"method": "holaInput",
"bubbles": true,
"cancelable": true,
"composed": true
}, {
"name": "holaChange",
"method": "holaChange",
"bubbles": true,
"cancelable": true,
"composed": true
}, {
"name": "holaBlur",
"method": "holaBlur",
"bubbles": true,
"cancelable": true,
"composed": true
}, {
"name": "holaFocus",
"method": "holaFocus",
"bubbles": true,
"cancelable": true,
"composed": true
}, {
"name": "holaInputDidLoad",
"method": "holaInputDidLoad",
"bubbles": true,
"cancelable": true,
"composed": true
}, {
"name": "holaInputDidUnload",
"method": "holaInputDidUnload",
"bubbles": true,
"cancelable": true,
"composed": true
}]; }
static get style() { return ".native-input {\n background: #fff;\n border-radius: 0;\n border: 1px solid #ccc;\n font-size: 1em;\n line-height: 1.5;\n -webkit-transition: 0.5s;\n transition: 0.5s;\n padding: 0.44em 0.7em;\n -moz-appearance: none;\n -webkit-appearance: none;\n appearance: none;\n}\n\n.native-input:focus {\n -webkit-box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.5);\n box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.5);\n -webkit-box-shadow: 0 0 0 3px var(--hola-focus-color);\n box-shadow: 0 0 0 3px var(--hola-focus-color);\n}\n\n.hola-form-ctrl.hola-input-singleline + .hola-button {\n margin-left: 3px;\n}"; }
}
export { Input as HolaInput };