|
| 1 | +<!-- https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield --> |
| 2 | +<script> |
| 3 | +/** A Svelte component that represents a text input for Number values. */ |
| 4 | +import { addOrRemoveInvalidClass, getDecimalPlacesLength } from './helpers' |
| 5 | +import { generateRandomID } from '../../../random' |
| 6 | +import { MDCTextField } from '@material/textfield' |
| 7 | +import { afterUpdate, onMount } from 'svelte' |
| 8 | +
|
| 9 | +/** @type {string} The label for the input. */ |
| 10 | +export let label = '' |
| 11 | +/** @type {number} The value of the input. */ |
| 12 | +export let value = 0 |
| 13 | +/** @type {number} The step value for the input. */ |
| 14 | +export let step = 1 |
| 15 | +/** @type {string} The placeholder for the input. */ |
| 16 | +export let placeholder = '' |
| 17 | +/** @type {string} The name of the input. */ |
| 18 | +export let name = '' |
| 19 | +/** @type {number} The maximum value allowed for the input. */ |
| 20 | +export let maxValue = undefined |
| 21 | +/** @type {number} The minimum value allowed for the input. */ |
| 22 | +export let minValue = undefined |
| 23 | +/** @type {boolean} If true, the input will be focused on mount. */ |
| 24 | +export let autofocus = false |
| 25 | +/** @type {boolean} If true, the input will be disabled. */ |
| 26 | +export let disabled = false |
| 27 | +/** @type {boolean} If true, the input will be required. */ |
| 28 | +export let required = false |
| 29 | +/** @type {string} The description to display below the input. */ |
| 30 | +export let description = '' |
| 31 | +/** @type {boolean} lets the component know to use error class. */ |
| 32 | +export let showError = false |
| 33 | +/** @type {boolean} lets the component know to use warn class. */ |
| 34 | +export let showWarn = false |
| 35 | +
|
| 36 | +const labelID = generateRandomID('text-label-') |
| 37 | +
|
| 38 | +let maxlength = 524288 /* default */ |
| 39 | +let element = {} |
| 40 | +let mdcTextField = {} |
| 41 | +let width = '' |
| 42 | +let hasFocused = false |
| 43 | +let hasBlurred = false |
| 44 | +
|
| 45 | +$: valueLength = value?.toString()?.length |
| 46 | +$: hasExceededMaxLength = maxlength && valueLength > maxlength |
| 47 | +$: hasExceededMaxValue = maxValue && internalValue > maxValue |
| 48 | +$: isLowerThanMinValue = minValue && internalValue < minValue |
| 49 | +$: showErrorIcon = hasExceededMaxValue || isLowerThanMinValue || hasExceededMaxLength || valueNotDivisibleByStep |
| 50 | +$: error = showErrorIcon || (hasFocused && hasBlurred && required && !internalValue) |
| 51 | +$: showCounter = maxlength && valueLength / maxlength > 0.85 |
| 52 | +$: valueHasTooManyDecPlaces = getDecimalPlacesLength(internalValue) > getDecimalPlacesLength(step) |
| 53 | +$: valueNotDivisibleByStep = |
| 54 | + (internalValue && (internalValue / Number(step)).toFixed(2) % 1 !== 0) || valueHasTooManyDecPlaces |
| 55 | +$: internalValue = Number(value) || 0 |
| 56 | +$: warn = showWarn |
| 57 | +$: value, addOrRemoveInvalidClass(error, element) |
| 58 | +$: addOrRemoveInvalidClass(showError || showWarn, element) |
| 59 | +
|
| 60 | +onMount(() => { |
| 61 | + mdcTextField = new MDCTextField(element) |
| 62 | + return () => mdcTextField.destroy() |
| 63 | +}) |
| 64 | +
|
| 65 | +afterUpdate(() => (width = `${element?.offsetWidth}px`)) |
| 66 | +
|
| 67 | +const focus = (node) => autofocus && node.focus() |
| 68 | +</script> |
| 69 | +
|
| 70 | +<label |
| 71 | + class="mdc-text-field mdc-text-field--outlined mdc-text-field--with-leading-icon {$$props.class || |
| 72 | + ''} textfield-radius" |
| 73 | + class:mdc-text-field--no-label={!label} |
| 74 | + class:mdc-text-field--disabled={disabled} |
| 75 | + class:warn |
| 76 | + class:showError |
| 77 | + bind:this={element} |
| 78 | +> |
| 79 | + <span class="mdc-notched-outline"> |
| 80 | + <span class="mdc-notched-outline__leading" /> |
| 81 | + {#if label} |
| 82 | + <span class="mdc-notched-outline__notch"> |
| 83 | + <span class="mdc-floating-label" class:error id={labelID}> |
| 84 | + {label} |
| 85 | + </span> |
| 86 | + </span> |
| 87 | + {/if} |
| 88 | + <span class="mdc-notched-outline__trailing" /> |
| 89 | + </span> |
| 90 | + <i class="material-icons mdc-text-field__icon mdc-text-field__icon--leading" class:error aria-hidden="true"> </i> |
| 91 | + <input |
| 92 | + data-1p-ignore |
| 93 | + {step} |
| 94 | + type="number" |
| 95 | + min={minValue} |
| 96 | + max={maxValue} |
| 97 | + class="mdc-text-field__input" |
| 98 | + aria-labelledby={labelID} |
| 99 | + aria-controls="{labelID}-helper-id" |
| 100 | + aria-describedby="{labelID}-helper-id" |
| 101 | + bind:value |
| 102 | + use:focus |
| 103 | + on:focus={() => (hasFocused = true)} |
| 104 | + on:blur |
| 105 | + on:blur={() => (hasBlurred = true)} |
| 106 | + on:keydown |
| 107 | + on:keypress |
| 108 | + on:keyup |
| 109 | + {disabled} |
| 110 | + {maxlength} |
| 111 | + {name} |
| 112 | + {placeholder} |
| 113 | + {required} |
| 114 | + /> |
| 115 | + {#if showErrorIcon} |
| 116 | + <i class="material-icons mdc-text-field__icon mdc-text-field__icon--trailing error" aria-hidden="true"> error</i> |
| 117 | + {/if} |
| 118 | +</label> |
| 119 | +<div class="mdc-text-field-helper-line" style="width: {width};"> |
| 120 | + <div |
| 121 | + class="mdc-text-field-helper-text |
| 122 | + mdc-text-field-helper-text--{error ? 'validation-msg' : 'persistent'}" |
| 123 | + id="{labelID}-helper-id" |
| 124 | + aria-hidden="true" |
| 125 | + > |
| 126 | + {#if !error && description} |
| 127 | + {description} |
| 128 | + {:else if required && !internalValue} |
| 129 | + ✴Required |
| 130 | + {:else if hasExceededMaxValue} |
| 131 | + Maximum value allowed is {maxValue} |
| 132 | + {:else if isLowerThanMinValue} |
| 133 | + Minimun value allowed is ({minValue}) |
| 134 | + {:else if valueNotDivisibleByStep} |
| 135 | + {internalValue} is not divisible by {step} |
| 136 | + {:else if hasExceededMaxLength} |
| 137 | + Maximum {maxlength} characters |
| 138 | + {/if} |
| 139 | + </div> |
| 140 | + {#if showCounter} |
| 141 | + <div class="mdc-text-field-character-counter" class:error> |
| 142 | + {valueLength} / {maxlength} |
| 143 | + </div> |
| 144 | + {/if} |
| 145 | +</div> |
0 commit comments