Inline Editing in Tina refers to editing values directly in the area they appear on the page, instead of in the Tina sidebar. These are the general steps to set up inline editing:
Play around with this simple demo to get a feel for Inline Editing. Get familiar with the Inline Editing API in this step-by-step guide.
The InlineForm
and InlineField
components can be used to set up inline editing in your layout. InlineForm
receives the form object created via one of the form hooks in order to provide it to the inline editing context.
Note that it is important to use a hook to register a form instead of an HOC or Render Props component. Depending on the Tina packages you are using, the hook names may differ than those seen in the examples.
InlineForm
should wrap the page or component where you want to add inline editing, turning the page into the form itself. You can then nest multiple InlineField
components, a render props-based component that allows you to conditionally display an editing interface (when in edit mode) or the page as it will appear in production.
The rough idea is like this:
<InlineForm form={formObject}>
<InlineField name="name-of-the-field">
{({ input, status }) => {
if (status === 'active') {
// we're in editing mode, show an editable interface
} else {
// we're not in editing mode, show the production layout
}
}}
</InlineField>
</InlineForm>
Let's take a modified version of the simplistic example from the form documentation:
import * as React from React
import ReactMarkdown from 'react-markdown'
import { useForm, usePlugin } from 'tinacms'
export function Page(props) {
const [modifiedValues, form] = useForm(props.data)
usePlugin(form)
return (
<main>
<h1>{modifiedValues.title}</h1>
<ReactMarkdown source={modifiedValues.markdownContent}>
</main>
)
}
Using InlineForm
and InlineField
from react-tinacms-inline
, we would rewrite the Page component as follows:
import * as React from React
import ReactMarkdown from 'react-markdown'
import { useForm } from 'tinacms'
import { Wysiwyg } from 'react-tinacms-editor'
import { InlineForm, InlineField } from 'react-tinacms-inline'
export function Page(props) {
/*
** The `modifiedValues` aren't
** called directly, so we only
** need the form object
*/
const [, form] = useForm(props.data)
return (
<InlineForm form={form}>
<main>
<InlineField name="title">
{({ input, status }) => {
if (status === 'active') {
return <input type="text" {...input} />
}
return <h1>{input.value}</h1>
}}
</InlineField>
<InlineField name="markdownContent">
{({ input, status }) => {
if (status === 'active') {
return <Wysiwyg input={input} />
}
return <ReactMarkdown source={input.value} />
}}
</InlineField>
</main>
</InlineForm>
)
}
When using InlineField
, you can create a custom Inline Field. This is helpful when you need precise control over rendering or input functionality.
However, Tina provides a set of pre-configured Inline Fields that should work for many use cases. These fields provide basic input elements and handle the rendering logic between edit and preview mode.
Refactoring the above example with Inline Fields:
import * as React from React
import ReactMarkdown from 'react-markdown'
import { useForm } from 'tinacms'
import {
InlineForm,
InlineTextField,
InlineWysiwyg,
} from 'react-tinacms-inline'
export function Page(props) {
const [modifiedValues, form] = useForm(props.data)
return (
<InlineForm form={form}>
<main>
<InlineTextField name="title" />
<InlineWysiwyg name="markdownContent">
<ReactMarkdown source={modifiedValues.markdownContent} />
</InlineWysiwyg>
</main>
</InlineForm>
)
}
There may be cases where you want to create your own Inline Fields. Below is an example of the Page
component used above, but refactored to define its own custom Inline Fields.
import * as React from React
import ReactMarkdown from 'react-markdown'
// import `useCMS`
import { useForm, useCMS } from 'tinacms'
import { Wysiwyg } from 'react-tinacms-editor'
import { InlineForm, InlineField } from 'react-tinacms-inline'
export function Page(props) {
// Access the CMS object
const cms = useCMS()
const [, form] = useForm(props.data)
return (
<InlineForm form={form}>
<main>
{/**
* Use `InlineField` and the render props
* pattern to create custom field inputs
* that render when the cms is enabled
*/}
<InlineField name="title">
{({ input, status }) => {
if (cms.enabled) {
return <input type="text" {...input} />
}
return <h1>{input.value}</h1>
}}
</InlineField>
<InlineField name="markdownContent">
{({ input, status }) => {
if (cms.enabled) {
return <Wysiwyg input={input} />
}
return <ReactMarkdown source={input.value} />
}}
</InlineField>
</main>
</InlineForm>
)
}
InlineField
uses render props to pass the form state and other props to its children. Based on cms.enabled
, you can conditionally render editing inputs or the original element / value.
If you have an idea for an Inline Field plugin, consider contributing! Make an issue with your suggestion or reach out on the forum for support.
The Inline Fields are meant to have minimal styles. But there may be situations where you'll want to override the base styles. This is made possible via Styled Components.
// An example `InlineTextField` with Extended Styles
export function Page(props) {
const [, form] = useForm(props.data)
return (
<InlineForm form={form}>
<main>
<StyledText name="title" />
</main>
</InlineForm>
)
}
// Extended InlineTextField styled component
const StyledText = styled(InlineTextField)`
color: green;
`
Notice how the new component, StyledText
is just a styled version of InlineTextField
.