Skip to content

Commit 9060c8d

Browse files
authored
refactor(fields): expose static method toFieldErrors (#2141)
1 parent 5a7573e commit 9060c8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+916
-37
lines changed

.changeset/seven-icons-smell.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
'@commercetools-uikit/async-creatable-select-field': patch
3+
'@commercetools-uikit/async-select-field': patch
4+
'@commercetools-uikit/creatable-select-field': patch
5+
'@commercetools-uikit/date-field': patch
6+
'@commercetools-uikit/date-range-field': patch
7+
'@commercetools-uikit/date-time-field': patch
8+
'@commercetools-uikit/localized-multiline-text-field': patch
9+
'@commercetools-uikit/localized-text-field': patch
10+
'@commercetools-uikit/money-field': patch
11+
'@commercetools-uikit/multiline-text-field': patch
12+
'@commercetools-uikit/number-field': patch
13+
'@commercetools-uikit/password-field': patch
14+
'@commercetools-uikit/radio-field': patch
15+
'@commercetools-uikit/search-select-field': patch
16+
'@commercetools-uikit/select-field': patch
17+
'@commercetools-uikit/text-field': patch
18+
'@commercetools-uikit/time-field': patch
19+
---
20+
21+
Expose static method `toFieldErrors` for each `*Field` component.
22+
23+
Use this function to convert the Formik `errors` object type to our custom field errors type. This is primarily useful when using TypeScript.
24+
25+
```ts
26+
type FormValues = {
27+
myField: string,
28+
};
29+
30+
<TextField
31+
// ...
32+
name="my-field"
33+
errors={
34+
TextField.toFieldErrors<FormValues>(formik.errors).myField
35+
}
36+
/>
37+
```

packages/components/fields/async-creatable-select-field/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,23 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
152152
Known error keys are:
153153

154154
- `missing`: tells the user that this field is required
155+
156+
## Static methods
157+
158+
### `AsyncCreatableSelectField.toFieldErrors`
159+
160+
Use this function to convert the Formik `errors` object type to our custom field errors type. This is primarily useful when using TypeScript.
161+
162+
```ts
163+
type FormValues = {
164+
myField: string;
165+
};
166+
167+
<AsyncCreatableSelectField
168+
// ...
169+
name="my-field"
170+
errors={
171+
AsyncCreatableSelectField.toFieldErrors<FormValues>(formik.errors).myField
172+
}
173+
/>;
174+
```

packages/components/fields/async-creatable-select-field/docs/additional-info.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,23 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
1818
Known error keys are:
1919

2020
- `missing`: tells the user that this field is required
21+
22+
## Static methods
23+
24+
### `AsyncCreatableSelectField.toFieldErrors`
25+
26+
Use this function to convert the Formik `errors` object type to our custom field errors type. This is primarily useful when using TypeScript.
27+
28+
```ts
29+
type FormValues = {
30+
myField: string;
31+
};
32+
33+
<AsyncCreatableSelectField
34+
// ...
35+
name="my-field"
36+
errors={
37+
AsyncCreatableSelectField.toFieldErrors<FormValues>(formik.errors).myField
38+
}
39+
/>;
40+
```

packages/components/fields/async-creatable-select-field/src/async-creatable-select-field.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ type ReactSelectAsyncCreatableProps = AsyncCreatableProps<
2424
GroupBase<unknown>
2525
>;
2626

27-
type TFieldErrors = Record<string, boolean>;
28-
2927
type TErrorRenderer = (key: string, error?: boolean) => ReactNode;
28+
type TFieldErrors = Record<string, boolean>;
29+
// Similar shape of `FormikErrors` but values are `TFieldErrors` objects.
30+
type TCustomFormErrors<Values> = {
31+
[K in keyof Values]?: TFieldErrors;
32+
};
3033

3134
const sequentialId = createSequentialId('async-creatable-select-field-');
3235
const sequentialErrorsId = createSequentialId(
@@ -386,6 +389,17 @@ export default class AsyncCreatableSelectField extends Component<
386389
id: getFieldId(props, state, sequentialId),
387390
});
388391

