Skip to content

Commit

Permalink
Remove any types
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto authored and JulienMattiussi committed Aug 24, 2020
1 parent bc8cc47 commit 20c33de
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 59 deletions.
20 changes: 7 additions & 13 deletions packages/ra-ui-materialui/src/form/SimpleFormIterator.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('<SimpleFormIterator />', () => {
const { getByText } = renderWithRedux(
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator translate={x => x}>
<SimpleFormIterator>
<TextInput source="email" />
</SimpleFormIterator>
</ArrayInput>
Expand All @@ -40,7 +40,7 @@ describe('<SimpleFormIterator />', () => {
const { queryAllByText } = renderWithRedux(
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator translate={x => x} disableAdd>
<SimpleFormIterator disableAdd>
<TextInput source="email" />
</SimpleFormIterator>
</ArrayInput>
Expand All @@ -60,7 +60,7 @@ describe('<SimpleFormIterator />', () => {
}}
>
<ArrayInput source="emails">
<SimpleFormIterator translate={x => x} disableRemove>
<SimpleFormIterator disableRemove>
<TextInput source="email" />
</SimpleFormIterator>
</ArrayInput>
Expand All @@ -79,7 +79,7 @@ describe('<SimpleFormIterator />', () => {
} = renderWithRedux(
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator translate={x => x}>
<SimpleFormIterator>
<TextInput source="email" />
</SimpleFormIterator>
</ArrayInput>
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('<SimpleFormIterator />', () => {
} = renderWithRedux(
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator translate={x => x}>
<SimpleFormIterator>
<TextInput source="email" label="CustomLabel" />
</SimpleFormIterator>
</ArrayInput>
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('<SimpleFormIterator />', () => {
} = renderWithRedux(
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator translate={x => x}>
<SimpleFormIterator>
<TextInput
source="email"
label="CustomLabel"
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('<SimpleFormIterator />', () => {
<ThemeProvider theme={theme}>
<SimpleForm record={{ id: 'whatever', emails }}>
<ArrayInput source="emails">
<SimpleFormIterator translate={x => x}>
<SimpleFormIterator>
<TextInput source="email" />
</SimpleFormIterator>
</ArrayInput>
Expand Down Expand Up @@ -242,7 +242,6 @@ describe('<SimpleFormIterator />', () => {
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator
translate={x => x}
addButton={<button>Custom Add Button</button>}
>
<TextInput source="email" />
Expand All @@ -261,7 +260,6 @@ describe('<SimpleFormIterator />', () => {
>
<ArrayInput source="emails">
<SimpleFormIterator
translate={x => x}
removeButton={<button>Custom Remove Button</button>}
>
<TextInput source="email" />
Expand All @@ -279,7 +277,6 @@ describe('<SimpleFormIterator />', () => {
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator
translate={x => x}
addButton={<button>Custom Add Button</button>}
>
<TextInput source="email" />
Expand All @@ -299,7 +296,6 @@ describe('<SimpleFormIterator />', () => {
>
<ArrayInput source="emails">
<SimpleFormIterator
translate={x => x}
removeButton={<button>Custom Remove Button</button>}
>
<TextInput source="email" />
Expand All @@ -319,7 +315,6 @@ describe('<SimpleFormIterator />', () => {
<SimpleForm>
<ArrayInput source="emails">
<SimpleFormIterator
translate={x => x}
addButton={
<button onClick={onClick}>
Custom Add Button
Expand All @@ -345,7 +340,6 @@ describe('<SimpleFormIterator />', () => {
>
<ArrayInput source="emails">
<SimpleFormIterator
translate={x => x}
removeButton={
<button onClick={onClick}>
Custom Remove Button
Expand Down
32 changes: 29 additions & 3 deletions packages/ra-ui-materialui/src/form/SimpleFormIterator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import FormHelperText from '@material-ui/core/FormHelperText';
import { makeStyles } from '@material-ui/core/styles';
import CloseIcon from '@material-ui/icons/RemoveCircleOutline';
import AddIcon from '@material-ui/icons/AddCircleOutline';
import { useTranslate, ValidationError } from 'ra-core';
import { useTranslate, ValidationError, Record } from 'ra-core';
import classNames from 'classnames';
import { FieldArrayRenderProps } from 'react-final-form-arrays';

import FormInput from './FormInput';

Expand Down Expand Up @@ -92,7 +93,7 @@ const DefaultRemoveButton = props => {
);
};

const SimpleFormIterator: FC<any> = props => {
const SimpleFormIterator: FC<SimpleFormIteratorProps> = props => {
const {
addButton = <DefaultAddButton />,
removeButton = <DefaultRemoveButton />,
Expand Down Expand Up @@ -175,7 +176,7 @@ const SimpleFormIterator: FC<any> = props => {
<ul className={classes.root}>
{submitFailed && typeof error !== 'object' && error && (
<FormHelperText error>
<ValidationError error={error} />
<ValidationError error={error as string} />
</FormHelperText>
)}
<TransitionGroup component={null}>
Expand Down Expand Up @@ -290,8 +291,10 @@ SimpleFormIterator.propTypes = {
children: PropTypes.node,
classes: PropTypes.object,
className: PropTypes.string,
// @ts-ignore
fields: PropTypes.object,
meta: PropTypes.object,
// @ts-ignore
record: PropTypes.object,
source: PropTypes.string,
resource: PropTypes.string,
Expand All @@ -301,4 +304,27 @@ SimpleFormIterator.propTypes = {
TransitionProps: PropTypes.shape({}),
};

type DisableRemoveFunction = (record: Record) => boolean;

export interface SimpleFormIteratorProps
extends Partial<Omit<FieldArrayRenderProps<any, HTMLElement>, 'meta'>> {
addButton?: ReactElement;
basePath?: string;
defaultValue?: any;
disableAdd?: boolean;
disableRemove?: boolean | DisableRemoveFunction;
margin?: 'none' | 'normal' | 'dense';
meta?: {
// the type defined in FieldArrayRenderProps says error is boolean, which is wrong.
error?: any;
submitFailed?: boolean;
};
record?: Record;
removeButton?: ReactElement;
resource?: string;
source?: string;
TransitionProps?: any;
variant?: 'standard' | 'outlined' | 'filled';
}

export default SimpleFormIterator;
109 changes: 66 additions & 43 deletions packages/ra-ui-materialui/src/form/TabbedForm.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import * as React from 'react';
import { Children, isValidElement, FC, ReactElement } from 'react';
import {
Children,
isValidElement,
FC,
ReactElement,
HtmlHTMLAttributes,
} from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Route, useRouteMatch, useLocation } from 'react-router-dom';
import Divider from '@material-ui/core/Divider';
import { makeStyles } from '@material-ui/core/styles';
import { escapePath, FormWithRedirect } from 'ra-core';
import {
escapePath,
FormWithRedirect,
Record,
RedirectionSideEffect,
} from 'ra-core';
import { FormProps, FormRenderProps } from 'react-final-form';
import get from 'lodash/get';

import Toolbar from './Toolbar';
import TabbedFormTabs, { getTabFullPath } from './TabbedFormTabs';
import { ClassesOverride } from '../types';

/**
* Form layout where inputs are divided by tab, one input per line.
Expand Down Expand Up @@ -78,7 +91,7 @@ import TabbedFormTabs, { getTabFullPath } from './TabbedFormTabs';
*
* @param {Prop} props
*/
const TabbedForm: FC<any> = props => (
const TabbedForm: FC<TabbedFormProps> = props => (
<FormWithRedirect
{...props}
render={formProps => <TabbedFormView {...formProps} />}
Expand All @@ -87,8 +100,8 @@ const TabbedForm: FC<any> = props => (

TabbedForm.propTypes = {
children: PropTypes.node,
defaultValue: PropTypes.oneOfType([PropTypes.object, PropTypes.func]), // @deprecated
initialValues: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
// @ts-ignore
record: PropTypes.object,
redirect: PropTypes.oneOfType([
PropTypes.string,
Expand All @@ -103,6 +116,25 @@ TabbedForm.propTypes = {
sanitizeEmptyValues: PropTypes.bool,
};

export interface TabbedFormProps
extends Omit<FormProps, 'onSubmit'>,
Omit<HtmlHTMLAttributes<HTMLFormElement>, 'onSubmit' | 'children'> {
basePath?: string;
className?: string;
initialValues?: any;
margin?: 'none' | 'normal' | 'dense';
record?: Record;
redirect?: RedirectionSideEffect;
resource?: string;
sanitizeEmptyValues?: boolean;
submitOnEnter?: boolean;
tabs?: ReactElement;
toolbar?: ReactElement;
undoable?: boolean;
variant?: 'standard' | 'outlined' | 'filled';
warnWhenUnsavedChanges?: boolean;
}

const useStyles = makeStyles(
theme => ({
errorTabButton: { color: theme.palette.error.main },
Expand All @@ -115,7 +147,7 @@ const useStyles = makeStyles(
{ name: 'RaTabbedForm' }
);

export const TabbedFormView = props => {
export const TabbedFormView: FC<TabbedFormViewProps> = props => {
const {
basePath,
children,
Expand All @@ -130,13 +162,10 @@ export const TabbedFormView = props => {
redirect: defaultRedirect,
resource,
saving,
setRedirect,
submitOnEnter,
tabs,
toolbar,
translate,
undoable,
value,
variant,
margin,
...rest
Expand Down Expand Up @@ -227,6 +256,7 @@ TabbedFormView.propTypes = {
location: PropTypes.object,
match: PropTypes.object,
pristine: PropTypes.bool,
// @ts-ignore
record: PropTypes.object,
redirect: PropTypes.oneOfType([
PropTypes.string,
Expand All @@ -253,57 +283,50 @@ TabbedFormView.defaultProps = {
toolbar: <Toolbar />,
};

export interface TabbedFormViewProps extends FormRenderProps {
basePath?: string;
classes?: ClassesOverride<typeof useStyles>;
className?: string;
margin?: 'none' | 'normal' | 'dense';
handleSubmitWithRedirect?: (redirectTo: RedirectionSideEffect) => void;
record?: Record;
redirect?: RedirectionSideEffect;
resource?: string;
save?: () => void;
saving?: boolean;
tabs?: ReactElement;
toolbar?: ReactElement;
undoable?: boolean;
variant?: 'standard' | 'outlined' | 'filled';
submitOnEnter?: boolean;
__versions?: any; // react-final-form internal prop, missing in their type
}

const sanitizeRestProps = ({
anyTouched,
array,
asyncBlurFields,
asyncValidate,
asyncValidating,
autofill,
blur,
change,
clearAsyncError,
clearFields,
clearSubmit,
clearSubmitErrors,
destroy,
active,
dirty,
dirtyFields,
dirtyFieldsSinceLastSubmit,
dirtySinceLastSubmit,
dispatch,
form,
handleSubmit,
error,
errors,
hasSubmitErrors,
hasValidationErrors,
initialize,
initialized,
initialValues,
modified = null,
modifiedSinceLastSubmit,
modifiedsincelastsubmit,
pristine,
pure,
redirect,
reset,
resetSection,
save,
staticContext,
submit,
submitAsSideEffect,
save = null,
submitError,
submitErrors,
submitFailed,
submitSucceeded,
submitting,
touch,
translate,
triggerSubmit,
undoable,
untouch,
touched = null,
valid,
validate,
validating,
__versions,
values,
visited = null,
__versions = null,
...props
}) => props;

Expand Down

0 comments on commit 20c33de

Please sign in to comment.