Skip to content

Commit

Permalink
pkp/pkp-lib#10033 Emit click event for DropdownActions component
Browse files Browse the repository at this point in the history
  • Loading branch information
blesildaramirez committed Jun 26, 2024
1 parent a868ab4 commit 3a3f88f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/components/DropdownActions/DropdownActions.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,47 @@ const downloadActions = [
];

export const Default = {
render: (args) => ({
components: {DropdownActions},
setup() {
function authorPdf() {
console.log('authorPdf clicked');
}

function authorXml() {
console.log('authorXml clicked');
}

function editorPdf() {
console.log('editorPdf clicked');
}

function editorXml() {
console.log('editorXml clicked');
}

function handleAction(name) {
switch (name) {
case 'authorPdf':
authorPdf();
break;
case 'authorXml':
authorXml();
break;
case 'editorPdf':
editorPdf();
break;
case 'editorXml':
editorXml();
break;
default:
console.error(`No handler for action: ${name}`);
}
}
return {args, handleAction};
},
template: '<DropdownActions v-bind="args" @action="handleAction" />',
}),
args: {
actions: downloadActions,
label: 'Download Review Form',
Expand Down Expand Up @@ -110,6 +151,7 @@ export const EllipsisMenu = {
};

export const RightAlignedMenu = {
...Default,
args: {
actions: downloadActions,
label: 'Right Aligned Menu',
Expand Down
23 changes: 21 additions & 2 deletions src/components/DropdownActions/DropdownActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
: 'ltr:right-0 ltr:origin-top-right rtl:left-0 rtl:origin-top-right'
"
>
<MenuItem v-for="(action, i) in actions" :key="i" v-slot="{active}">
<MenuItem
v-for="(action, i) in actions"
:key="i"
v-slot="{active, close}"
>
<div class="min-w-[96px]">
<PkpButton
v-if="isValidAction(action)"
Expand All @@ -48,7 +52,12 @@
:class="i !== actions.length - 1 ? 'border-b' : ''"
size-variant="fullWidth"
class="border-light"
@click="action.name"
@click="
() => {
emitAction(action);
close();
}
"
>
{{ action.label }}
</PkpButton>
Expand Down Expand Up @@ -100,6 +109,16 @@ defineProps({
},
});
const emit = defineEmits(['action']);
const emitAction = (action) => {
if (action.url) {
window.location.href = action.url;
} else if (action.name) {
emit('action', action.name);
}
};
const isValidAction = (action) => {
return action?.label && (action?.url || action?.name);
};
Expand Down

0 comments on commit 3a3f88f

Please sign in to comment.