Skip to content

Commit

Permalink
use named imports with inflection
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammedFaragallah committed May 1, 2024
1 parent dd1c079 commit 99a55ae
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 64 deletions.
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 * as 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 * as 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
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 * as 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 * as 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 * as 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 * as 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
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 * as 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 * as 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 * as 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 * as 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 * as 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
8 changes: 4 additions & 4 deletions packages/ra-ui-materialui/src/detail/ShowGuesser.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 * as inflection from 'inflection';

import {
ShowBase,
InferredElement,
Expand All @@ -13,6 +13,8 @@ import { ShowProps } from '../types';
import { ShowView } from './ShowView';
import { showFieldTypes } from './showFieldTypes';

import { capitalize, singularize } from 'inflection';

export const ShowGuesser = ({
id,
queryOptions,
Expand Down Expand Up @@ -73,9 +75,7 @@ const ShowViewGuesser = (
import { ${components.join(', ')} } from 'react-admin';
export const ${inflection.capitalize(
inflection.singularize(resource)
)}Show = () => (
export const ${capitalize(singularize(resource))}Show = () => (
<Show>
${inferredChild.getRepresentation()}
</Show>
Expand Down
Loading

0 comments on commit 99a55ae

Please sign in to comment.