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

Fix hoc config #723

Merged
merged 2 commits into from
Feb 6, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/demo-next/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import { withTina } from 'tinacms'
import { GitClient } from '@tinacms/git-client'

export default withTina(App, {
cms: {
apis: {
git: new GitClient('http://localhost:3000/___tina')
}
apis: {
git: new GitClient('http://localhost:3000/___tina'),
},
sidebar: {
hidden: process.env.NODE_ENV === 'production'
}
})
hidden: process.env.NODE_ENV === 'production',
},
})
68 changes: 30 additions & 38 deletions packages/tinacms/src/react-tinacms/with-tina.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,36 @@ limitations under the License.
*/

import * as React from 'react'
import { CMSConfig } from '@tinacms/core'
import { Tina, TinaProps } from '../components/Tina'
import { TinaCMS } from '../tina-cms'

export interface TinaConfig {
cms?: CMSConfig,
sidebar?: {
hidden?: TinaProps["hidden"],
theme?: TinaProps["theme"]
}
import { Tina } from '../components/Tina'
import { TinaCMS, TinaCMSConfig } from '../tina-cms'

function mergeDefaultConfig(config?: TinaCMSConfig) {
// TODO maybe use lodash/fp/defaultsDeep
// Using lodash/fp libs requires installing all of lodash so requires consideration
const merge = require('lodash.merge')
const cloneDeep = require('lodash.clonedeep')
return merge(
{
plugins: [],
apis: {},
sidebar: {
position: 'displace',
hidden: false,
theme: {},
},
},
cloneDeep(config)
)
}

function mergeDefaultConfig(config?: TinaConfig) {
// TODO maybe use lodash/fp/defaultsDeep
// Using lodash/fp libs requires installing all of lodash so requires consideration
const merge = require('lodash.merge')
const cloneDeep = require('lodash.clonedeep')
return merge({
cms: {
plugins: [],
apis: {},
},
sidebar: {
position: 'displace',
hidden: false,
theme: {}
}
}, cloneDeep(config))
export function withTina(Component: any, config?: TinaCMSConfig) {
return (props: any) => {
const safeConfig = React.useMemo(() => mergeDefaultConfig(config), [config])
const cms = React.useMemo(() => new TinaCMS(safeConfig), [safeConfig])
return (
<Tina cms={cms} {...safeConfig.sidebar}>
<Component {...props} />
</Tina>
)
}
}

export function withTina(Component: any, config?: TinaConfig) {
return (props: any) => {
const safeConfig = React.useMemo(() => mergeDefaultConfig(config), [config])
const cms = React.useMemo(() => new TinaCMS(safeConfig.cms), [safeConfig])
return (
<Tina cms={cms} {...safeConfig.sidebar}>
<Component {...props} />
</Tina>
)
}
}