Skip to content

Commit cae41c4

Browse files
PatrikKozakkendelljoseph
authored andcommitted
feat: adds showSaveDraftButton option to show draft button with autosave enabled (#12150)
This adds a new `showSaveDraftButton` option to the `versions.drafts.autosave` config for collections and globals. By default, the "Save as draft" button is hidden when autosave is enabled. This new option allows the button to remain visible for manual saves while autosave is active. Also updates the admin UI logic to conditionally render the button when this flag is set, and updates the documentation with an example usage.
1 parent 324f553 commit cae41c4

File tree

9 files changed

+282
-98
lines changed

9 files changed

+282
-98
lines changed

docs/versions/autosave.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Collections and Globals both support the same options for configuring autosave.
2222
| Drafts Autosave Options | Description |
2323
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
2424
| `interval` | Define an `interval` in milliseconds to automatically save progress while documents are edited. Document updates are "debounced" at this interval. Defaults to `800`. |
25+
| `showSaveDraftButton` | Set this to `true` to show the "Save as draft" button even while autosave is enabled. Defaults to `false`. |
2526

2627
**Example config with versions, drafts, and autosave enabled:**
2728

@@ -50,9 +51,13 @@ export const Pages: CollectionConfig = {
5051
drafts: {
5152
autosave: true,
5253

53-
// Alternatively, you can specify an `interval`:
54+
// Alternatively, you can specify an object to customize autosave:
5455
// autosave: {
56+
// Define how often the document should be autosaved (in milliseconds)
5557
// interval: 1500,
58+
//
59+
// Show the "Save as draft" button even while autosave is enabled
60+
// showSaveDraftButton: true,
5661
// },
5762
},
5863
},

packages/payload/src/versions/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ export type Autosave = {
66
* @default 800
77
*/
88
interval?: number
9+
/**
10+
* When set to `true`, the "Save as draft" button will be displayed even while autosave is enabled.
11+
* By default, this button is hidden to avoid redundancy with autosave behavior.
12+
*
13+
* @default false
14+
*/
15+
showSaveDraftButton?: boolean
916
}
1017

1118
export type SchedulePublish = {

packages/ui/src/elements/DocumentControls/index.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,23 @@ export const DocumentControls: React.FC<{
133133
const unsavedDraftWithValidations =
134134
!id && collectionConfig?.versions?.drafts && collectionConfig.versions?.drafts.validate
135135

136+
const collectionConfigDrafts = collectionConfig?.versions?.drafts
137+
const globalConfigDrafts = globalConfig?.versions?.drafts
138+
136139
const autosaveEnabled =
137-
(collectionConfig?.versions?.drafts && collectionConfig?.versions?.drafts?.autosave) ||
138-
(globalConfig?.versions?.drafts && globalConfig?.versions?.drafts?.autosave)
140+
(collectionConfigDrafts && collectionConfigDrafts?.autosave) ||
141+
(globalConfigDrafts && globalConfigDrafts?.autosave)
142+
143+
const collectionAutosaveEnabled = collectionConfigDrafts && collectionConfigDrafts?.autosave
144+
const globalAutosaveEnabled = globalConfigDrafts && globalConfigDrafts?.autosave
145+
146+
const showSaveDraftButton =
147+
(collectionAutosaveEnabled &&
148+
collectionConfigDrafts.autosave !== false &&
149+
collectionConfigDrafts.autosave.showSaveDraftButton === true) ||
150+
(globalAutosaveEnabled &&
151+
globalConfigDrafts.autosave !== false &&
152+
globalConfigDrafts.autosave.showSaveDraftButton === true)
139153

140154
const showCopyToLocale = localization && !collectionConfig?.admin?.disableCopyToLocale
141155

@@ -218,7 +232,9 @@ export const DocumentControls: React.FC<{
218232
<Fragment>
219233
{collectionConfig?.versions?.drafts || globalConfig?.versions?.drafts ? (
220234
<Fragment>
221-
{(unsavedDraftWithValidations || !autosaveEnabled) && (
235+
{(unsavedDraftWithValidations ||
236+
!autosaveEnabled ||
237+
(autosaveEnabled && showSaveDraftButton)) && (
222238
<RenderCustomComponent
223239
CustomComponent={CustomSaveDraftButton}
224240
Fallback={<SaveDraftButton />}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { CollectionConfig } from 'payload'
2+
3+
import { autosaveWithDraftButtonSlug } from '../slugs.js'
4+
5+
const AutosaveWithDraftButtonPosts: CollectionConfig = {
6+
slug: autosaveWithDraftButtonSlug,
7+
labels: {
8+
singular: 'Autosave with Draft Button Post',
9+
plural: 'Autosave with Draft Button Posts',
10+
},
11+
admin: {
12+
useAsTitle: 'title',
13+
defaultColumns: ['title', 'subtitle', 'createdAt', '_status'],
14+
},
15+
versions: {
16+
drafts: {
17+
autosave: {
18+
showSaveDraftButton: true,
19+
interval: 1000,
20+
},
21+
},
22+
},
23+
fields: [
24+
{
25+
name: 'title',
26+
type: 'text',
27+
required: true,
28+
},
29+
],
30+
}
31+
32+
export default AutosaveWithDraftButtonPosts

test/versions/config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const filename = fileURLToPath(import.meta.url)
44
const dirname = path.dirname(filename)
55
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
66
import AutosavePosts from './collections/Autosave.js'
7+
import AutosaveWithDraftButtonPosts from './collections/AutosaveWithDraftButton.js'
78
import AutosaveWithValidate from './collections/AutosaveWithValidate.js'
89
import CustomIDs from './collections/CustomIDs.js'
910
import { Diff } from './collections/Diff/index.js'
@@ -17,6 +18,7 @@ import Posts from './collections/Posts.js'
1718
import { TextCollection } from './collections/Text.js'
1819
import VersionPosts from './collections/Versions.js'
1920
import AutosaveGlobal from './globals/Autosave.js'
21+
import AutosaveWithDraftButtonGlobal from './globals/AutosaveWithDraftButton.js'
2022
import DisablePublishGlobal from './globals/DisablePublish.js'
2123
import DraftGlobal from './globals/Draft.js'
2224
import DraftWithMaxGlobal from './globals/DraftWithMax.js'
@@ -35,6 +37,7 @@ export default buildConfigWithDefaults({
3537
DisablePublish,
3638
Posts,
3739
AutosavePosts,
40+
AutosaveWithDraftButtonPosts,
3841
AutosaveWithValidate,
3942
DraftPosts,
4043
DraftWithMax,
@@ -46,7 +49,14 @@ export default buildConfigWithDefaults({
4649
TextCollection,
4750
Media,
4851
],
49-
globals: [AutosaveGlobal, DraftGlobal, DraftWithMaxGlobal, DisablePublishGlobal, LocalizedGlobal],
52+
globals: [
53+
AutosaveGlobal,
54+
AutosaveWithDraftButtonGlobal,
55+
DraftGlobal,
56+
DraftWithMaxGlobal,
57+
DisablePublishGlobal,
58+
LocalizedGlobal,
59+
],
5060
indexSortableFields: true,
5161
localization: {
5262
defaultLocale: 'en',

0 commit comments

Comments
 (0)