Skip to content

Commit

Permalink
Add append on duplication for slug fields
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpopus committed Nov 14, 2023
1 parent 4920d7c commit a9d2234
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ The `SlugField` accepts the following parameters

- `useFields` - `string[]` defaults to `['title']`

- `appendOnDuplication` - `boolean` defaults to `false` | Adds a validation hook that appends `-1` to your slug to avoid conflicts (experimental)

- `slugify` - Options to be passed to the [slugify](https://www.npmjs.com/package/slugify) function

```ts
Expand Down Expand Up @@ -123,6 +125,10 @@ Here is a more full example:
)
```

### Notes

`index` and `unique` are set to true by default

## Combo field

[source](https://github.com/NouanceLabs/payload-better-fields-plugin/tree/master/src/fields/Combo)
Expand Down
3 changes: 3 additions & 0 deletions dev/src/collections/SlugExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const SlugExamples: CollectionConfig = {
},
{
useFields: ['title', 'subtitle'],
appendOnDuplication: true,
},
),
{
Expand All @@ -49,6 +50,8 @@ const SlugExamples: CollectionConfig = {
...SlugField(
{
name: 'secondSlug',
index: false,
unique: false,
admin: {
position: 'sidebar',
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nouance/payload-better-fields-plugin",
"version": "1.0.2",
"version": "1.1.0",
"homepage:": "https://github.com/NouanceLabs/payload-better-fields-plugin",
"repository": "[email protected]:NouanceLabs/payload-better-fields-plugin.git",
"description": "A Payload plugin that aims to provide improved fields for the admin panel",
Expand Down
48 changes: 40 additions & 8 deletions src/fields/Slug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SlugifyOptions } from '../../types'
import { TextField, CheckboxField } from 'payload/types'
import beforeValidate from './beforeValidate'
import { PartialRequired } from '../../utilities/partialRequired'
import { FieldHook } from 'payload/types'

/**
* Additional config unique to the Slug field
Expand All @@ -15,6 +16,10 @@ export type Config = {
* @default {string[]} ['title']
*/
useFields: string[]
/**
* Append a '-1' on duplication of collection in the case that the slug needs to be unique
*/
appendOnDuplication?: boolean
/**
* Options passed to the slugify function
* @default { lower: true }
Expand Down Expand Up @@ -62,6 +67,7 @@ export const SlugField: Slug = (
config = {
useFields: ['title'],
slugify: { lower: true, remove: /[*+~.()'"!?#\.,:@]/g },
appendOnDuplication: false,
},
checkbox = {
enable: true,
Expand All @@ -86,35 +92,61 @@ export const SlugField: Slug = (
checkbox.overrides,
)

const checkboxName = checkboxField.name
const slugName = slugOverrides.name ?? 'slug'

const checkboxDedupe: FieldHook[] = [
({ operation, siblingData }) => {
if (operation === 'create') {
const slugValue = siblingData[slugName]

if (slugValue && slugValue !== '') {
return true
}
}
},
]

const slugDedupe: FieldHook[] = [
({ operation, value }) => {
if (operation === 'create') {
if (value && value !== '') {
const incrementedValue = value + '-1'

return incrementedValue
}
}
},
]

const editField = deepMerge<CheckboxField, Partial<CheckboxField>>(
{
name: checkboxField.name,
name: checkboxName,
label: checkboxField.label,
type: 'checkbox',
required: false,
admin: {
disabled: !checkbox.enable,
hidden: true,
},
hooks: {
beforeValidate: [...(config.appendOnDuplication ? checkboxDedupe : [])],
},
},
checkboxField,
)

const slugField = deepMerge<TextField, Partial<TextField>>(
{
name: 'slug',
name: slugName,
label: 'Slug',
type: 'text',
index: true,
required: false,
hooks: {
beforeValidate: [
beforeValidate(
config.useFields,
Boolean(checkbox.enable),
editField.name,
slugifyOptions,
),
beforeValidate(config.useFields, Boolean(checkbox.enable), checkboxName, slugifyOptions),
...(config.appendOnDuplication ? slugDedupe : []),
],
},
unique: true,
Expand Down

0 comments on commit a9d2234

Please sign in to comment.