Files
classroomio/src/components/Form/TextField.svelte
T

99 lines
2.2 KiB
Svelte
Raw Normal View History

<script>
2021-07-31 23:06:14 +03:00
import { onMount } from 'svelte';
2022-11-28 11:23:46 +01:00
import IconButton from '../IconButton/index.svelte';
import PasswordEye from '../Icons/PasswordEye.svelte';
2021-08-18 19:47:02 +03:00
export let label = '';
export let placeholder = '';
export let value = '';
export let onKeyDown = () => {};
2021-07-26 02:31:00 +03:00
export let className = '';
export let inputClassName = '';
2022-12-03 22:45:01 +01:00
export let labelClassName = '';
2021-07-26 02:31:00 +03:00
export let type = 'text';
2022-11-28 11:23:46 +01:00
export let isPassword = false;
2021-07-26 02:31:00 +03:00
export let autoFocus = false;
2021-07-26 07:49:05 +03:00
export let isRequired = false;
2021-07-31 23:06:14 +03:00
export let isDisabled = false;
2021-08-18 19:47:02 +03:00
export let errorMessage = '';
export let helperMessage = '';
export let autoComplete = true;
export let onChange = () => {}; // This is to know if element is 'dirty'
2021-07-26 07:49:05 +03:00
let ref;
2022-11-28 11:23:46 +01:00
let fieldNode;
2021-07-26 02:31:00 +03:00
function typeAction(node) {
node.type = type;
2021-07-26 07:49:05 +03:00
if (isRequired) {
node.required = '';
}
2022-11-28 11:23:46 +01:00
fieldNode = node;
}
function handlePasswordEye() {
type = type === 'password' ? 'text' : 'password';
typeAction(fieldNode);
2021-07-26 02:31:00 +03:00
}
2021-07-31 23:06:14 +03:00
onMount(() => {
2021-07-26 02:31:00 +03:00
if (autoFocus) {
ref.focus();
}
2022-11-28 11:23:46 +01:00
if (type === 'password') {
isPassword = true;
}
2021-07-26 02:31:00 +03:00
});
</script>
2022-11-28 11:23:46 +01:00
<label class="block relative {className}">
2021-07-31 23:06:14 +03:00
{#if label}
2022-12-03 22:45:01 +01:00
<label for="text-field" class="m-0 font-light {labelClassName}">
2021-08-18 19:47:02 +03:00
{label}
{#if isRequired}
<span class="text-red-700">*</span>
{/if}
2021-07-31 23:06:14 +03:00
</label>
{/if}
<input
2021-07-26 02:31:00 +03:00
use:typeAction
2022-11-28 11:23:46 +01:00
class="form-input p-3 mt-1 block w-full border-none bg-gray-100 {inputClassName} {errorMessage
2021-07-31 23:06:14 +03:00
? 'border-red-500'
2022-11-28 11:23:46 +01:00
: ''}"
2021-05-29 22:24:14 +03:00
on:keydown={onKeyDown}
{placeholder}
2021-05-30 14:17:08 +03:00
bind:value
2021-07-26 02:31:00 +03:00
bind:this={ref}
2021-07-31 23:06:14 +03:00
required={isRequired}
disabled={isDisabled}
2021-08-18 19:47:02 +03:00
autocomplete={autoComplete ? 'on' : 'off'}
on:blur={onChange}
2021-05-30 14:17:08 +03:00
/>
2022-11-28 11:23:46 +01:00
{#if isPassword}
<span class="password-eye">
<IconButton value="write-code" onClick={handlePasswordEye}>
<PasswordEye isClosed={type === 'password' ? true : false} />
</IconButton>
</span>
{/if}
2021-07-31 23:06:14 +03:00
{#if errorMessage}
<p class="text-sm text-red-500">{errorMessage}</p>
2021-08-18 19:47:02 +03:00
{:else if helperMessage}
<p class="text-sm">
{helperMessage}
</p>
2021-07-31 23:06:14 +03:00
{/if}
</label>
2022-11-28 11:23:46 +01:00
<style>
.password-eye {
position: absolute;
top: 33px;
right: 6px;
}
</style>