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

feat: add disabled attribute handling to link #2101

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
19 changes: 15 additions & 4 deletions packages/vue3/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface InertiaLinkProps {
async?: boolean
prefetch?: boolean | LinkPrefetchOption | LinkPrefetchOption[]
cacheFor?: CacheForOption | CacheForOption[]
disabled?: boolean
}

type InertiaLink = DefineComponent<InertiaLinkProps>
Expand Down Expand Up @@ -132,6 +133,10 @@ const Link: InertiaLink = defineComponent({
type: Function as PropType<(cancelToken: import('axios').CancelTokenSource) => void>,
default: () => {},
},
disabled: {
type: Boolean,
default: false,
},
},
setup(props, { slots, attrs }) {
const inFlightCount = ref(0)
Expand Down Expand Up @@ -185,7 +190,7 @@ const Link: InertiaLink = defineComponent({

const elProps = {
a: { href },
button: { type: 'button' },
button: { type: 'button', disabled: props.disabled },
}

const baseParams = {
Expand Down Expand Up @@ -219,14 +224,18 @@ const Link: InertiaLink = defineComponent({
}

const prefetch = () => {
router.prefetch(href, baseParams, { cacheFor: cacheForValue })
if (!props.disabled) {
router.prefetch(href, baseParams, { cacheFor: cacheForValue })
}
}

const regularEvents = {
onClick: (event) => {
if (shouldIntercept(event)) {
event.preventDefault()
router.visit(href, visitParams)
if (!props.disabled) {
router.visit(href, visitParams)
}
}
},
}
Expand All @@ -252,7 +261,9 @@ const Link: InertiaLink = defineComponent({
},
onMouseup: (event) => {
event.preventDefault()
router.visit(href, visitParams)
if (!props.disabled) {
router.visit(href, visitParams)
}
},
onClick: (event) => {
if (shouldIntercept(event)) {
Expand Down
19 changes: 19 additions & 0 deletions packages/vue3/test-app/Pages/Links/Disabled.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script setup lang="ts">
import { Link } from '@inertiajs/vue3'
</script>

<template>
<div>
<span class="text">This is the links page that demonstrates disabled attribute</span>

<Link href="/#" class="disabled-default" disabled>Disabled Default</Link>
<Link href="/#" class="disabled" :disabled="true">Disabled</Link>
<Link href="/links/disabled" class="enabled" :disabled="false">Enabled</Link>
<Link href="/links/disabled" class="enabled-default">Enabled Default</Link>

<Link as='button' href="/#" class="disabled-default" disabled>Disabled Default Button</Link>
<Link as='button' href="/#" class="disabled" :disabled="true">Disabled Button</Link>
<Link as='button' href="/links/disabled" class="enabled" :disabled="false">Enabled Button</Link>
<Link as='button' href="/links/disabled" class="enabled-default">Enabled Default Button</Link>
</div>
</template>
2 changes: 2 additions & 0 deletions tests/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ app.get('/links/headers/version', (req, res) =>
)
app.get('/links/data-loading', (req, res) => inertia.render(req, res, { component: 'Links/DataLoading' }))

app.get('/links/disabled', (req, res) => inertia.render(req, res, { component: 'Links/Disabled' }))

app.get('/visits/partial-reloads', (req, res) =>
inertia.render(req, res, {
component: 'Visits/PartialReloads',
Expand Down
66 changes: 66 additions & 0 deletions tests/links.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,69 @@ test.describe('data-loading attribute', () => {
await expect(link2).not.toHaveAttribute('data-loading')
})
})

test.describe('disabled attribute', () => {
test('disables the link when the disabled attribute is set to true', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('link', { name: 'Disabled', exact: true })
await expect(link).not.toHaveAttribute('disabled')
await link.click()
await expect(page).toHaveURL('/links/disabled')
})

test('disables the link when the disabled attribute exists', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('link', { name: 'Disabled Default', exact: true })
await expect(link).not.toHaveAttribute('disabled')
await link.click()
await expect(page).toHaveURL('/links/disabled')
})

test('does not disable the link when the disabled attribute is set to false', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('link', { name: 'Enabled', exact: true })
await expect(link).not.toHaveAttribute('disabled')
await link.click()
await expect(page).toHaveURL('/links/disabled')
})

test('does not disable the link when the disabled attribute not exists', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('link', { name: 'Enabled Default', exact: true })
await expect(link).not.toHaveAttribute('disabled')
await link.click()
await expect(page).toHaveURL('/links/disabled')
})

test('disables the link (as=button) when the disabled attribute is set to true', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('button', { name: 'Disabled Button', exact: true })
await expect(link).toBeDisabled()
await expect(link).toHaveAttribute('disabled')
})

test('disables the link (as=button) when the disabled attribute is set', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('button', { name: 'Disabled Default Button', exact: true })
await expect(link).toBeDisabled()
await expect(link).toHaveAttribute('disabled')
})

test('does not disable the link (as=button) when the disabled attribute is set to false', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('button', { name: 'Enabled Button', exact: true })
await expect(link).not.toBeDisabled()
await expect(link).not.toHaveAttribute('disabled')
await link.click()
await expect(page).toHaveURL('/links/disabled')
})

test('does not disable the link (as=button) when the disabled attribute is not set', async ({ page }) => {
await page.goto('/links/disabled')
const link = await page.getByRole('button', { name: 'Enabled Default Button', exact: true })
await expect(link).not.toBeDisabled()
await expect(link).not.toHaveAttribute('disabled')
await link.click()
await expect(page).toHaveURL('/links/disabled')
})
})
Loading