Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Textfield component #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions src/components/Textfield/Textfield.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script setup lang="ts">
import {
computed,
reactive,
ref,
watch,
type CSSProperties,
type HTMLAttributes,
type InputHTMLAttributes,
Expand All @@ -12,7 +10,6 @@ import {
import './textfield.css';
import Icon from '../Icon/Icon.vue';
import Label from '../Label/Label.vue';
// import { vFocus } from '../../directives';
export interface Props {
/**
* id of the textfield
Expand Down Expand Up @@ -159,9 +156,6 @@ const emit = defineEmits([
'click:icon',
'click:clear',
]);
const state = reactive({
value: '',
});
const typeOfInputRef = ref(props.type);
const prependIconsOfType = {
password: 'lock_outline',
Expand All @@ -172,7 +166,7 @@ const prependIconsOfType = {
text: '',
};
const isFocused = ref(false);
const isFilled = computed(() => !!state.value);
const isFilled = computed(() => !!props.modelValue);
const classes = computed(() => {
const { disabled, loading, clearable, errorMsg } = props;
return {
Expand Down Expand Up @@ -200,7 +194,7 @@ const appendIconClasses = computed(() => {
};
});
const hasValue = computed(() => {
return state.value.length > 0;
return props.modelValue.length > 0;
});
const hasErrorMsg = computed(() => {
return props.errorMsg?.length;
Expand Down Expand Up @@ -229,7 +223,7 @@ const appendIconName = computed(() => {
const onFocus = () => {
isFocused.value = true;
emit('focus', {
value: state.value,
value: props.modelValue,
});
};
/**
Expand All @@ -238,24 +232,24 @@ const onFocus = () => {
const onBlur = () => {
isFocused.value = false;
emit('blur', {
value: state.value,
value: props.modelValue,
});
};
/**
* @description - Emit click event with value of append icon
* @returns {void}
*/
const clickIcon = () => {
if (hasClear.value) {
state.value = '';
if (hasClear.value && props.type !== 'password') {
emit('update:modelValue', '');
inputRef.value?.focus();
emit('click:clear', {
value: state.value,
value: props.modelValue,
});
return;
}
emit('click:icon', {
value: state.value,
value: props.modelValue,
});
setPassType();
};
Expand All @@ -274,24 +268,8 @@ const setPassType = () => {
*/
const onInput = (e: Event) => {
const target = e.target as HTMLInputElement;
state.value = target.value;
emit('update:modelValue', target.value);
};

watch(
() => props.modelValue,
(value) => {
state.value = value;
},
{
immediate: true,
}
);
watch(
() => state.value,
(value) => {
emit('update:modelValue', value);
}
);
</script>
<template>
<fieldset>
Expand Down Expand Up @@ -322,7 +300,7 @@ watch(
:disabled="props.disabled"
:placeholder="props.placeholder"
:type="typeOfInputRef"
:value="state.value"
:value="props.modelValue"
@blur="onBlur"
@focus="onFocus"
@input="onInput"
Expand Down