Skip to content
Open
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
17 changes: 14 additions & 3 deletions templates/website/src/fields/slug/SlugComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
import React, { useCallback, useEffect } from 'react'
import { TextFieldClientProps } from 'payload'

import { useField, Button, TextInput, FieldLabel, useFormFields, useForm } from '@payloadcms/ui'
import {
useField,
Button,
TextInput,
FieldLabel,
useFormFields,
useForm,
useDocumentInfo,
} from '@payloadcms/ui'

import { formatSlug } from './formatSlug'
import './index.scss'
Expand All @@ -29,6 +37,9 @@ export const SlugComponent: React.FC<SlugComponentProps> = ({

const { dispatchFields } = useForm()

// Check if the document is already published
const { hasPublishedDoc } = useDocumentInfo()

// The value of the checkbox
// We're using separate useFormFields to minimise re-renders
const checkboxValue = useFormFields(([fields]) => {
Expand All @@ -41,7 +52,7 @@ export const SlugComponent: React.FC<SlugComponentProps> = ({
})

useEffect(() => {
if (checkboxValue) {
if (checkboxValue && !hasPublishedDoc) {
if (targetFieldValue) {
const formattedSlug = formatSlug(targetFieldValue)

Expand All @@ -50,7 +61,7 @@ export const SlugComponent: React.FC<SlugComponentProps> = ({
if (value !== '') setValue('')
}
}
}, [targetFieldValue, checkboxValue, setValue, value])
}, [targetFieldValue, checkboxValue, setValue, value, hasPublishedDoc])

const handleLock = useCallback(
(e: React.MouseEvent<Element>) => {
Expand Down
Loading