Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use form load initial values #1356

Merged
merged 7 commits into from Jul 30, 2020
4 changes: 1 addition & 3 deletions packages/@tinacms/react-core/src/use-form.ts
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
import { FormOptions, Form, Field } from '@tinacms/forms'
import * as React from 'react'
import { usePlugins } from './use-plugin'
import { useCMS } from './use-cms'

export interface WatchableFormValue {
values: any
Expand Down Expand Up @@ -48,7 +47,6 @@ export function useForm<FormShape = any>(
{ loadInitialValues, ...options }: FormOptions<any>,
watch: Partial<WatchableFormValue> = {}
): [FormShape, Form] {
const cms = useCMS()
/**
* `initialValues` will be usually be undefined if `loadInitialValues` is used.
*
Expand Down Expand Up @@ -82,7 +80,7 @@ export function useForm<FormShape = any>(
)

React.useEffect(() => {
if (cms.enabled && loadInitialValues) {
if (loadInitialValues) {
loadInitialValues().then((values: any) => {
form.updateInitialValues(values)
})
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby-tinacms-git/src/use-git-form.ts
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

*/

import { useForm, Form, FormOptions, WatchableFormValue } from 'tinacms'
import { useForm, Form, FormOptions, WatchableFormValue, useCMS } from 'tinacms'
import { useGitFile } from '@tinacms/git-client'

export interface GitNode {
Expand All @@ -34,13 +34,17 @@ export function useGitForm<N extends GitNode>(
options: GitFormOptions<N>,
watch: WatchableFormValue
): [N, Form] {
const cms = useCMS()
const { format, parse, ...config } = options
const gitFile = useGitFile(node.fileRelativePath, format, parse)

const defaultConfig = {
label: '',
fields: [],
loadInitialValues: gitFile.show,
async loadInitialValues() {
if (cms.disabled) return watch.values
return gitFile.show()
},
onSubmit: gitFile.commit,
reset: gitFile.reset,
onChange({ values }: any) {
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-tinacms-mdx/src/useMdxForm.tsx
Expand Up @@ -107,7 +107,8 @@ export function useMdxForm(
{
label,
id,
loadInitialValues() {
async loadInitialValues() {
if (cms.disabled) return valuesOnDisk
return cms.api.git
.show(id) // Load the contents of this file at HEAD
.then((git: any) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/next-tinacms-json/src/use-json-form.ts
Expand Up @@ -55,7 +55,8 @@ export function useJsonForm<T = any>(
fields,
actions,
buttons,
loadInitialValues() {
async loadInitialValues() {
if (cms.disabled) return jsonFile.data
return cms.api.git
.show(jsonFile.fileRelativePath) // Load the contents of this file at HEAD
.then((git: { content: string }) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/next-tinacms-markdown/src/use-markdown-form.ts
Expand Up @@ -71,7 +71,8 @@ export function useMarkdownForm(
fields,
actions,
buttons,
loadInitialValues() {
async loadInitialValues() {
if (cms.disabled) return valuesOnDisk
return cms.api.git
.show(markdownFile.fileRelativePath) // Load the contents of this file at HEAD
.then((git: { content: string }) => {
Expand Down