Skip to content

Commit

Permalink
LinkWithPill.vue, link-with-pill.spec.ts, ProfilePicture.vue, Profile…
Browse files Browse the repository at this point in the history
…Pic.vue, profile-pic.spec.ts, ListSkeleton.story.vue - add onclick event to prevent default behaviour of link and execute a function
  • Loading branch information
DanuAnjana committed Nov 1, 2024
1 parent 15975c3 commit 232237b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 2 deletions.
13 changes: 12 additions & 1 deletion components/src/core/components/CardTable/Cell/LinkWithPill.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<oxd-skeleton width="50%" animate></oxd-skeleton>
</template>
<template v-else>
<a :href="link ? row[link] : '#'" :target="target" :class="linkClasses">
<a :href="link ? row[link] : '#'" :target="target" :class="linkClasses" @click="handleLinkClick(row, $event)">
{{ cell }}
</a>
<div v-if="pillProperty" :class="pillClasses">
Expand Down Expand Up @@ -45,6 +45,13 @@ export default defineComponent({
}
return value;
},
handleLinkClick(row: RowItem, event: MouseEvent) {
const cellConfig = this.header?.cellConfig;
if (cellConfig && typeof cellConfig?.onClick === 'function') {
event.preventDefault();
this.header.cellConfig.onClick(row, event);
}
},
},
props: {
link: {
Expand All @@ -70,6 +77,10 @@ export default defineComponent({
type: Boolean,
default: false,
},
header: {
type: Object,
default: () => ({}),
},
},
computed: {
linkClasses(): object {
Expand Down
12 changes: 12 additions & 0 deletions components/src/core/components/CardTable/Cell/ProfilePicture.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
:link="profilePicture.link"
:imageSrc="profilePicture.src"
:link-mode="profilePicture.target"
:header="profilePicture.header"
:rowItem="profilePicture.rowItem"
v-bind="$attrs"
/>
</template>
Expand Down Expand Up @@ -52,6 +54,14 @@ export default defineComponent({
type: Boolean,
default: false,
},
header: {
type: Object,
default: () => ({}),
},
rowItem: {
type: Object,
default: () => ({}),
},
},
setup(props) {
const imgSrc = ref(null);
Expand Down Expand Up @@ -82,6 +92,8 @@ export default defineComponent({
? props.rowItem[props.link]
: null,
target: props.target ?? TARGET_SELF,
header: props.header,
rowItem: props.rowItem,
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,54 @@ describe('CardTable > Cell > LinkWithPill.vue', () => {
.trim(),
).toEqual('external');
});

it('should trigger handleLinkClick on link click', async () => {
const mockOnClick = jest.fn();
const rowItem = {
url1: 'https://orangehrm.com',
tag: 'external',
};

const wrapper = mount(LinkWithPill, {
global: GLOBAL,
props: {
item: 'This is a link',
target: '_parent',
link: 'url1',
rowItem: rowItem,
header: {
cellConfig: {
onClick: mockOnClick,
},
},
},
});

const link = wrapper.find('a');
await link.trigger('click');

expect(mockOnClick).toHaveBeenCalledWith(rowItem, expect.any(MouseEvent));
});

it('should not prevent default behavior if no onClick handler is present', async () => {
const wrapper = mount(LinkWithPill, {
global: GLOBAL,
props: {
item: 'This is a link',
target: '_blank',
link: 'url1',
rowItem: {
url1: 'https://orangehrm.com',
tag: 'external',
},
},
});

const link = wrapper.find('a');
const event = { preventDefault: jest.fn() };

await link.trigger('click', event);

expect(event.preventDefault).not.toHaveBeenCalled();
});
});
19 changes: 18 additions & 1 deletion components/src/core/components/ProfilePic/ProfilePic.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div :class="classes">
<slot>
<a v-if="link" :href="link" :target="linkMode">
<a v-if="link" :href="link" :target="linkMode" @click="handleLinkClick($event)">
<span
class="img-tag"
:style="`background-image: url(${profileImage})`"
Expand Down Expand Up @@ -55,6 +55,23 @@ export default defineComponent({
return TARGETS.indexOf(value) !== -1;
},
},
header: {
type: Object,
default: () => ({}),
},
rowItem: {
type: Object,
default: () => ({}),
},
},
methods: {
handleLinkClick(event: MouseEvent | KeyboardEvent) {
const cellConfig = this.header?.cellConfig;
if (cellConfig && typeof cellConfig?.onClick === 'function') {
event.preventDefault();
this.header.cellConfig.onClick(this.rowItem, event);
}
},
},
computed: {
classes(): object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,45 @@ describe('ProfilePic.vue', () => {
});
expect(wrapper.html()).toMatchSnapshot();
});

it('should trigger handleLinkClick on link click', async () => {
const mockOnClick = jest.fn();
const rowItem = {
id: 1,
name: 'John Doe',
};

const wrapper = mount(ProfilePic, {
props: {
link: 'https://orangehrm.com',
rowItem: rowItem,
header: {
cellConfig: {
onClick: mockOnClick,
},
},
},
});

const link = wrapper.find('a');
await link.trigger('click');

expect(mockOnClick).toHaveBeenCalledWith(rowItem, expect.any(MouseEvent));
});

it('should not prevent default behavior if no onClick handler is present', async () => {
const wrapper = mount(ProfilePic, {
props: {
link: 'https://orangehrm.com',
},
});

const link = wrapper.find('a');
const event = { preventDefault: jest.fn() };

await link.trigger('click', event);

expect(event.preventDefault).not.toHaveBeenCalled();
});

});
44 changes: 44 additions & 0 deletions storybook/stories/core/components/ListTable/ListSkeleton.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
:skeleton="showSkeleton"
v-model:selected="checkedItems"
></oxd-list-table>
<br />
<p>{{ LinkPhillResult }}</p>
<br/>
<p>{{ profilePicResult }}</p>
</template>

