Skip to content

Commit

Permalink
a bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Seokky committed Apr 22, 2020
1 parent 753dd62 commit 9cea0d0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/plugin/Component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@

<script lang="ts">
import Vue from 'vue';
import props from './props';
import { TLetter } from './types/Letter';
import { TInputType } from './types/InputType';
import { TLettersInputTypes } from './types/LettersInputTypes';
import {
BASE_REF_NAME, LETTER_REGEXP, DEFAULT_INPUT_TYPE, SECURE_INPUT_TYPE,
BASE_REF_NAME, LETTER_REGEXP,
DEFAULT_INPUT_TYPE,
SECURE_INPUT_TYPE,
} from './constants';
export default Vue.extend({
props: {
value: { type: String, required: true },
length: { type: Number, default: 4 },
autofocus: { type: Boolean, default: true },
secure: { type: Boolean, default: false },
characterPreview: { type: Boolean, default: true },
previewDuration: { type: Number, default: 300 },
},
props,
data: () => ({
baseRefName: BASE_REF_NAME,
Expand All @@ -50,8 +48,8 @@ export default Vue.extend({
},
watch: {
value(val) {
this.handleParentValue();
value() {
this.setParentValue();
},
length: {
Expand All @@ -72,7 +70,7 @@ export default Vue.extend({
mounted() {
this.init();
this.handleParentValue();
this.setParentValue();
if (this.autofocus) {
this.focusLetterByIndex(0);
Expand All @@ -93,24 +91,30 @@ export default Vue.extend({
}
},
handleParentValue() {
if (!this.value) return;
setParentValue() {
if (!this.value) {
return;
}
if (this.value.length !== this.length) {
return;
}
const letters = this.value.split('');
letters.forEach((letter: string, idx: number) => {
this.letters[idx].value = letter || '';
});
this.value
.split('') // getting letters
.forEach((letter: string, idx: number) => {
this.letters[idx].value = letter || '';
});
},
isTheLetterValid(letter: string): boolean {
if (letter === '') return true;
if (letter === '') {
return true;
}
if (!letter) return false;
if (!letter) {
return false;
}
return !!letter.match(LETTER_REGEXP);
},
Expand All @@ -134,6 +138,7 @@ export default Vue.extend({
this.previewDuration,
);
}
this.setFocusedLetterIndex(this.focusedLetterIdx + 1);
}
},
Expand Down
26 changes: 26 additions & 0 deletions src/plugin/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
value: {
type: String,
required: true,
},
length: {
type: Number,
default: 4,
},
autofocus: {
type: Boolean,
default: true,
},
secure: {
type: Boolean,
default: false,
},
characterPreview: {
type: Boolean,
default: true,
},
previewDuration: {
type: Number,
default: 300,
},
};

0 comments on commit 9cea0d0

Please sign in to comment.