|
| 1 | +<!-- https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield --> |
| 2 | +<script> |
| 3 | +import { MDCTextField } from '@material/textfield' |
| 4 | +import { generateRandomID } from '../../../random' |
| 5 | +import { onMount } from 'svelte' |
| 6 | +
|
| 7 | +export let label = '' |
| 8 | +export let value = '' |
| 9 | +export let placeholder = '' |
| 10 | +export let maxlength = undefined |
| 11 | +export let autofocus = false |
| 12 | +export let disabled = false |
| 13 | +export let required = false |
| 14 | +
|
| 15 | +const labelID = generateRandomID('text-label-') |
| 16 | +
|
| 17 | +let element = {} |
| 18 | +let mdcTextField = {} |
| 19 | +
|
| 20 | +$: mdcTextField.value = value |
| 21 | +
|
| 22 | +onMount(() => { |
| 23 | + mdcTextField = new MDCTextField(element) |
| 24 | + return () => mdcTextField.destroy() |
| 25 | +}) |
| 26 | +
|
| 27 | +const focus = (node) => autofocus && node.focus() |
| 28 | +</script> |
| 29 | + |
| 30 | +<style> |
| 31 | +.material-icons { |
| 32 | + color: rgb(133, 140, 148); |
| 33 | + position: relative; |
| 34 | + top: 0.4rem; |
| 35 | + right: 0.6rem; |
| 36 | +} |
| 37 | +.required { |
| 38 | + color: var(--mdc-required-input, var(--mdc-theme-status-error)); |
| 39 | + font-size: small; |
| 40 | + margin-left: 1rem; |
| 41 | + margin-top: 0.2rem; |
| 42 | +} |
| 43 | +.label-margin { |
| 44 | + margin-left: 1.1rem; |
| 45 | +} |
| 46 | +.mdc-text-field--label-floating .mdc-floating-label { |
| 47 | + margin-left: 0; |
| 48 | +} |
| 49 | +</style> |
| 50 | + |
| 51 | +<label |
| 52 | + class="mdc-text-field mdc-text-field--outlined {$$props.class} textfield-radius" |
| 53 | + class:mdc-text-field--no-label={!label} |
| 54 | + class:mdc-text-field--disabled={disabled} |
| 55 | + bind:this={element} |
| 56 | +> |
| 57 | + <i class="material-icons" aria-hidden="true">attach_money</i> |
| 58 | + <input |
| 59 | + step="0.01" |
| 60 | + type="number" |
| 61 | + min="0" |
| 62 | + class="mdc-text-field__input" |
| 63 | + aria-labelledby={labelID} |
| 64 | + bind:value |
| 65 | + use:focus |
| 66 | + on:blur |
| 67 | + on:keydown |
| 68 | + on:keypress |
| 69 | + on:keyup |
| 70 | + {required} |
| 71 | + {maxlength} |
| 72 | + {disabled} |
| 73 | + {placeholder} |
| 74 | + /> |
| 75 | + <span class="mdc-notched-outline"> |
| 76 | + <span class="mdc-notched-outline__leading" /> |
| 77 | + {#if label} |
| 78 | + <span class="mdc-notched-outline__notch"> |
| 79 | + <span class="mdc-floating-label label-margin" id={labelID}> |
| 80 | + {label} |
| 81 | + </span> |
| 82 | + </span> |
| 83 | + {/if} |
| 84 | + <span class="mdc-notched-outline__trailing" /> |
| 85 | + </span> |
| 86 | +</label> |
| 87 | +{#if required && !value} |
| 88 | + <div class="required">*Required</div> |
| 89 | +{/if} |
0 commit comments