Slug Field
payload-extra-fields/slugFreeItem
Copy of original payload slug field to allow code customization.
Install it
npx shadcn@latest add https://payload.veiag.dev/r/slug.jsonSource
1'use client'23import React, { useCallback, useState } from 'react'4import { Button, useForm, useField, FieldLabel, TextInput } from '@payloadcms/ui'5import './style.scss'6import { SlugFieldProps } from './field'7import { slugify } from './slugify'89/**10 * @experimental This component is experimental and may change or be removed in the future. Use at your own risk.11 */12export const SlugField: React.FC<SlugFieldProps> = ({13 field,14 fieldToUse,15 path,16 readOnly: readOnlyFromProps,17}) => {18 const { label } = field1920 const { setValue, value } = useField<string>({ path: path || field.name })2122 const { getDataByPath } = useForm()2324 const [isLocked, setIsLocked] = useState(true)2526 const handleGenerate = useCallback(27 (e: React.MouseEvent<Element>) => {28 e.preventDefault()2930 const targetFieldValue = getDataByPath(fieldToUse || 'title')3132 if (targetFieldValue) {33 const formattedSlug = slugify(targetFieldValue as string)3435 if (value !== formattedSlug) {36 setValue(formattedSlug)37 }38 } else {39 if (value !== '') {40 setValue('')41 }42 }43 },44 [setValue, value, fieldToUse, getDataByPath],45 )4647 const toggleLock = useCallback((e: React.MouseEvent<Element>) => {48 e.preventDefault()49 setIsLocked((prev) => !prev)50 }, [])5152 return (53 <div className="field-type slug-field-component">54 <div className="label-wrapper">55 <FieldLabel htmlFor={`field-${path}`} label={label} />56 {!isLocked && (57 <Button buttonStyle="none" className="lock-button" onClick={handleGenerate}>58 Generate59 </Button>60 )}61 <Button buttonStyle="none" className="lock-button" onClick={toggleLock}>62 {isLocked ? 'Unlock' : 'Lock'}63 </Button>64 </div>65 <TextInput66 onChange={setValue}67 path={path || field.name}68 readOnly={Boolean(readOnlyFromProps || isLocked)}69 value={value}70 />71 </div>72 )73}
Files it writes
More from payload-extra-fields
All 4 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Custom Array Row Labelarray-row-label | payload-extra-fields | Other | Free | no deps · 3 files | |
| Color Pickercolor-picker | payload-extra-fields | Other | Free | no deps · 4 files | |
| Lucide Icon Pickerlucide-icon-picker | payload-extra-fields | Other | Free | 4 deps · 5 files |
