Skip to content

Commit e858b84

Browse files
committed
feat(component): add transformErrors props
1 parent 6e5f00a commit e858b84

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

lib/forms/component.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
cleanData,
2828
getSchemaFromConfig,
2929
isMutationConfig,
30-
transformErrors,
3130
ApolloFormConfig,
3231
ReactJsonschemaFormError
3332
} from './utils';
@@ -59,6 +58,7 @@ export type ApolloFormProps<T> = {
5958
ui?: UiSchema & ApolloFormUi;
6059
children?: React.SFC<ApolloRenderProps>;
6160
liveValidate?: boolean;
61+
transformErrors?: (formName: string) => (errors: ReactJsonschemaFormError[]) => ReactJsonschemaFormError[];
6262
};
6363

6464
export interface ApolloFormState {
@@ -323,7 +323,11 @@ export function configure<MutationNamesType = {}>(opts: ApolloFormConfigureOptio
323323
theme={theme}
324324
onChange={this.onChange}
325325
save={this.save}
326-
transformErrors={transformErrors}
326+
transformErrors={
327+
this.props.transformErrors ?
328+
this.props.transformErrors :
329+
undefined
330+
}
327331
config={this.props.config}
328332
ui={this.props.ui}
329333
liveValidate={this.props.liveValidate}

lib/forms/renderers.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ export interface FormRendererProps {
6969
// tslint:disable-next-line:no-any
7070
save: (data: any) => void;
7171
// tslint:disable-next-line:no-any
72-
transformErrors: any;
7372
config: ApolloFormConfig;
7473
schema: JSONSchema6;
7574
data: object;
7675
isDirty: boolean;
7776
ui?: UiSchema & ApolloFormUi;
7877
subTitle?: string;
7978
liveValidate?: boolean;
79+
// tslint:disable-next-line:no-any
80+
transformErrors?: any;
8081
}
8182

8283
export interface FormContext {
@@ -117,7 +118,9 @@ export class FormRenderer extends React.Component<FormRendererProps> {
117118
// tslint:disable-next-line:no-any
118119
{...{ ErrorList: props.ui ? props.ui.errorListComponent : undefined } as any}
119120
transformErrors={
120-
props.transformErrors(formContext.formPrefix)
121+
props.transformErrors ?
122+
props.transformErrors(formContext.formPrefix) :
123+
undefined
121124
}
122125
>
123126
{this.props.children}

lib/forms/utils.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,6 @@ export type ReactJsonschemaFormError = {
196196
property: string;
197197
stack: string;
198198
};
199-
export const transformErrors = (prefix: string) => (errors: ReactJsonschemaFormError[]) => {
200-
return errors.map(error => ({
201-
...error,
202-
message: `FormError.${prefix}${error.property}.${error.name}`
203-
// message: t(`FormError.${prefix}${error.property}.${error.name}`)
204-
}));
205-
};
206199

207200
// Given formData and form properties, remove all formData properties not present in form properties
208201
export const cleanData = (formData: object, properties: object, parentPath: string | null = null): object => {

stories/index.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
// tslint:disable-next-line:no-unused-variable
22
import { storiesOf } from '@storybook/react';
3-
import { schema, types } from 'functional-json-schema';
4-
import { graphqlSync, introspectionQuery, DocumentNode, IntrospectionQuery } from 'graphql';
3+
import { graphqlSync, introspectionQuery, IntrospectionQuery } from 'graphql';
54
import { fromIntrospectionQuery } from 'graphql-2-json-schema';
65
import gql from 'graphql-tag';
76
import { JSONSchema6 } from 'json-schema';
87
import { keys } from 'lodash';
98
import * as React from 'react';
10-
import { ApolloConsumer, Mutation } from 'react-apollo';
9+
import { ApolloConsumer } from 'react-apollo';
1110
import { FieldProps } from 'react-jsonschema-form';
1211
import { schema as mockSchema } from '../graphql-mock';
1312
import { configure, ApolloFormConfigureTheme, ErrorListComponent } from '../lib/forms/component';
14-
import { ApolloFormBuilder } from '../lib/forms/definitions';
13+
import { ReactJsonschemaFormError } from '../lib/forms/utils';
1514
const { Button, Input, Checkbox, Header, Form, Message } = require('semantic-ui-react');
1615
const { withKnobs, select, boolean: bool } = require('@storybook/addon-knobs/react');
1716

@@ -34,10 +33,17 @@ const ErrorList: ErrorListComponent = p => (
3433
/>
3534
);
3635

36+
const transformErrors = (prefix: string) => (errors: ReactJsonschemaFormError[]) => {
37+
return errors.map(error => ({
38+
...error,
39+
message: `FormError.${prefix}${error.property}.${error.name}`
40+
}));
41+
};
42+
3743
const theme: ApolloFormConfigureTheme = {
3844
templates: {
3945
FieldTemplate: props => {
40-
const { classNames, help, description, errors, children, rawErrors, label } = props;
46+
const { description, children, label } = props;
4147
return (
4248
<Form.Field>
4349
<label>{label}{props.required && '*'}</label>
@@ -125,6 +131,7 @@ storiesOf('ApolloForm', module)
125131
}
126132
}
127133
}}
134+
transformErrors={transformErrors}
128135
>
129136
{
130137
form => (
@@ -196,6 +203,7 @@ storiesOf('ApolloForm', module)
196203
}
197204
}
198205
}}
206+
transformErrors={transformErrors}
199207
>
200208
{
201209
form => (

0 commit comments

Comments
 (0)