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
6 changes: 6 additions & 0 deletions .changeset/purple-keys-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tinacms/schema-tools": minor
"tinacms": minor
---

✨ Object Schema - Add openFormOnCreate flag. Allowing users to opt-in to automatically going inside an object form when the field is true
10 changes: 8 additions & 2 deletions packages/@tinacms/schema-tools/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,14 @@ export type ObjectField<WithNamespace extends boolean = false> = (
| FieldGeneric<string, false, ObjectUiProps>
) &
MaybeNamespace<WithNamespace> &
BaseField &
(
BaseField & {
/**
* @default false
* When `list: true`, automatically opens the form for newly created list items.
* When used without `list: true`, this does not do anything.
*/
openFormOnCreate?: boolean;
} & (
| {
type: 'object';
fields: Field<WithNamespace>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface GroupFieldDefinititon extends Field {
component: 'group';
fields: Field[];
defaultItem?: object | (() => object);
openFormOnCreate?: boolean;
addItemBehavior?: 'append' | 'prepend';
/**
* An optional function which generates `props` for
Expand Down Expand Up @@ -46,6 +47,9 @@ interface GroupProps {
}

const Group = ({ tinaForm, form, field, input, meta, index }: GroupProps) => {
const cms = useCMS();
const { dispatch: setFocusedField } =
useEvent<FieldFocusEvent>('field:focus');
const addItem = React.useCallback(() => {
let obj = {};
if (typeof field.defaultItem === 'function') {
Expand All @@ -58,6 +62,26 @@ const Group = ({ tinaForm, form, field, input, meta, index }: GroupProps) => {
} else {
form.mutators.push(field.name, obj);
}
if (field.openFormOnCreate) {
const state = tinaForm.finalForm.getState();
const newIndex = field.addItemBehavior === 'prepend' ? 0 : items.length;
if (state.invalid === true) {
// @ts-ignore
cms.alerts.error('Cannot navigate away from an invalid form.');
return;
}
cms.dispatch({
type: 'forms:set-active-field-name',
value: {
formId: tinaForm.id,
fieldName: `${field.name}.${newIndex}`,
},
});
setFocusedField({
id: tinaForm.id,
fieldName: `${field.name}.${newIndex}`,
});
}
}, [form, field]);

const items = input.value || [];
Expand Down
Loading