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

Disabling keypress action on primary button loading state #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions src/components/button/Primary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
:disabled="disabled"
:tabindex="loading ? '-1' : '0'"
role="button"
@keydown="handleKeypress"
>
<span
class="inline-flex items-center justify-center whitespace-nowrap"
Expand Down Expand Up @@ -85,7 +86,7 @@ interface Props {
shortcut?: string[]
}

withDefaults(defineProps<Props>(), {
const props = withDefaults(defineProps<Props>(), {
to: "",
exact: true,
blank: false,
Expand All @@ -103,4 +104,11 @@ withDefaults(defineProps<Props>(), {
outline: false,
shortcut: () => [],
})
</script>

const handleKeypress = (event: KeyboardEvent) => {
// Check if the button is in a loading state
if (props.loading && (event.key === "Enter" || event.key === " ")){
event.preventDefault();
}
};
</script>
2 changes: 1 addition & 1 deletion src/stories/Button.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

<script setup lang="ts">
import { HoppButtonPrimary, HoppButtonSecondary } from "../components/button"
</script>
</script>
70 changes: 70 additions & 0 deletions src/stories/Keypress.story.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<template>
<Story title="Button">
<Variant title="Primary">
<HoppButtonPrimary
ref="primaryButton"
label="Button"
@click="handleClick('Primary button clicked')"
/>
</Variant>
<Variant title="Secondary">
<HoppButtonSecondary
label="Button"
:loading="isLoading"
@click="handleClick('Secondary button clicked')"
/>
</Variant>
</Story>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { HoppButtonPrimary, HoppButtonSecondary } from "../components/button"

const primaryButton = ref<InstanceType<typeof HoppButtonPrimary> | null>(null);
const isLoading = ref(false);

// Function to handle click event, log message, and trigger an Enter keypress event
function handleClick(message: string) {
console.log(message)

// Set loading state to true
isLoading.value = true;

// Simulate loading by resetting the loading state after 3 seconds
setTimeout(() => {
isLoading.value = false;
}, 3000); // 3 seconds delay
}

function handleKeyPress(event: KeyboardEvent) {

const enterEvent = new KeyboardEvent('keydown', {
key: ' ',
code: ' ',
keyCode: 32,
charCode: 32,
which: 32,
bubbles: true,
});

// Dispatch the Enter keypress event on the HoppButtonPrimary element
if (primaryButton.value) {
primaryButton.value.$el.dispatchEvent(enterEvent); // Trigger the event on the button
}
}

onMounted(() => {
document.addEventListener('keydown', (event: KeyboardEvent) => {
if (event.key === 'Enter') {
handleKeyPress(event);
}
});
})

onBeforeUnmount(() => {
document.removeEventListener('keydown', handleKeyPress)
})

</script>