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

Introduce SourceContext #9533

Merged
merged 28 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
afd756d
Introduce SourcePrefixContext
djhi Dec 18, 2023
8b09539
Update upgrade guide
djhi Dec 18, 2023
f782ab8
Refactor to handle TranslatableInput
djhi Dec 20, 2023
7396578
Apply suggestions from code review
djhi Dec 20, 2023
e00521c
Apply suggestions from code review
djhi Dec 20, 2023
c954747
Apply suggestions from code review
djhi Dec 20, 2023
7d14f1d
Linting
djhi Dec 20, 2023
6dd99fc
Rewriting
fzaninotto Jan 3, 2024
4484f60
Add an upgrade guide section about FormDataConsumer
djhi Jan 4, 2024
f56bb8c
Revert FieldTitle unnecessary change
djhi Jan 4, 2024
2ad60b3
Rename SourceContext file and extract useWrappedSource
djhi Jan 4, 2024
cfaf018
Revert breaking changes for labels
djhi Jan 4, 2024
04ddc0e
Make test title clearer
djhi Jan 4, 2024
ba8ffca
Apply reviews suggestions
djhi Jan 5, 2024
6244870
Refactor to handle labels correctly
djhi Jan 5, 2024
35c6d9b
Refactor to centralize label logic
djhi Jan 5, 2024
59bc45f
Restore removed test
djhi Jan 8, 2024
d9c9fb8
Apply review suggestion
djhi Jan 8, 2024
005da20
Apply suggestions from code review
djhi Jan 8, 2024
e602813
Reintroduce useWrappedSource
djhi Jan 8, 2024
b1f8ce1
Compute TranslatableInputs label translationKey in place
djhi Jan 8, 2024
8c1d5b7
Rename variables
djhi Jan 8, 2024
cc0e461
Replace LabelPrefix with SourceContext
djhi Jan 9, 2024
6d69112
Simplify TranslatableInputsTabContent getLabel
djhi Jan 11, 2024
84c71d8
Handle scalar array inputs
djhi Jan 11, 2024
5496cca
Apply suggestions from code review
djhi Jan 11, 2024
50258d5
Update documentation to remove passing rest parameters
djhi Jan 11, 2024
3894f03
Better ArrayInput i18n story
djhi Jan 12, 2024
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
10 changes: 10 additions & 0 deletions packages/ra-core/src/form/useInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ export const useInput = <ValueType = any>(
const formGroups = useFormGroups();
const record = useRecordContext();

if (
!source &&
props.label == null &&
process.env.NODE_ENV === 'development'
) {
console.warn(
'If your input source is empty, you must provide a label prop.'
djhi marked this conversation as resolved.
Show resolved Hide resolved
);
}

useEffect(() => {
if (!formGroups || formGroupName == null) {
return;
Expand Down
39 changes: 23 additions & 16 deletions packages/ra-core/src/i18n/TestTranslationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@ import * as React from 'react';
import lodashGet from 'lodash/get';

import { I18nContextProvider } from './I18nContextProvider';
import { I18nProvider } from '../types';

export const TestTranslationProvider = ({
translate,
messages,
children,
}: any) => (
<I18nContextProvider
value={{
translate: messages
? (key: string, options?: any) => {
const message = lodashGet(messages, key);
return message
? typeof message === 'function'
? message(options)
: message
: options?._ || key;
}
: translate,
changeLocale: () => Promise.resolve(),
getLocale: () => 'en',
}}
>
<I18nContextProvider value={testI18nProvider({ translate, messages })}>
{children}
</I18nContextProvider>
);

export const testI18nProvider = ({
translate,
messages,
}: {
translate?: I18nProvider['translate'];
messages?: Record<string, string | ((options?: any) => string)>;
}): I18nProvider => ({
translate: messages
? (key, options) => {
const message = lodashGet(messages, key);
return message
? typeof message === 'function'
? message(options)
: message
: options?._ || key;
}
: translate,
changeLocale: () => Promise.resolve(),
getLocale: () => 'en',
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Admin } from 'react-admin';
import { required, Resource } from 'ra-core';
import { required, Resource, testI18nProvider } from 'ra-core';
import { createMemoryHistory } from 'history';
import { InputAdornment } from '@mui/material';

Expand Down Expand Up @@ -167,6 +167,47 @@ export const Scalar = () => (
</Admin>
);

export const ScalarI18n = () => (
<Admin
dataProvider={dataProvider}
history={history}
i18nProvider={testI18nProvider({
messages: {
resources: {
books: {
fields: {
tags: 'Some tags',
},
},
},
},
})}
>
<Resource
name="books"
edit={() => (
<Edit
mutationMode="pessimistic"
mutationOptions={{
onSuccess: data => {
console.log(data);
},
}}
>
<SimpleForm>
<TextInput source="title" />
<ArrayInput source="tags" fullWidth>
<SimpleFormIterator disableReordering>
<TextInput source="" helperText={false} />
slax57 marked this conversation as resolved.
Show resolved Hide resolved
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
)}
/>
</Admin>
);

const order = {
id: 1,
date: '2022-08-30',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ export const SimpleFormIteratorItem = React.forwardRef(
source ? `${member}.${source}` : member,
getLabel: (source: string) => {
// remove digits, e.g. 'book.authors.2.categories.3.identifier.name' => 'book.authors.categories.identifier.name'
const itemSource = `${member.replace(
/\.\d+/g,
''
)}.${source}`;
const sanitizedMember = member.replace(/\.\d+/g, '');
// source may be empty for scalar values arrays
const itemSource = source
? `${sanitizedMember}.${source}`
: sanitizedMember;

return parentSourceContext
? parentSourceContext.getLabel(itemSource)
: getResourceFieldLabelKey(resource, itemSource);
Expand Down
Loading