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

Upgrade inflection to v3 #9804

Merged
merged 5 commits into from
May 1, 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
3 changes: 1 addition & 2 deletions examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"@apollo/client": "^3.3.19",
"@mui/icons-material": "^5.15.15",
"@mui/material": "^5.15.15",
"@types/inflection": "^1.5.28",
"@types/recharts": "^1.8.10",
"@vitejs/plugin-react": "^2.2.0",
"clsx": "^1.1.1",
Expand All @@ -17,7 +16,7 @@
"fetch-mock": "~9.11.0",
"graphql": "^15.6.0",
"graphql-tag": "^2.12.6",
"inflection": "~1.12.0",
"inflection": "^3.0.0",
"json-graphql-server": "~3.0.1",
"prop-types": "^15.8.1",
"query-string": "^7.1.1",
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/src/categories/CategoryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
RecordContextProvider,
useListContext,
} from 'react-admin';
import inflection from 'inflection';
import {
Grid,
Card,
Expand All @@ -14,6 +13,7 @@ import {
CardActions,
Typography,
} from '@mui/material';
import { humanize } from 'inflection';

import LinkToRelatedProducts from './LinkToRelatedProducts';
import { Category } from '../types';
Expand Down Expand Up @@ -62,7 +62,7 @@ const CategoryGrid = () => {
component="h2"
align="center"
>
{inflection.humanize(record.name)}
{humanize(record.name)}
</Typography>
</CardContent>
<CardActions
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/src/products/Aside.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import inflection from 'inflection';
import { Card, CardContent } from '@mui/material';
import LocalOfferIcon from '@mui/icons-material/LocalOfferOutlined';
import BarChartIcon from '@mui/icons-material/BarChart';
Expand All @@ -13,6 +12,7 @@ import {
} from 'react-admin';

import { Category } from '../types';
import { humanize } from 'inflection';

const Aside = () => {
const { data } = useGetList<Category>('categories', {
Expand Down Expand Up @@ -118,7 +118,7 @@ const Aside = () => {
{data &&
data.map((record: any) => (
<FilterListItem
label={inflection.humanize(record.name)}
label={humanize(record.name)}
key={record.id}
value={{ category_id: record.id }}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"date-fns": "^3.6.0",
"eventemitter3": "^4.0.7",
"hotscript": "^1.0.12",
"inflection": "~1.12.0",
"inflection": "^3.0.0",
"jsonexport": "^3.2.0",
"lodash": "~4.17.5",
"prop-types": "^15.8.1",
Expand Down
9 changes: 4 additions & 5 deletions packages/ra-core/src/core/useGetResourceLabel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inflection from 'inflection';

import { useResourceDefinitions } from './useResourceDefinitions';
import { useTranslate } from '../i18n';
import { humanize, pluralize, singularize } from 'inflection';

/**
* A hook which returns function to get a translated resource name. It will use the label option of the `Resource` component if it was provided.
Expand Down Expand Up @@ -40,10 +39,10 @@ export const useGetResourceLabel = (): GetResourceLabel => {
smart_count: count,
_: resourceDefinition.options.label,
})
: inflection.humanize(
: humanize(
count > 1
? inflection.pluralize(resource)
: inflection.singularize(resource)
? pluralize(resource)
: singularize(resource)
),
});

Expand Down
10 changes: 5 additions & 5 deletions packages/ra-core/src/inference/inferElementFromValues.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import inflection from 'inflection';

import getValuesFromRecords from './getValuesFromRecords';
import InferredElement from './InferredElement';
Expand All @@ -17,6 +16,7 @@ import {
valuesAreString,
} from './assertions';
import { InferredTypeMap } from './types';
import { pluralize } from 'inflection';

const DefaultComponent = () => <span>;</span>;
const defaultType = {
Expand Down Expand Up @@ -86,7 +86,7 @@ const inferElementFromValues = (
return new InferredElement(types.id, { source: name });
}
if (name.substr(name.length - 3) === '_id' && hasType('reference', types)) {
const reference = inflection.pluralize(name.substr(0, name.length - 3));
const reference = pluralize(name.substr(0, name.length - 3));
return (
types.reference &&
new InferredElement(types.reference, {
Expand All @@ -96,7 +96,7 @@ const inferElementFromValues = (
);
}
if (name.substr(name.length - 2) === 'Id' && hasType('reference', types)) {
const reference = inflection.pluralize(name.substr(0, name.length - 2));
const reference = pluralize(name.substr(0, name.length - 2));
return (
types.reference &&
new InferredElement(types.reference, {
Expand All @@ -109,7 +109,7 @@ const inferElementFromValues = (
name.substr(name.length - 4) === '_ids' &&
hasType('referenceArray', types)
) {
const reference = inflection.pluralize(name.substr(0, name.length - 4));
const reference = pluralize(name.substr(0, name.length - 4));
return (
types.referenceArray &&
new InferredElement(types.referenceArray, {
Expand All @@ -122,7 +122,7 @@ const inferElementFromValues = (
name.substr(name.length - 3) === 'Ids' &&
hasType('referenceArray', types)
) {
const reference = inflection.pluralize(name.substr(0, name.length - 3));
const reference = pluralize(name.substr(0, name.length - 3));
return (
types.referenceArray &&
new InferredElement(types.referenceArray, {
Expand Down
19 changes: 5 additions & 14 deletions packages/ra-core/src/inference/inferTypeFromValues.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import inflection from 'inflection';

import getValuesFromRecords from './getValuesFromRecords';

import {
Expand All @@ -17,6 +15,7 @@ import {
valuesAreImageUrl,
valuesAreEmail,
} from './assertions';
import { pluralize } from 'inflection';

export const InferenceTypes = [
'array',
Expand Down Expand Up @@ -69,9 +68,7 @@ export const inferTypeFromValues = (
type: 'reference',
props: {
source: name,
reference: inflection.pluralize(
name.substr(0, name.length - 3)
),
reference: pluralize(name.substr(0, name.length - 3)),
},
children: { type: 'referenceChild' },
};
Expand All @@ -81,9 +78,7 @@ export const inferTypeFromValues = (
type: 'reference',
props: {
source: name,
reference: inflection.pluralize(
name.substr(0, name.length - 2)
),
reference: pluralize(name.substr(0, name.length - 2)),
},
children: { type: 'referenceChild' },
};
Expand All @@ -93,9 +88,7 @@ export const inferTypeFromValues = (
type: 'referenceArray',
props: {
source: name,
reference: inflection.pluralize(
name.substr(0, name.length - 4)
),
reference: pluralize(name.substr(0, name.length - 4)),
},
children: { type: 'referenceArrayChild' },
};
Expand All @@ -105,9 +98,7 @@ export const inferTypeFromValues = (
type: 'referenceArray',
props: {
source: name,
reference: inflection.pluralize(
name.substr(0, name.length - 3)
),
reference: pluralize(name.substr(0, name.length - 3)),
},
children: { type: 'referenceArrayChild' },
};
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/util/getFieldLabelTranslationArgs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import inflection from 'inflection';
import { transform } from 'inflection';

interface Args {
label?: string;
Expand Down Expand Up @@ -39,7 +39,7 @@ export const getFieldLabelTranslationArgs = (

const { sourceWithoutDigits, sourceSuffix } = getSourceParts(source);

const defaultLabelTranslation = inflection.transform(
const defaultLabelTranslation = transform(
sourceSuffix.replace(/\./g, ' '),
['underscore', 'humanize']
);
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-no-code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@tanstack/react-query": "^5.8.4",
"clsx": "^1.1.1",
"date-fns": "^3.6.0",
"inflection": "~1.12.0",
"inflection": "^3.0.0",
"lodash": "~4.17.5",
"papaparse": "^5.3.0",
"prop-types": "^15.8.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"css-mediaquery": "^0.1.2",
"dompurify": "^2.4.3",
"hotscript": "^1.0.12",
"inflection": "~1.12.0",
"inflection": "^3.0.0",
"jsonexport": "^3.2.0",
"lodash": "~4.17.5",
"prop-types": "^15.8.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { Fragment, ReactElement } from 'react';
import PropTypes from 'prop-types';
import ActionDelete from '@mui/icons-material/Delete';
import inflection from 'inflection';

import { alpha, styled } from '@mui/material/styles';
import {
MutationMode,
Expand All @@ -21,6 +21,7 @@ import { Confirm } from '../layout';
import { Button, ButtonProps } from './Button';
import { BulkActionProps } from '../types';
import { UseMutationOptions } from '@tanstack/react-query';
import { humanize, inflect } from 'inflection';

export const BulkDeleteWithConfirmButton = (
props: BulkDeleteWithConfirmButtonProps
Expand Down Expand Up @@ -117,14 +118,11 @@ export const BulkDeleteWithConfirmButton = (
smart_count: selectedIds.length,
name: translate(`resources.${resource}.forcedCaseName`, {
smart_count: selectedIds.length,
_: inflection.humanize(
_: humanize(
translate(`resources.${resource}.name`, {
smart_count: selectedIds.length,
_: resource
? inflection.inflect(
resource,
selectedIds.length
)
? inflect(resource, selectedIds.length)
: undefined,
}),
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { Fragment, useState, ReactElement } from 'react';
import PropTypes from 'prop-types';
import ActionUpdate from '@mui/icons-material/Update';
import inflection from 'inflection';

import { alpha, styled } from '@mui/material/styles';
import {
useListContext,
Expand All @@ -20,6 +20,7 @@ import { Confirm } from '../layout';
import { Button, ButtonProps } from './Button';
import { BulkActionProps } from '../types';
import { UseMutationOptions } from '@tanstack/react-query';
import { humanize, inflect } from 'inflection';

export const BulkUpdateWithConfirmButton = (
props: BulkUpdateWithConfirmButtonProps
Expand Down Expand Up @@ -118,14 +119,11 @@ export const BulkUpdateWithConfirmButton = (
smart_count: selectedIds.length,
name: translate(`resources.${resource}.forcedCaseName`, {
smart_count: selectedIds.length,
_: inflection.humanize(
_: humanize(
translate(`resources.${resource}.name`, {
smart_count: selectedIds.length,
_: resource
? inflection.inflect(
resource,
selectedIds.length
)
? inflect(resource, selectedIds.length)
: undefined,
}),
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Fragment, ReactEventHandler, ReactElement } from 'react';
import PropTypes from 'prop-types';
import ActionDelete from '@mui/icons-material/Delete';
import clsx from 'clsx';
import inflection from 'inflection';

import { UseMutationOptions } from '@tanstack/react-query';
import {
MutationMode,
Expand All @@ -17,6 +17,7 @@ import {

import { Confirm } from '../layout';
import { Button, ButtonProps } from './Button';
import { humanize, singularize } from 'inflection';

export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
props: DeleteWithConfirmButtonProps<RecordType>
Expand Down Expand Up @@ -76,12 +77,10 @@ export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
translateOptions={{
name: translate(`resources.${resource}.forcedCaseName`, {
smart_count: 1,
_: inflection.humanize(
_: humanize(
translate(`resources.${resource}.name`, {
smart_count: 1,
_: resource
? inflection.singularize(resource)
: undefined,
_: resource ? singularize(resource) : undefined,
}),
true
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { Fragment, useState, ReactElement } from 'react';
import PropTypes from 'prop-types';
import ActionUpdate from '@mui/icons-material/Update';
import inflection from 'inflection';

import { alpha, styled } from '@mui/material/styles';
import {
useTranslate,
Expand All @@ -19,6 +19,7 @@ import { Confirm } from '../layout';
import { Button, ButtonProps } from './Button';
import { BulkActionProps } from '../types';
import { UseMutationOptions } from '@tanstack/react-query';
import { humanize, inflect } from 'inflection';

export const UpdateWithConfirmButton = (
props: UpdateWithConfirmButtonProps
Expand Down Expand Up @@ -125,12 +126,10 @@ export const UpdateWithConfirmButton = (
smart_count: 1,
name: translate(`resources.${resource}.forcedCaseName`, {
smart_count: 1,
_: inflection.humanize(
_: humanize(
translate(`resources.${resource}.name`, {
smart_count: 1,
_: resource
? inflection.inflect(resource, 1)
: undefined,
_: resource ? inflect(resource, 1) : undefined,
}),
true
),
Expand Down
7 changes: 3 additions & 4 deletions packages/ra-ui-materialui/src/detail/EditGuesser.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { useEffect, useState } from 'react';
import inflection from 'inflection';

import {
EditBase,
InferredElement,
Expand All @@ -13,6 +13,7 @@ import {
import { EditProps } from '../types';
import { EditView } from './EditView';
import { editFieldTypes } from './editFieldTypes';
import { capitalize, singularize } from 'inflection';

export const EditGuesser = <RecordType extends RaRecord = RaRecord>(
props: EditProps<RecordType> & { enableLog?: boolean }
Expand Down Expand Up @@ -94,9 +95,7 @@ const EditViewGuesser = (

import { ${components.join(', ')} } from 'react-admin';

export const ${inflection.capitalize(
inflection.singularize(resource)
)}Edit = () => (
export const ${capitalize(singularize(resource))}Edit = () => (
<Edit>
${representation}
</Edit>
Expand Down
Loading
Loading