Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/real-suns-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tinacms": minor
---

Added parse field for filename field
10 changes: 10 additions & 0 deletions packages/@tinacms/schema-tools/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,16 @@ export interface UICollection<Form = any, CMS = any, TinaForm = any> {
* Customize the way filenames are generated during content creation
*/
filename?: {
/**
* A callback function which formats the filename each time the value changes
* to enforce naming constraints (the extension is not necessary)
*
* eg:
* ```ts
* parse: (value) => value.toLowerCase().split(" ").join("-")
* ```
*/
parse?: (filename: string) => string;
/**
* A callback which receives form values as an argument. The return value
* here will be used as the filename (the extension is not necessary)
Expand Down
3 changes: 2 additions & 1 deletion packages/tinacms/src/admin/pages/CollectionCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ export const RenderForm = ({
{};

const fileReadOnly = schemaCollection?.ui?.filename?.readonly;

const parse = schemaCollection?.ui?.filename?.parse;
const filenameField = {
name: 'filename',
label: 'Filename',
parse,
component:
slugFunction && !fileReadOnly
? wrapFieldsWithMeta(({ field, input, meta }) => {
Expand Down
14 changes: 11 additions & 3 deletions packages/tinacms/src/admin/pages/CollectionListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ const CollectionListPage = () => {
const collectionDefinition = cms.api.tina.schema.getCollection(
collection.name
);

const parse = collectionDefinition?.ui?.filename?.parse;
const allowCreate =
collectionDefinition?.ui?.allowedActions?.create ?? true;
const allowDelete =
Expand Down Expand Up @@ -440,6 +440,7 @@ const CollectionListPage = () => {

{renameModalOpen && (
<RenameModal
parser={parse}
filename={vars.relativePathWithoutExtension}
newRelativePath={vars.newRelativePath}
setNewRelativePath={(newRelativePath) => {
Expand Down Expand Up @@ -1459,12 +1460,13 @@ interface ModalProps {
}

const RenameModal = ({
parser,
close,
renameFunc,
filename,
newRelativePath,
setNewRelativePath,
}: ModalProps) => {
}: ModalProps & { parser?: (filename: string) => string }) => {
return (
<Modal>
<PopupModal>
Expand All @@ -1477,7 +1479,13 @@ const RenameModal = ({
<BaseTextField
placeholder="Enter a new name for the document's file"
value={newRelativePath}
onChange={(event) => setNewRelativePath(event.target.value)}
onChange={(event) => {
let value = event.target.value;
if (parser) {
value = parser(value);
}
setNewRelativePath(value);
}}
/>
</>
</ModalBody>
Expand Down
Loading