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 render vocabulary field #207

Merged
merged 3 commits into from
Jan 8, 2024
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ e2e/example/.next
# Guillotina
.python-version
*.egg-info
__pycache__
__pycache__

.vscode
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.24.1
------
- fix: Render vocabulary field

0.24.0
------
- feat: Implement and update some guillotina behaviors
Expand Down
1 change: 0 additions & 1 deletion e2e/vite_example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ function App() {
return null
}

console.log('language', navigator.language)
return (
<ClientProvider client={clientInstance}>
<Layout auth={auth} onLogout={onLogout}>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.24.0",
"version": "0.24.1",
"repository": {
"type": "git",
"url": "[email protected]:guillotinaweb/guillotina_react.git"
Expand Down
73 changes: 49 additions & 24 deletions src/guillo-gmi/components/fields/renderField.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React from 'react'
import { DownloadField } from './downloadField'
import { useIntl } from 'react-intl'
import { useVocabulary } from '../../hooks/useVocabulary'
import { get } from '../../lib/utils'
import { useIntl } from 'react-intl'

const plain = ['string', 'number', 'boolean']

export function RenderField({ value, Widget }) {
export function RenderField({ value, Widget, schema }) {
if (value === null || value === undefined) return ''

if (Widget) {
return <Widget value={value} />
return <Widget value={value} schema={schema} />
}

const type = typeof value
Expand Down Expand Up @@ -40,13 +41,52 @@ const FieldValue = ({ field, value }) => (
</div>
)

export function RenderFieldComponent({ schema, field, val, modifyContent }) {
const intl = useIntl()
const DEFAULT_VALUE_EDITABLE_FIELD = intl.formatMessage({
const DEFAULT_VALUE_NO_EDITABLE_FIELD = ' -- '
const getDefaultValueEditableField = (intl) => {
return intl.formatMessage({
id: 'default_value_editable_field',
defaultMessage: 'Click to edit',
})
const DEFAULT_VALUE_NO_EDITABLE_FIELD = ' -- '
}

export const VocabularyRenderField = ({ schema, value, modifyContent }) => {
const intl = useIntl()
const DEFAULT_VALUE_EDITABLE_FIELD = getDefaultValueEditableField(intl)

const vocabularyName = schema?.items?.vocabularyName || schema?.vocabularyName
const vocabulary = useVocabulary(vocabularyName)

const getRenderProps = () => {
const renderProps = {
value:
value ??
(modifyContent
? DEFAULT_VALUE_EDITABLE_FIELD
: DEFAULT_VALUE_NO_EDITABLE_FIELD),
}

if (schema?.vocabularyName) {
const vocabularyValue = get(vocabulary, 'data.items', []).find(
(item) => item.token === value
)
renderProps['value'] = vocabularyValue?.title ?? ''
} else {
renderProps['value'] = (renderProps['value'] ?? []).map((value) => {
return (
get(vocabulary, 'data.items', []).find((item) => item.token === value)
?.title ?? ''
)
})
}

return renderProps
}
return <RenderField {...getRenderProps()} />
}

export function RenderFieldComponent({ schema, field, val, modifyContent }) {
const intl = useIntl()
const DEFAULT_VALUE_EDITABLE_FIELD = getDefaultValueEditableField(intl)

const getRenderProps = () => {
const renderProps = {
Expand All @@ -67,23 +107,8 @@ export function RenderFieldComponent({ schema, field, val, modifyContent }) {
} else if (val && schema?.type === 'datetime') {
renderProps['value'] = new Date(val).toLocaleString()
} else if (schema?.items?.vocabularyName || schema?.vocabularyName) {
const vocabularyName =
schema?.items?.vocabularyName || schema?.vocabularyName
const vocabulary = useVocabulary(vocabularyName)
if (schema?.vocabularyName) {
const vocabularyValue = get(vocabulary, 'data.items', []).find(
(item) => item.token === val
)
renderProps['value'] = vocabularyValue?.title ?? ''
} else {
renderProps['value'] = (renderProps['value'] ?? []).map((value) => {
return (
get(vocabulary, 'data.items', []).find(
(item) => item.token === value
)?.title ?? ''
)
})
}
renderProps['Widget'] = VocabularyRenderField
renderProps['schema'] = schema
}

return renderProps
Expand Down
1 change: 1 addition & 0 deletions src/guillo-gmi/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from './input/dropdown'
export * from './fields/editableField'
export * from './fields/renderField'
export * from './fields/downloadField'
export * from './fields/editComponent'

export * from './behaviors/iattachment'
export * from './behaviors/imultiattachment'
Expand Down
Loading