Skip to content

Commit

Permalink
[Chore] Reorganize ra-core/form directory
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Nov 20, 2024
1 parent 75f547f commit 74dcf49
Show file tree
Hide file tree
Showing 37 changed files with 63 additions and 100 deletions.
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/Form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CoreAdminContext } from '../core';
import { Form } from './Form';
import { useNotificationContext } from '../notification';
import { useInput } from './useInput';
import { required } from './validate';
import { required } from './validation/validate';
import {
FormLevelValidation,
InputLevelValidation,
Expand Down
3 changes: 1 addition & 2 deletions packages/ra-core/src/form/Form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { Route, Routes, useNavigate, Link, HashRouter } from 'react-router-dom';
import { CoreAdminContext } from '../core';
import { Form } from './Form';
import { useInput } from './useInput';
import { required } from './validate';
import ValidationError from './ValidationError';
import { required, ValidationError } from './validation';
import { mergeTranslations } from '../i18n';
import { I18nProvider } from '../types';
import { SaveContextProvider, useNotificationContext } from '..';
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
UNSAFE_DataRouterStateContext,
} from 'react-router';

import { FormGroupsProvider } from './FormGroupsProvider';
import { FormGroupsProvider } from './groups/FormGroupsProvider';
import { RaRecord } from '../types';
import {
useRecordContext,
Expand All @@ -23,7 +23,7 @@ import {
SourceContextValue,
useResourceContext,
} from '../core';
import { ValidateForm } from './getSimpleValidationResolver';
import { ValidateForm } from './validation/getSimpleValidationResolver';
import { WarnWhenUnsavedChanges } from './WarnWhenUnsavedChanges';
import { useAugmentedForm } from './useAugmentedForm';

Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/FormDataConsumer.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { render, waitFor, screen, fireEvent } from '@testing-library/react';

import FormDataConsumer, { FormDataConsumerView } from './FormDataConsumer';
import { FormDataConsumer, FormDataConsumerView } from './FormDataConsumer';
import { testDataProvider } from '../dataProvider';
import {
AdminContext,
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-core/src/form/FormDataConsumer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import { useWrappedSource } from '../core';
* </Edit>
* );
*/
const FormDataConsumer = <TFieldValues extends FieldValues = FieldValues>(
export const FormDataConsumer = <
TFieldValues extends FieldValues = FieldValues,
>(
props: ConnectedProps<TFieldValues>
) => {
const form = useFormContext<TFieldValues>();
Expand Down Expand Up @@ -83,8 +85,6 @@ export const FormDataConsumerView = <
return ret === undefined ? null : ret;
};

export default FormDataConsumer;

const ArraySourceRegex = new RegExp(/.+\.\d+$/);

export interface FormDataConsumerRenderParams<
Expand Down
1 change: 1 addition & 0 deletions packages/ra-core/src/form/choices/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './ChoicesContext';
export * from './ChoicesContextProvider';
export * from './useChoicesContext';
export * from './useChoices';
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import expect from 'expect';
import { render, screen } from '@testing-library/react';

import { useChoices } from './useChoices';
import { TestTranslationProvider } from '../i18n';
import { useRecordContext } from '../controller';
import { TestTranslationProvider } from '../../i18n';
import { useRecordContext } from '../../controller';

describe('useChoices hook', () => {
const defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as React from 'react';
import { ReactElement, isValidElement, useCallback } from 'react';
import get from 'lodash/get';

import { useTranslate } from '../i18n';
import { RaRecord } from '../types';
import { RecordContextProvider } from '../controller';
import { useTranslate } from '../../i18n';
import { RaRecord } from '../../types';
import { RecordContextProvider } from '../../controller';

export type OptionTextElement = ReactElement<{
record: RaRecord;
Expand Down
6 changes: 6 additions & 0 deletions packages/ra-core/src/form/groups/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './FormGroupContext';
export * from './FormGroupContextProvider';
export * from './FormGroupsProvider';
export * from './useFormGroup';
export * from './useFormGroups';
export * from './useFormGroupContext';
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import expect from 'expect';
import { FormGroupContextProvider } from './FormGroupContextProvider';
import { testDataProvider } from '../dataProvider';
import { ResourceContextProvider } from '..';
import { testDataProvider } from '../../dataProvider';
import { ResourceContextProvider } from '../..';

describe('useFormGroup', () => {
test.each([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import get from 'lodash/get';
import isEqual from 'lodash/isEqual';
import { useFormState } from 'react-hook-form';
import { useFormGroups } from './useFormGroups';
import { useEvent } from '../util';
import { useEvent } from '../../util';

type FieldState = {
name: string;
Expand Down
File renamed without changes.
45 changes: 3 additions & 42 deletions packages/ra-core/src/form/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
import FormDataConsumer, {
FormDataConsumerRender,
FormDataConsumerRenderParams,
} from './FormDataConsumer';
import {
FormGroupsContext,
FormGroupsContextValue,
FormGroupSubscriber,
} from './FormGroupsContext';
import ValidationError, { ValidationErrorProps } from './ValidationError';
import {
getSimpleValidationResolver,
ValidateForm,
} from './getSimpleValidationResolver';

export type {
FormGroupsContextValue,
FormGroupSubscriber,
FormDataConsumerRender,
FormDataConsumerRenderParams,
ValidationErrorProps,
ValidateForm,
};

export {
FormDataConsumer,
FormGroupsContext,
ValidationError,
getSimpleValidationResolver,
};
export * from './choices';
export * from './Form';
export * from './validate';
export * from './FormGroupContext';
export * from './FormGroupContextProvider';
export * from './FormGroupsProvider';
export * from './setSubmissionErrors';
export * from './FormDataConsumer';
export * from './groups';
export * from './useApplyInputDefaultValues';
export * from './useChoices';
export * from './useFormGroup';
export * from './useFormGroups';
export * from './useFormGroupContext';
export * from './useGetValidationErrorMessage';
export * from './useNotifyIsFormInvalid';
export * from './useAugmentedForm';
export * from './useInput';
export * from './useSuggestions';
export * from './useUnique';
export * from './useWarnWhenUnsavedChanges';
export * from './validation';
export * from './WarnWhenUnsavedChanges';
11 changes: 0 additions & 11 deletions packages/ra-core/src/form/isRequired.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/ra-core/src/form/useAugmentedForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import getFormInitialValues from './getFormInitialValues';
import {
getSimpleValidationResolver,
ValidateForm,
} from './getSimpleValidationResolver';
import { setSubmissionErrors } from './setSubmissionErrors';
import { useNotifyIsFormInvalid } from './useNotifyIsFormInvalid';
} from './validation/getSimpleValidationResolver';
import { setSubmissionErrors } from './validation/setSubmissionErrors';
import { useNotifyIsFormInvalid } from './validation/useNotifyIsFormInvalid';
import { sanitizeEmptyValues as sanitizeValues } from './sanitizeEmptyValues';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/useInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CoreAdminContext, SourceContextProvider } from '../core';
import { testDataProvider } from '../dataProvider';
import { Form } from './Form';
import { useInput, InputProps, UseInputValue } from './useInput';
import { required } from './validate';
import { required } from './validation/validate';

const Input: FunctionComponent<
{
Expand Down
6 changes: 2 additions & 4 deletions packages/ra-core/src/form/useInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import {
import get from 'lodash/get';

import { useRecordContext } from '../controller';
import { composeValidators, Validator } from './validate';
import isRequired from './isRequired';
import { useFormGroupContext } from './useFormGroupContext';
import { useFormGroups } from './useFormGroups';
import { composeValidators, Validator, isRequired } from './validation';
import { useFormGroupContext, useFormGroups } from './groups';
import { useApplyInputDefaultValues } from './useApplyInputDefaultValues';
import { useEvent } from '../util';
import { useWrappedSource } from '../core';
Expand Down
6 changes: 5 additions & 1 deletion packages/ra-core/src/form/useSuggestions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useCallback, isValidElement, ReactElement } from 'react';
import set from 'lodash/set';
import { useChoices, OptionText, UseChoicesOptions } from './useChoices';
import {
useChoices,
OptionText,
UseChoicesOptions,
} from './choices/useChoices';
import { useTranslate } from '../i18n';

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import { render } from '@testing-library/react';

import ValidationError from './ValidationError';
import { TestTranslationProvider } from '../i18n';
import { ValidationError } from './ValidationError';
import { TestTranslationProvider } from '../../i18n';

const translate = jest.fn(key => {
return key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
ValidationErrorMessage,
ValidationErrorMessageWithArgs,
} from './validate';
import { useTranslate } from '../i18n';
import { useTranslate } from '../../i18n';

export interface ValidationErrorProps {
error: ValidationErrorMessage;
}

const ValidationErrorSpecialFormatPrefix = '@@react-admin@@';
const ValidationError = (props: ValidationErrorProps) => {
export const ValidationError = (props: ValidationErrorProps) => {
const { error } = props;
let errorMessage = error;
const translate = useTranslate();
Expand All @@ -35,5 +35,3 @@ const ValidationError = (props: ValidationErrorProps) => {

return <>{translate(errorMessage as string, { _: errorMessage })}</>;
};

export default ValidationError;
7 changes: 7 additions & 0 deletions packages/ra-core/src/form/validation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './getSimpleValidationResolver';
export * from './setSubmissionErrors';
export * from './useGetValidationErrorMessage';
export * from './useNotifyIsFormInvalid';
export * from './useUnique';
export * from './validate';
export * from './ValidationError';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ValidationErrorMessage,
ValidationErrorMessageWithArgs,
} from './validate';
import { useTranslate } from '../i18n';
import { useTranslate } from '../../i18n';

/**
* @deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef } from 'react';
import { useFormState, Control } from 'react-hook-form';
import { useNotify } from '../notification';
import { useNotify } from '../../notification';

/**
* This hook display an error message on submit in Form and SaveButton.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
WithAdditionalFilters,
WithMessage,
} from './useUnique.stories';
import { testDataProvider } from '../dataProvider';
import { DataProvider } from '../types';
import { testDataProvider } from '../../dataProvider';
import { DataProvider } from '../../types';

describe('useUnique', () => {
const baseDataProvider = (overrides?: Partial<DataProvider>) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as React from 'react';
import fakerestDataProvider from 'ra-data-fakerest';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from 'ra-language-english';
import { Form } from './Form';
import { useInput } from './useInput';
import { Form } from '../Form';
import { useInput } from '../useInput';
import {
CoreAdminContext,
CreateBase,
Expand All @@ -13,9 +13,9 @@ import {
ValidationError,
mergeTranslations,
useUnique,
} from '..';
} from '../..';
import { QueryClient } from '@tanstack/react-query';
import { TestMemoryRouter } from '../routing';
import { TestMemoryRouter } from '../../routing';

export default {
title: 'ra-core/form/useUnique',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import merge from 'lodash/merge';
import { useResourceContext } from '../core';
import { useDataProvider } from '../dataProvider';
import { useTranslate, useTranslateLabel } from '../i18n';
import { InputProps } from './useInput';
import { useCallback, useRef } from 'react';
import merge from 'lodash/merge';
import set from 'lodash/set';
import { asyncDebounce } from '../util';
import { useRecordContext } from '../controller';
import { useResourceContext } from '../../core';
import { useDataProvider } from '../../dataProvider';
import { useTranslate, useTranslateLabel } from '../../i18n';
import { asyncDebounce } from '../../util';
import { useRecordContext } from '../../controller';
import { InputProps } from '../useInput';
import { isEmpty } from './validate';

/**
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 74dcf49

Please sign in to comment.