392+
/**
393+
* Use this function to convert the Formik `errors` object type to
394+
* our custom field errors type.
395+
* This is primarly useful when using TypeScript.
396+
*/
397+
static toFieldErrors<FormValues>(
398+
errors: unknown
399+
): TCustomFormErrors<FormValues> {
400+
return errors as TCustomFormErrors<FormValues>;
401+
}
402+
389403
render() {
390404
const hasError =
391405
AsyncCreatableSelectInput.isTouched(this.props.touched) &&

packages/components/fields/async-select-field/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,21 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
145145
Known error keys are:
146146

147147
- `missing`: tells the user that this field is required
148+
149+
## Static methods
150+
151+
### `AsyncSelectField.toFieldErrors`
152+
153+
Use this function to convert the Formik `errors` object type to our custom field errors type. This is primarily useful when using TypeScript.
154+
155+
```ts
156+
type FormValues = {
157+
myField: string;
158+
};
159+
160+
<AsyncSelectField
161+
// ...
162+
name="my-field"
163+
errors={AsyncSelectField.toFieldErrors<FormValues>(formik.errors).myField}
164+
/>;
165+
```

packages/components/fields/async-select-field/docs/additional-info.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
1818
Known error keys are:
1919

2020
- `missing`: tells the user that this field is required
21+
22+
## Static methods
23+
24+
### `AsyncSelectField.toFieldErrors`
25+
26+
Use this function to convert the Formik `errors` object type to our custom field errors type. This is primarily useful when using TypeScript.
27+
28+
```ts
29+
type FormValues = {
30+
myField: string;
31+
};
32+
33+
<AsyncSelectField
34+
// ...
35+
name="my-field"
36+
errors={AsyncSelectField.toFieldErrors<FormValues>(formik.errors).myField}
37+
/>;
38+
```

packages/components/fields/async-select-field/src/async-select-field.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import FieldErrors from '@commercetools-uikit/field-errors';
2222
type ReactSelectAsyncProps = AsyncProps<unknown, boolean, GroupBase<unknown>>;
2323
type TErrorRenderer = (key: string, error?: boolean) => ReactNode;
2424
type TFieldErrors = Record<string, boolean>;
25+
// Similar shape of `FormikErrors` but values are `TFieldErrors` objects.
26+
type TCustomFormErrors<Values> = {
27+
[K in keyof Values]?: TFieldErrors;
28+
};
2529

2630
type TEvent = {
2731
target: {
@@ -331,6 +335,17 @@ export default class AsyncSelectField extends Component<
331335
id: getFieldId(props, state, sequentialId),
332336
});
333337

338+
/**
339+
* Use this function to convert the Formik `errors` object type to
340+
* our custom field errors type.
341+
* This is primarly useful when using TypeScript.
342+
*/
343+
static toFieldErrors<FormValues>(
344+
errors: unknown
345+
): TCustomFormErrors<FormValues> {
346+
return errors as TCustomFormErrors<FormValues>;
347+
}
348+
334349
render() {
335350
const hasError =
336351
AsyncSelectInput.isTouched(this.props.touched) &&

packages/components/fields/creatable-select-field/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,21 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
147147
Known error keys are:
148148

149149
- `missing`: tells the user that this field is required
150+
151+
## Static methods
152+
153+
### `CreatableSelectField.toFieldErrors`
154+
155+
Use this function to convert the Formik `errors` object type to our custom field errors type. This is primarily useful when using TypeScript.
156+
157+
```ts
158+
type FormValues = {
159+
myField: string;
160+
};
161+
162+
<CreatableSelectField
163+
// ...
164+
name="my-field"
165+
errors={CreatableSelectField.toFieldErrors<FormValues>(formik.errors).myField}
166+
/>;
167+
```

packages/components/fields/creatable-select-field/docs/additional-info.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
1818
Known error keys are:
1919

2020
- `missing`: tells the user that this field is required
21+
22+
## Static methods
23+
24+
### `CreatableSelectField.toFieldErrors`
25+
26+
Use this function to convert the Formik `errors` object type to our custom field errors type. This is primarily useful when using TypeScript.
27+
28+
```ts
29+
type FormValues = {
30+
myField: string;
31+
};
32+
33+
<CreatableSelectField
34+
// ...
35+
name="my-field"
36+
errors={CreatableSelectField.toFieldErrors<FormValues>(formik.errors).myField}
37+
/>;
38+
```

packages/components/fields/creatable-select-field/src/creatable-select-field.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ type ReactSelectCreatableProps = CreatableProps<
2525
>;
2626
type TErrorRenderer = (key: string, error?: boolean) => ReactNode;
2727
type TFieldErrors = Record<string, boolean>;
28+
// Similar shape of `FormikErrors` but values are `TFieldErrors` objects.
29+
type TCustomFormErrors<Values> = {
30+
[K in keyof Values]?: TFieldErrors;
31+
};
2832
type TValue = {
2933
value: string;
3034
};
@@ -363,6 +367,17 @@ export default class CreatableSelectField extends Component<
363367
id: getFieldId(props, state, sequentialId),
364368
});
365369

370+
/**
371+
* Use this function to convert the Formik `errors` object type to
372+
* our custom field errors type.
373+
* This is primarly useful when using TypeScript.
374+
*/
375+
static toFieldErrors<FormValues>(
376+
errors: unknown
377+
): TCustomFormErrors<FormValues> {
378+
return errors as TCustomFormErrors<FormValues>;
379+
}
380+
366381
render() {
367382
const hasError =
368383
CreatableSelectInput.isTouched(this.props.touched) &&

0 commit comments

Comments
 (0)