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

[MS] Added setFocus to Code input #149

Merged
merged 1 commit into from
Oct 1, 2024
Merged
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
24 changes: 15 additions & 9 deletions lib/components/ms-input/MsCodeValidationInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@
</template>

<script setup lang="ts">
import { onMounted, ref, defineEmits } from 'vue';
import { onMounted, ref, defineEmits, defineExpose } from 'vue';
import { IonInput, IonText } from '@ionic/vue';
import { Ref } from 'vue';
const inputs = ref();
const codes = ref<string[]>([]);
const isFinalCodeValid: Ref<undefined | boolean> = ref(undefined);

onMounted(async (): Promise<void> => {
setTimeout(() => {
focusInputElement(getFirstInputElement());
}, 200);
await focusInputElement(getFirstInputElement());
});

const props = defineProps<{
Expand All @@ -60,20 +58,28 @@ const emits = defineEmits<{
(e: 'codeComplete', code: Array<string>): void;
}>();

defineExpose({
setFocus,
});

async function onPaste(event: ClipboardEvent): Promise<void> {
event.preventDefault();
const code = event.clipboardData?.getData('text');
if (code && isValidCode(code)) {
codes.value = code.split('');
focusInputElement(getLastInputElement());
await focusInputElement(getLastInputElement());
checkCode();
}
}

async function setFocus(): Promise<void> {
await focusInputElement(getFirstInputElement());
}

async function onIonInput(event: CustomEvent): Promise<void> {
const input = event.target as HTMLIonInputElement;
if (input !== getLastInputElement()) {
focusInputElement(getNextInputElement(input));
await focusInputElement(getNextInputElement(input));
}

if (input.value === '') {
Expand All @@ -91,7 +97,7 @@ async function onKeyUpBackspace(event: KeyboardEvent): Promise<void> {
} else {
if (input.value === '' && input !== getFirstInputElement()) {
const previousInput = getPreviousInputElement(input);
focusInputElement(previousInput);
await focusInputElement(previousInput);
setInputElementValue(previousInput, '');
}
}
Expand Down Expand Up @@ -188,9 +194,9 @@ function setInputElementValue(input: HTMLIonInputElement | undefined, value: str
codes.value[inputIndex] = value;
}

function focusInputElement(input: HTMLIonInputElement | undefined): void {
async function focusInputElement(input: HTMLIonInputElement | undefined): Promise<void> {
if (input) {
input.setFocus();
await input.setFocus();
}
}
</script>
Expand Down