<script>
Expand All @@ -30,6 +34,8 @@ export default {
data() {
return {
LinkPhillResult: '',
profilePicResult: '',
selector: {
style: {flex: 1},
},
Expand All @@ -44,6 +50,23 @@ export default {
iconStyle: 'color: #929baa; justify-content: center;',
cellType: 'oxd-table-cell-profile-pic',
},
{
name: 'profilePic',
style: {
width: '55px',
'justify-content': 'center',
},
iconName: 'oxd-profile-pic',
iconStyle: 'color: #929baa; justify-content: center;',
cellType: 'oxd-table-cell-profile-pic',
cellProps: {
link: "url1",
target: "_blank",
},
cellConfig: {
onClick: this.onClickProfilePic,
},
},
{
name: 'col1',
sortField: 'col1.name',
Expand Down Expand Up @@ -71,6 +94,20 @@ export default {
pillProperty: ['tag', 'name'],
},
},
{
name: 'col2',
title: 'Link with Pill with onClick event',
style: {flex: 1},
cellType: 'oxd-table-cell-link-with-pill',
cellProps: {
link: 'url1',
target: '_blank',
pillProperty: ['tag', 'name'],
},
cellConfig: {
onClick: this.onClickLinkWithPill,
},
},
{
name: 'col4',
title: 'Date',
Expand Down Expand Up @@ -116,6 +153,13 @@ export default {
},
};
},
onClickLinkWithPill(row, event) {
this.LinkPhillResult = 'Link with Pill with onClick function executed: ' + row.col2;
},
onClickProfilePic(row, event) {
console.log('Profile Pic with onClick function executed: ', row.col2);
this.profilePicResult = 'Profile Pic with onClick function executed: ' + row.col2;
},
},
beforeMount() {
Expand Down

0 comments on commit 232237b

Please sign in to comment.