shoelace/src/components/input/input.ts

429 wiersze
14 KiB
TypeScript
Czysty Zwykły widok Historia

import { html, LitElement } from 'lit';
2021-05-27 21:00:43 +00:00
import { customElement, property, query, state } from 'lit/decorators.js';
2021-09-29 12:40:26 +00:00
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
2021-09-29 12:40:26 +00:00
import { live } from 'lit/directives/live.js';
2022-03-24 11:48:03 +00:00
import '~/components/icon/icon';
import { emit } from '~/internal/event';
import { FormSubmitController } from '~/internal/form';
import { HasSlotController } from '~/internal/slot';
import { watch } from '~/internal/watch';
2021-07-10 00:45:44 +00:00
import styles from './input.styles';
2021-02-26 14:09:13 +00:00
/**
* @since 2.0
* @status stable
*
* @dependency sl-icon
*
2021-06-25 20:25:46 +00:00
* @slot label - The input's label. Alternatively, you can use the label prop.
* @slot prefix - Used to prepend an icon or similar element to the input.
* @slot suffix - Used to append an icon or similar element to the input.
* @slot clear-icon - An icon to use in lieu of the default clear icon.
* @slot show-password-icon - An icon to use in lieu of the default show password icon.
* @slot hide-password-icon - An icon to use in lieu of the default hide password icon.
* @slot help-text - Help text that describes how to use the input. Alternatively, you can use the help-text prop.
2021-02-26 14:09:13 +00:00
*
2022-02-12 18:46:27 +00:00
* @event sl-change - Emitted when an alteration to the control's value is committed by the user.
2021-06-25 20:25:46 +00:00
* @event sl-clear - Emitted when the clear button is activated.
2022-02-12 18:46:27 +00:00
* @event sl-input - Emitted when the control receives input and its value changes.
2021-06-25 20:25:46 +00:00
* @event sl-focus - Emitted when the control gains focus.
* @event sl-blur - Emitted when the control loses focus.
*
2021-06-25 20:25:46 +00:00
* @csspart form-control - The form control that wraps the label, input, and help-text.
2022-03-18 21:33:23 +00:00
* @csspart form-control-label - The label's wrapper.
* @csspart form-control-input - The input's wrapper.
* @csspart form-control-help-text - The help text's wrapper.
* @csspart base - The component's internal wrapper.
2021-06-25 20:25:46 +00:00
* @csspart input - The input control.
* @csspart prefix - The input prefix container.
* @csspart clear-button - The clear button.
* @csspart password-toggle-button - The password toggle button.
* @csspart suffix - The input suffix container.
2021-02-26 14:09:13 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-input')
2021-03-09 00:14:32 +00:00
export default class SlInput extends LitElement {
2021-07-10 00:45:44 +00:00
static styles = styles;
2021-03-06 17:01:39 +00:00
@query('.input__control') input: HTMLInputElement;
private readonly formSubmitController = new FormSubmitController(this);
private readonly hasSlotController = new HasSlotController(this, 'help-text', 'label');
2021-02-26 14:09:13 +00:00
@state() private hasFocus = false;
@state() private isPasswordVisible = false;
2021-03-06 17:01:39 +00:00
2021-02-26 14:09:13 +00:00
/** The input's type. */
2021-08-24 12:13:36 +00:00
@property({ reflect: true }) type: 'date' | 'email' | 'number' | 'password' | 'search' | 'tel' | 'text' | 'url' =
'text';
2021-02-26 14:09:13 +00:00
/** The input's size. */
2021-03-06 17:01:39 +00:00
@property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium';
2021-02-26 14:09:13 +00:00
/** The input's name attribute. */
2021-04-02 11:47:25 +00:00
@property() name: string;
2021-02-26 14:09:13 +00:00
/** The input's value attribute. */
2021-07-01 00:04:46 +00:00
@property() value = '';
2021-02-26 14:09:13 +00:00
2021-09-25 02:28:14 +00:00
/** Draws a filled input. */
@property({ type: Boolean, reflect: true }) filled = false;
2021-02-26 14:09:13 +00:00
/** Draws a pill-style input with rounded edges. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) pill = false;
2021-02-26 14:09:13 +00:00
/** The input's label. Alternatively, you can use the label slot. */
@property() label = '';
2021-02-26 14:09:13 +00:00
/** The input's help text. Alternatively, you can use the help-text slot. */
2021-07-01 00:04:46 +00:00
@property({ attribute: 'help-text' }) helpText = '';
2021-02-26 14:09:13 +00:00
/** Adds a clear button when the input is populated. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean }) clearable = false;
2021-02-26 14:09:13 +00:00
/** Adds a password toggle button to password inputs. */
2021-07-01 00:04:46 +00:00
@property({ attribute: 'toggle-password', type: Boolean }) togglePassword = false;
2021-02-26 14:09:13 +00:00
/** The input's placeholder text. */
2021-04-02 11:47:25 +00:00
@property() placeholder: string;
2021-02-26 14:09:13 +00:00
/** Disables the input. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2021-02-26 14:09:13 +00:00
/** Makes the input readonly. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) readonly = false;
2021-02-26 14:09:13 +00:00
/** The minimum length of input that will be considered valid. */
2021-03-06 17:01:39 +00:00
@property({ type: Number }) minlength: number;
2021-02-26 14:09:13 +00:00
/** The maximum length of input that will be considered valid. */
2021-03-06 17:01:39 +00:00
@property({ type: Number }) maxlength: number;
2021-02-26 14:09:13 +00:00
/** The input's minimum value. */
2021-03-06 17:01:39 +00:00
@property() min: number | string;
2021-02-26 14:09:13 +00:00
/** The input's maximum value. */
2021-03-06 17:01:39 +00:00
@property() max: number | string;
2021-02-26 14:09:13 +00:00
/** The input's step attribute. */
2021-03-06 17:01:39 +00:00
@property({ type: Number }) step: number;
2021-02-26 14:09:13 +00:00
/** A pattern to validate input against. */
2021-03-06 17:01:39 +00:00
@property() pattern: string;
2021-02-26 14:09:13 +00:00
/** Makes the input a required field. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) required = false;
2021-02-26 14:09:13 +00:00
/**
* This will be true when the control is in an invalid state. Validity is determined by props such as `type`,
* `required`, `minlength`, `maxlength`, and `pattern` using the browser's constraint validation API.
*/
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) invalid = false;
2021-02-26 14:09:13 +00:00
2022-01-01 01:39:16 +00:00
/** The input's autocapitalize attribute. */
2021-03-16 13:16:11 +00:00
@property() autocapitalize: 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters';
2021-02-26 14:09:13 +00:00
/** The input's autocorrect attribute. */
2021-03-06 17:01:39 +00:00
@property() autocorrect: string;
2021-02-26 14:09:13 +00:00
/** The input's autocomplete attribute. */
2021-03-06 17:01:39 +00:00
@property() autocomplete: string;
2021-02-26 14:09:13 +00:00
/** The input's autofocus attribute. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean }) autofocus: boolean;
2021-02-26 14:09:13 +00:00
/** Enables spell checking on the input. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean }) spellcheck: boolean;
2021-02-26 14:09:13 +00:00
/** The input's inputmode attribute. */
2021-03-06 17:01:39 +00:00
@property() inputmode: 'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url';
/** Gets or sets the current value as a `Date` object. Only valid when `type` is `date`. */
get valueAsDate() {
2022-03-02 17:19:59 +00:00
return this.input?.valueAsDate ?? null;
}
2022-03-02 17:19:59 +00:00
set valueAsDate(newValue: Date | null) {
this.updateComplete.then(() => {
this.input.valueAsDate = newValue;
this.value = this.input.value;
});
}
/** Gets or sets the current value as a number. */
get valueAsNumber() {
2022-03-02 17:19:59 +00:00
return this.input?.valueAsNumber ?? parseFloat(this.value);
}
set valueAsNumber(newValue: number) {
2022-03-02 17:19:59 +00:00
this.updateComplete.then(() => {
this.input.valueAsNumber = newValue;
this.value = this.input.value;
});
}
2021-06-16 12:42:02 +00:00
firstUpdated() {
this.invalid = !this.input.checkValidity();
}
2021-02-26 14:09:13 +00:00
/** Sets focus on the input. */
focus(options?: FocusOptions) {
2021-02-26 14:09:13 +00:00
this.input.focus(options);
}
/** Removes focus from the input. */
blur() {
2021-02-26 14:09:13 +00:00
this.input.blur();
}
/** Selects all the text in the input. */
select() {
this.input.select();
2021-02-26 14:09:13 +00:00
}
/** Sets the start and end positions of the text selection (0-based). */
setSelectionRange(
selectionStart: number,
selectionEnd: number,
selectionDirection: 'forward' | 'backward' | 'none' = 'none'
) {
this.input.setSelectionRange(selectionStart, selectionEnd, selectionDirection);
2021-02-26 14:09:13 +00:00
}
/** Replaces a range of text with a new string. */
setRangeText(
replacement: string,
start: number,
end: number,
selectMode: 'select' | 'start' | 'end' | 'preserve' = 'preserve'
) {
this.input.setRangeText(replacement, start, end, selectMode);
if (this.value !== this.input.value) {
this.value = this.input.value;
emit(this, 'sl-input');
emit(this, 'sl-change');
2021-02-26 14:09:13 +00:00
}
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() {
return this.input.reportValidity();
}
/** Sets a custom validation message. If `message` is not empty, the field will be considered invalid. */
setCustomValidity(message: string) {
this.input.setCustomValidity(message);
this.invalid = !this.input.checkValidity();
}
2021-06-25 23:22:05 +00:00
handleBlur() {
this.hasFocus = false;
2021-06-25 23:40:12 +00:00
emit(this, 'sl-blur');
2021-06-25 23:22:05 +00:00
}
2021-02-26 14:09:13 +00:00
handleChange() {
this.value = this.input.value;
emit(this, 'sl-change');
2021-02-26 14:09:13 +00:00
}
2021-06-25 23:22:05 +00:00
handleClearClick(event: MouseEvent) {
this.value = '';
2021-06-25 23:40:12 +00:00
emit(this, 'sl-clear');
emit(this, 'sl-input');
2021-06-25 23:40:12 +00:00
emit(this, 'sl-change');
2021-06-25 23:22:05 +00:00
this.input.focus();
2021-02-26 14:09:13 +00:00
2021-06-25 23:22:05 +00:00
event.stopPropagation();
2021-02-26 14:09:13 +00:00
}
@watch('disabled', { waitUntilFirstUpdate: true })
2021-06-25 23:22:05 +00:00
handleDisabledChange() {
// Disabled form controls are always valid, so we need to recheck validity when the state changes
this.input.disabled = this.disabled;
this.invalid = !this.input.checkValidity();
2021-02-26 14:09:13 +00:00
}
handleFocus() {
this.hasFocus = true;
emit(this, 'sl-focus');
2021-02-26 14:09:13 +00:00
}
2021-06-25 23:22:05 +00:00
handleInput() {
this.value = this.input.value;
emit(this, 'sl-input');
2021-06-25 23:22:05 +00:00
}
2021-02-26 14:09:13 +00:00
2021-06-25 23:22:05 +00:00
handleInvalid() {
this.invalid = true;
2021-02-26 14:09:13 +00:00
}
2022-03-01 15:24:57 +00:00
handleKeyDown(event: KeyboardEvent) {
const hasModifier = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
2022-03-01 15:24:57 +00:00
// Pressing enter when focused on an input should submit the form like a native input
if (event.key === 'Enter' && !hasModifier) {
2022-03-01 15:24:57 +00:00
this.formSubmitController.submit();
}
}
2021-02-26 14:09:13 +00:00
handlePasswordToggle() {
this.isPasswordVisible = !this.isPasswordVisible;
}
@watch('value', { waitUntilFirstUpdate: true })
2021-03-06 19:39:48 +00:00
handleValueChange() {
this.invalid = !this.input.checkValidity();
2021-03-06 19:39:48 +00:00
}
2021-02-26 14:09:13 +00:00
render() {
const hasLabelSlot = this.hasSlotController.test('label');
const hasHelpTextSlot = this.hasSlotController.test('help-text');
2022-03-08 22:34:17 +00:00
const hasLabel = this.label ? true : !!hasLabelSlot;
const hasHelpText = this.helpText ? true : !!hasHelpTextSlot;
return html`
<div
part="form-control"
class=${classMap({
'form-control': true,
'form-control--small': this.size === 'small',
'form-control--medium': this.size === 'medium',
'form-control--large': this.size === 'large',
'form-control--has-label': hasLabel,
'form-control--has-help-text': hasHelpText
})}
>
2022-03-18 21:33:23 +00:00
<label
part="form-control-label"
class="form-control__label"
for="input"
aria-hidden=${hasLabel ? 'false' : 'true'}
>
2022-03-08 22:34:17 +00:00
<slot name="label">${this.label}</slot>
</label>
2022-03-18 21:33:23 +00:00
<div part="form-control-input" class="form-control-input">
2022-03-08 22:34:17 +00:00
<div
part="base"
class=${classMap({
input: true,
// Sizes
'input--small': this.size === 'small',
'input--medium': this.size === 'medium',
'input--large': this.size === 'large',
// States
'input--pill': this.pill,
'input--standard': !this.filled,
'input--filled': this.filled,
'input--disabled': this.disabled,
'input--focused': this.hasFocus,
'input--empty': this.value.length === 0,
'input--invalid': this.invalid
})}
>
<span part="prefix" class="input__prefix">
<slot name="prefix"></slot>
</span>
<input
part="input"
id="input"
class="input__control"
type=${this.type === 'password' && this.isPasswordVisible ? 'text' : this.type}
name=${ifDefined(this.name)}
?disabled=${this.disabled}
?readonly=${this.readonly}
?required=${this.required}
placeholder=${ifDefined(this.placeholder)}
minlength=${ifDefined(this.minlength)}
maxlength=${ifDefined(this.maxlength)}
min=${ifDefined(this.min)}
max=${ifDefined(this.max)}
step=${ifDefined(this.step)}
.value=${live(this.value)}
autocapitalize=${ifDefined(this.autocapitalize)}
autocomplete=${ifDefined(this.autocomplete)}
autocorrect=${ifDefined(this.autocorrect)}
?autofocus=${this.autofocus}
spellcheck=${ifDefined(this.spellcheck)}
pattern=${ifDefined(this.pattern)}
inputmode=${ifDefined(this.inputmode)}
aria-describedby="help-text"
aria-invalid=${this.invalid ? 'true' : 'false'}
@change=${this.handleChange}
@input=${this.handleInput}
@invalid=${this.handleInvalid}
@keydown=${this.handleKeyDown}
@focus=${this.handleFocus}
@blur=${this.handleBlur}
/>
${this.clearable && this.value.length > 0
? html`
<button
part="clear-button"
class="input__clear"
type="button"
2022-03-16 21:39:39 +00:00
aria-hidden="true"
2022-03-08 22:34:17 +00:00
@click=${this.handleClearClick}
tabindex="-1"
>
<slot name="clear-icon">
<sl-icon name="x-circle-fill" library="system"></sl-icon>
</slot>
</button>
`
: ''}
${this.togglePassword
? html`
<button
part="password-toggle-button"
class="input__password-toggle"
type="button"
2022-03-16 21:39:39 +00:00
aria-hidden="true"
2022-03-08 22:34:17 +00:00
@click=${this.handlePasswordToggle}
tabindex="-1"
>
${this.isPasswordVisible
? html`
<slot name="show-password-icon">
<sl-icon name="eye-slash" library="system"></sl-icon>
</slot>
`
: html`
<slot name="hide-password-icon">
<sl-icon name="eye" library="system"></sl-icon>
</slot>
`}
</button>
`
: ''}
<span part="suffix" class="input__suffix">
<slot name="suffix"></slot>
</span>
</div>
</div>
2021-02-26 14:09:13 +00:00
<div
2022-03-18 21:33:23 +00:00
part="form-control-help-text"
2022-03-08 22:34:17 +00:00
id="help-text"
class="form-control__help-text"
aria-hidden=${hasHelpText ? 'false' : 'true'}
2021-02-26 14:09:13 +00:00
>
2022-03-08 22:34:17 +00:00
<slot name="help-text">${this.helpText}</slot>
2021-02-26 14:09:13 +00:00
</div>
2022-03-08 22:34:17 +00:00
</div>
`;
2021-02-26 14:09:13 +00:00
}
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-input': SlInput;
}
}