Skip to content

Commit

Permalink
Fix: incomplete typing for reference input (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelSuwinski authored Jun 24, 2024
1 parent e0c087f commit a7827cb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/InputGuesser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import type { SortPayload } from 'react-admin';
import {
AdminContext,
Edit,
Expand Down Expand Up @@ -243,6 +244,29 @@ describe('<InputGuesser />', () => {
});
});

test('renders reference input', async () => {
render(
<AdminContext dataProvider={dataProvider}>
<SchemaAnalyzerContext.Provider value={hydraSchemaAnalyzer}>
<ResourceContextProvider value="users">
<Edit id="/users/123" mutationMode="pessimistic">
<SimpleForm>
<InputGuesser
source="owner"
sort={{ field: 'id', order: 'DESC' } as SortPayload}
/>
</SimpleForm>
</Edit>
</ResourceContextProvider>
</SchemaAnalyzerContext.Provider>
</AdminContext>,
);

expect(
await screen.findAllByText('resources.users.fields.owner'),
).toHaveLength(1);
});

test.each([
// Default enum names.
{
Expand Down
8 changes: 4 additions & 4 deletions src/InputGuesser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const IntrospectedInputGuesser = ({

if (field.reference !== null && typeof field.reference === 'object') {
if (field.maxCardinality === 1) {
const { filter, page, perPage, sort, enableGetChoices } =
const { filter, page, perPage, sort, enableGetChoices, ...rest } =
props as ReferenceInputProps;

return (
Expand All @@ -75,13 +75,13 @@ export const IntrospectedInputGuesser = ({
<SelectInput
optionText={schemaAnalyzer.getFieldNameFromSchema(field.reference)}
validate={guessedValidate}
{...(props as SelectInputProps)}
{...(rest as SelectInputProps)}
/>
</ReferenceInput>
);
}

const { filter, page, perPage, sort, enableGetChoices } =
const { filter, page, perPage, sort, enableGetChoices, ...rest } =
props as ReferenceArrayInputProps;

return (
Expand All @@ -97,7 +97,7 @@ export const IntrospectedInputGuesser = ({
<SelectArrayInput
optionText={schemaAnalyzer.getFieldNameFromSchema(field.reference)}
validate={guessedValidate}
{...(props as SelectArrayInputProps)}
{...(rest as SelectArrayInputProps)}
/>
</ReferenceArrayInput>
);
Expand Down
13 changes: 13 additions & 0 deletions src/__fixtures__/parsedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,17 @@ export const API_FIELDS_DATA = [
enum: { Epic: 'EPIC', 'Fairy tale': 'FAIRY_TALE', Myth: 'MYTH' },
required: false,
}),
new Field('owner', {
id: 'http://localhost/owner',
range: 'https://schema.org/Person',
reference: {
id: 'https://schema.org/Person',
name: 'users',
url: 'http://localhost/users',
fields: [],
},
embedded: null,
maxCardinality: 1,
required: false,
}),
];
16 changes: 12 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import type {
ReferenceFieldProps,
ReferenceInputProps,
ResourceProps,
SelectArrayInputProps,
SelectInputProps,
ShowProps,
SimpleFormProps,
SingleFieldListProps,
Expand Down Expand Up @@ -479,20 +481,26 @@ type InputProps =
| BooleanInputProps
| NumberInputProps
| ArrayInputProps
| ReferenceArrayInputProps
| ReferenceInputProps;
| SelectArrayInputProps
| SelectInputProps;

export type IntrospectedInputGuesserProps = Partial<InputProps> &
IntrospectedGuesserProps & {
transformEnum?: (value: string | number) => string | number;
};
} & Pick<
ReferenceInputProps | ReferenceArrayInputProps,
'filter' | 'page' | 'perPage' | 'sort' | 'enableGetChoices'
>;

export type InputGuesserProps = Omit<
InputProps & Omit<BaseIntrospecterProps, 'resource'>,
'component'
> & {
transformEnum?: (value: string | number) => string | number;
};
} & Pick<
ReferenceInputProps | ReferenceArrayInputProps,
'filter' | 'page' | 'perPage' | 'sort' | 'enableGetChoices'
>;

export type IntrospecterProps = (
| CreateGuesserProps
Expand Down

0 comments on commit a7827cb

Please sign in to comment.