shoelace/src/components/input/input.ts

539 wiersze
20 KiB
TypeScript
Czysty Zwykły widok Historia

2022-08-17 15:37:37 +00:00
import { html } 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-06-28 22:08:28 +00:00
import { defaultValue } from '../../internal/default-value';
2022-03-24 12:01:09 +00:00
import { FormSubmitController } from '../../internal/form';
2022-08-17 15:37:37 +00:00
import ShoelaceElement from '../../internal/shoelace-element';
2022-03-24 12:01:09 +00:00
import { HasSlotController } from '../../internal/slot';
import { watch } from '../../internal/watch';
2022-04-13 13:39:29 +00:00
import { LocalizeController } from '../../utilities/localize';
2022-08-08 14:15:41 +00:00
import '../icon/icon';
2021-07-10 00:45:44 +00:00
import styles from './input.styles';
import type { ShoelaceFormControl } from '../../internal/shoelace-element';
import type { CSSResultGroup } from 'lit';
2021-02-26 14:09:13 +00:00
2022-08-15 15:02:36 +00:00
// It's currently impossible to hide Firefox's built-in clear icon when using <input type="date|time">, so we need this
// check to apply a clip-path to hide it. I know, I know...user agent sniffing is nasty but, if it fails, we only see a
// redundant clear icon so nothing important is breaking. The benefits outweigh the costs for this one. See the
// discussion at: https://github.com/shoelace-style/shoelace/pull/794
//
// Also note that we do the Chromium check first to prevent Chrome from logging a console notice as described here:
// https://github.com/shoelace-style/shoelace/issues/855
//
const isChromium = navigator.userAgentData?.brands.some(b => b.brand.includes('Chromium'));
const isFirefox = isChromium ? false : navigator.userAgent.includes('Firefox');
2021-02-26 14:09:13 +00:00
/**
Enrich components `@summary` with description from docs (#962) * keep header styles with repositioned description text * `animated-image` move description to component * code style * `avatar` add summary from docs * `badge` add summary from docs * `breadcrumb` add summary from docs * `button` add summary from docs * lead sentence is now part of the header * `button-group` add summary from docs * `card` add summary from docs * `checkbox` add summary from docs * `color-picker` add summary from docs * `details` add summary from docs * `dialog` add summary from docs * `divider` add summary from docs * `drawer` add summary from docs * `dropdown` add summary from docs * `format-bytes` add summary from docs * `format-date` add summary from docs * `format-number` add summary from docs * `icon` add summary from docs * `icon-button` add summary from docs * `image-comparer` add summary from docs * `include` add summary from docs * `input` add summary from docs * `menu` add summary from docs * `menu-item` add summary from docs * `menu-label` add summary from docs * `popup` add summary from docs * `progressbar` add summary from docs * `progress-ring` add summary from docs * `radio` add summary from docs * `radio-button` add summary from docs * `range` add summary from docs * `rating` add summary from docs * `relative-time` add summary from docs * `select` add summary from docs * `skeleton` add summary from docs * `spinner` add summary from docs * `split-panel` add summary from docs * `switch` add summary from docs * `tab-group` add summary from docs * `tag` add summary from docs * `textarea` add summary from docs * `tooltip` add summary from docs * `visually-hidden` add summary from docs * `animation` add summary from docs * `breadcrumb-item` add summary from docs * `mutation-observer` add summary from docs * `radio-group` add summary from docs * `resize-observer` add summary from docs * `tab` add summary from docs * `tab-panel` add summary from docs * `tree` add summary from docs * `tree-item` add summary from docs * remove `title` for further usage of `Sl` classnames in docs * revert: use markdown parser for component summary
2022-10-21 13:56:35 +00:00
* @summary Inputs collect data from the user.
*
2021-02-26 14:09:13 +00:00
* @since 2.0
* @status stable
*
* @dependency sl-icon
*
2022-08-03 15:06:52 +00:00
* @slot label - The input's label. Alternatively, you can use the `label` attribute.
2022-12-06 16:18:14 +00:00
* @slot prefix - Used to prepend a presentational icon or similar element to the input.
* @slot suffix - Used to append a presentational icon or similar element to the input.
2021-06-25 20:25:46 +00:00
* @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.
2022-12-06 16:18:14 +00:00
* @slot help-text - Text that describes how to use the input. Alternatively, you can use the `help-text` attribute.
2021-02-26 14:09:13 +00:00
*
2022-12-07 22:40:46 +00:00
* @event sl-blur - Emitted when the control loses focus.
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.
* @event sl-focus - Emitted when the control gains focus.
2022-12-07 22:40:46 +00:00
* @event sl-input - Emitted when the control receives input.
*
2022-12-06 16:18:14 +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.
2022-12-06 16:18:14 +00:00
* @csspart base - The component's base wrapper.
* @csspart input - The internal `<input>` control.
* @csspart prefix - The container that wraps the prefix.
2021-06-25 20:25:46 +00:00
* @csspart clear-button - The clear button.
* @csspart password-toggle-button - The password toggle button.
2022-12-06 16:18:14 +00:00
* @csspart suffix - The container that wraps the suffix.
2021-02-26 14:09:13 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-input')
export default class SlInput extends ShoelaceElement implements ShoelaceFormControl {
static styles: CSSResultGroup = 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');
2022-04-13 13:39:29 +00:00
private readonly localize = new LocalizeController(this);
2021-02-26 14:09:13 +00:00
@state() private hasFocus = false;
@state() invalid = false;
2022-11-30 20:45:21 +00:00
@property() title = ''; // make reactive to pass through
2021-03-06 17:01:39 +00:00
2022-12-06 16:18:14 +00:00
/**
* The type of input. Works the same as a native `<input>` element, but only a subset of types are supported. Defaults
* to `text`.
*/
2022-04-26 12:13:13 +00:00
@property({ reflect: true }) type:
| 'date'
| 'datetime-local'
2022-04-26 12:13:13 +00:00
| 'email'
| 'number'
| 'password'
| 'search'
| 'tel'
| 'text'
| 'time'
| '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
2022-12-06 16:18:14 +00:00
/** The name of the input, submitted as a name/value pair with form data. */
@property() name = '';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** The current value of the input, submitted as a name/value pair with form data. */
2021-07-01 00:04:46 +00:00
@property() value = '';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** The default value of the form control. Primarily used for resetting the form control. */
@defaultValue() defaultValue = '';
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
2022-12-06 16:18:14 +00:00
/** The input's label. If you need to display HTML, use the `label` slot instead. */
@property() label = '';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** The input's help text. If you need to display HTML, use the `help-text` slot instead. */
2021-07-01 00:04:46 +00:00
@property({ attribute: 'help-text' }) helpText = '';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** Adds a clear button when the input is not empty. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean }) clearable = false;
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** Adds a button to toggle the password's visibility. Only applies to password types. */
2022-09-16 19:27:10 +00:00
@property({ attribute: 'password-toggle', type: Boolean }) passwordToggle = false;
2022-12-06 16:18:14 +00:00
/** Determines whether or not the password is currently visible. Only applies to password input types. */
2022-09-16 19:27:10 +00:00
@property({ attribute: 'password-visible', type: Boolean }) passwordVisible = false;
2021-02-26 14:09:13 +00:00
2022-06-23 20:57:30 +00:00
/** Hides the browser's built-in increment/decrement spin buttons for number inputs. */
@property({ attribute: 'no-spin-buttons', type: Boolean }) noSpinButtons = false;
2022-12-06 16:18:14 +00:00
/** Placeholder text to show as a hint when the input is empty. */
@property() placeholder = '';
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
2022-12-06 16:18:14 +00:00
/** The input's minimum value. Only applies to date and number input types. */
@property() min: number;
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** The input's maximum value. Only applies to date and number input types. */
@property() max: number;
2021-02-26 14:09:13 +00:00
2022-07-27 20:45:39 +00:00
/**
* Specifies the granularity that the value must adhere to, or the special value `any` which means no stepping is
2022-12-06 16:18:14 +00:00
* implied, allowing any numeric value. Only applies to date and number input types.
2022-07-27 20:45:39 +00:00
*/
@property() step: number | 'any';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** A regular expression 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
2022-12-06 16:18:14 +00:00
/** Controls whether and how text input is automatically capitalized as it is entered by the user. */
2021-03-16 13:16:11 +00:00
@property() autocapitalize: 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** Indicates whether the browser's autocorrect feature is on or off. */
@property() autocorrect: 'off' | 'on';
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/**
* Specifies what permission the browser has to provide assistance in filling out form field values. Refer to
* [this page on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete) for available values.
*/
2021-03-06 17:01:39 +00:00
@property() autocomplete: string;
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** Indicates that the input should receive focus on page load. */
2021-03-06 17:01:39 +00:00
@property({ type: Boolean }) autofocus: boolean;
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/** Used to customize the label or icon of the Enter key on virtual keyboards. */
2022-04-04 13:47:44 +00:00
@property() enterkeyhint: 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send';
2021-02-26 14:09:13 +00:00
/** Enables spell checking on the input. */
2022-12-08 19:42:04 +00:00
@property({
type: Boolean,
converter: {
// Allow "true|false" attribute values but keep the property boolean
fromAttribute: value => (!value || value === 'false' ? false : true),
toAttribute: value => (value ? 'true' : 'false')
}
})
spellcheck = true;
2021-02-26 14:09:13 +00:00
2022-12-06 16:18:14 +00:00
/**
* Tells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual
* keyboard on supportive devices.
*/
2021-03-06 17:01:39 +00:00
@property() inputmode: 'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url';
2022-12-06 16:18:14 +00:00
/** Gets or sets the current value as a `Date` object. Returns `null` if the value can't be converted. */
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) {
2022-06-22 13:18:34 +00:00
// We use an in-memory input instead of the one in the template because the property can be set before render
const input = document.createElement('input');
input.type = 'date';
input.valueAsDate = newValue;
this.value = input.value;
}
2022-12-06 16:18:14 +00:00
/** Gets or sets the current value as a number. Returns `NaN` if the value can't be converted. */
get valueAsNumber() {
2022-03-02 17:19:59 +00:00
return this.input?.valueAsNumber ?? parseFloat(this.value);
}
set valueAsNumber(newValue: number) {
2022-06-22 13:18:34 +00:00
// We use an in-memory input instead of the one in the template because the property can be set before render
const input = document.createElement('input');
input.type = 'number';
input.valueAsNumber = newValue;
this.value = 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,
2022-12-07 22:40:46 +00:00
start?: number,
end?: number,
selectMode?: 'select' | 'start' | 'end' | 'preserve'
2021-02-26 14:09:13 +00:00
) {
2022-12-07 22:40:46 +00:00
// @ts-expect-error - start, end, and selectMode are optional
2021-02-26 14:09:13 +00:00
this.input.setRangeText(replacement, start, end, selectMode);
if (this.value !== this.input.value) {
this.value = this.input.value;
}
}
/** Displays the browser picker for an input element (only works if the browser supports it for the input type). */
showPicker() {
if ('showPicker' in HTMLInputElement.prototype) {
this.input.showPicker();
}
}
/** Increments the value of a numeric input type by the value of the step attribute. */
stepUp() {
this.input.stepUp();
if (this.value !== this.input.value) {
this.value = this.input.value;
}
}
/** Decrements the value of a numeric input type by the value of the step attribute. */
stepDown() {
this.input.stepDown();
if (this.value !== this.input.value) {
this.value = this.input.value;
}
}
/** Checks for validity but does not show the browser's validation message. */
checkValidity() {
return this.input.checkValidity();
}
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;
2022-09-16 20:21:40 +00:00
this.emit('sl-blur');
2021-06-25 23:22:05 +00:00
}
2021-02-26 14:09:13 +00:00
handleChange() {
this.value = this.input.value;
2022-09-16 20:21:40 +00:00
this.emit('sl-change');
2021-02-26 14:09:13 +00:00
}
2021-06-25 23:22:05 +00:00
handleClearClick(event: MouseEvent) {
this.value = '';
2022-09-16 20:21:40 +00:00
this.emit('sl-clear');
this.emit('sl-input');
this.emit('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
}
2022-07-27 20:45:39 +00:00
@watch('step', { waitUntilFirstUpdate: true })
handleStepChange() {
// If step changes, the value may become invalid so we need to recheck after the update. We set the new step
// imperatively so we don't have to wait for the next render to report the updated validity.
this.input.step = String(this.step);
this.invalid = !this.input.checkValidity();
}
2021-02-26 14:09:13 +00:00
handleFocus() {
this.hasFocus = true;
2022-09-16 20:21:40 +00:00
this.emit('sl-focus');
2021-02-26 14:09:13 +00:00
}
2021-06-25 23:22:05 +00:00
handleInput() {
this.value = this.input.value;
2022-09-16 20:21:40 +00:00
this.emit('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-05-27 12:15:31 +00:00
// Pressing enter when focused on an input should submit the form like a native input, but we wait a tick before
// submitting to allow users to cancel the keydown event if they need to
if (event.key === 'Enter' && !hasModifier) {
2022-05-27 12:15:31 +00:00
setTimeout(() => {
2022-11-10 13:06:35 +00:00
//
// When using an Input Method Editor (IME), pressing enter will cause the form to submit unexpectedly. One way
// to check for this is to look at event.isComposing, which will be true when the IME is open.
//
// See https://github.com/shoelace-style/shoelace/pull/988
//
if (!event.defaultPrevented && !event.isComposing) {
2022-05-27 12:15:31 +00:00
this.formSubmitController.submit();
}
});
2022-03-01 15:24:57 +00:00
}
}
2021-02-26 14:09:13 +00:00
handlePasswordToggle() {
2022-09-16 19:27:10 +00:00
this.passwordVisible = !this.passwordVisible;
2021-02-26 14:09:13 +00:00
}
@watch('value', { waitUntilFirstUpdate: true })
2021-03-06 19:39:48 +00:00
handleValueChange() {
this.input.value = this.value; // force a sync update
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;
2022-06-19 16:22:52 +00:00
const hasClearIcon =
this.clearable && !this.disabled && !this.readonly && (typeof this.value === 'number' || this.value.length > 0);
2022-03-08 22:34:17 +00:00
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,
2022-06-21 15:21:54 +00:00
'input--invalid': this.invalid,
2022-06-23 20:57:30 +00:00
'input--no-spin-buttons': this.noSpinButtons,
2022-08-15 15:02:36 +00:00
'input--is-firefox': isFirefox
2022-03-08 22:34:17 +00:00
})}
>
2022-12-02 22:03:59 +00:00
<slot name="prefix" part="prefix" class="input__prefix"></slot>
2022-03-08 22:34:17 +00:00
<input
part="input"
id="input"
class="input__control"
2022-09-16 19:27:10 +00:00
type=${this.type === 'password' && this.passwordVisible ? 'text' : this.type}
title=${this.title /* An empty title prevents browser validation tooltips from appearing on hover */}
2022-03-08 22:34:17 +00:00
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)}
2022-07-27 20:45:39 +00:00
step=${ifDefined(this.step as number)}
2022-03-08 22:34:17 +00:00
.value=${live(this.value)}
autocapitalize=${ifDefined(this.type === 'password' ? 'off' : this.autocapitalize)}
autocomplete=${ifDefined(this.type === 'password' ? 'off' : this.autocomplete)}
autocorrect=${ifDefined(this.type === 'password' ? 'off' : this.autocorrect)}
2022-03-08 22:34:17 +00:00
?autofocus=${this.autofocus}
2022-12-08 19:42:04 +00:00
spellcheck=${this.spellcheck}
2022-03-08 22:34:17 +00:00
pattern=${ifDefined(this.pattern)}
2022-04-04 13:47:44 +00:00
enterkeyhint=${ifDefined(this.enterkeyhint)}
2022-03-08 22:34:17 +00:00
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}
/>
2022-12-02 22:03:59 +00:00
${
hasClearIcon
? html`
<button
part="clear-button"
class="input__clear"
type="button"
aria-label=${this.localize.term('clearEntry')}
@click=${this.handleClearClick}
tabindex="-1"
>
<slot name="clear-icon">
<sl-icon name="x-circle-fill" library="system"></sl-icon>
</slot>
</button>
`
: ''
}
${
this.passwordToggle && !this.disabled
? html`
<button
part="password-toggle-button"
class="input__password-toggle"
type="button"
aria-label=${this.localize.term(this.passwordVisible ? 'hidePassword' : 'showPassword')}
@click=${this.handlePasswordToggle}
tabindex="-1"
>
${this.passwordVisible
? 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>
`
: ''
}
<slot name="suffix" part="suffix" class="input__suffix"></slot>
2022-03-08 22:34:17 +00:00
</div>
</div>
2022-12-02 22:03:59 +00:00
<slot
name="help-text"
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-12-02 22:03:59 +00:00
${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;
}
}