2021-05-22 12:32:32 +03:00
|
|
|
<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 = '';
|
|
|
|
|
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
|
|
|
});
|
2021-05-22 12:32:32 +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-11-28 11:23:46 +01:00
|
|
|
<label for="text-field" class="m-0 font-light">
|
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}
|
2021-05-22 12:32:32 +03:00
|
|
|
<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}
|
2021-05-22 12:32:32 +03:00
|
|
|
{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}
|
2021-05-22 12:32:32 +03:00
|
|
|
</label>
|
2022-11-28 11:23:46 +01:00
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
.password-eye {
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 33px;
|
|
|
|
|
right: 6px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|