envoc-form 5.0.6 → 5.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3981,7 +3981,7 @@ ___
3981
3981
 
3982
3982
  ### FieldProps
3983
3983
 
3984
- Ƭ **FieldProps**<`TForm`, `TProp`, `TRenderComponent`\>: { `Component`: [`RenderComponent`](#rendercomponent)<`TForm`[`TProp`], `TRenderComponent`\> ; `disabled?`: `boolean` ; `id?`: `string` ; `name`: `TProp` ; `normalize?`: [`NormalizationFunction`](#interfacesnormalizationfunctionmd)<`TForm`[`TProp`]\> ; `validate?`: [`ValidationFunction`](#interfacesvalidationfunctionmd)<`TForm`[`TProp`]\> \| [`ValidationFunction`](#interfacesvalidationfunctionmd)<`TForm`[`TProp`]\>[] } & `Omit`<[`RenderComponentProps`](#rendercomponentprops)<`TForm`[`TProp`], `TRenderComponent`\>, keyof [`InjectedFieldProps`](#interfacesinjectedfieldpropsmd)<`TForm`[`TProp`]\>\>
3984
+ Ƭ **FieldProps**<`TForm`, `TProp`, `TRenderComponent`\>: { `Component`: [`RenderComponent`](#rendercomponent)<`TForm`[`TProp`], `TRenderComponent`\> ; `disabled?`: `boolean` ; `id?`: `string` ; `name`: `TProp` ; `normalize?`: [`NormalizationFunction`](#interfacesnormalizationfunctionmd)<`TForm`[`TProp`]\> ; `required?`: `boolean` ; `validate?`: [`ValidationFunction`](#interfacesvalidationfunctionmd)<`TForm`[`TProp`]\> \| [`ValidationFunction`](#interfacesvalidationfunctionmd)<`TForm`[`TProp`]\>[] } & `Omit`<[`RenderComponentProps`](#rendercomponentprops)<`TForm`[`TProp`], `TRenderComponent`\>, keyof [`InjectedFieldProps`](#interfacesinjectedfieldpropsmd)<`TForm`[`TProp`]\>\>
3985
3985
 
3986
3986
  A specific Field instance to be rendered by the given TRenderComponent or by whatever default is reasonable
3987
3987
 
@@ -4384,7 +4384,7 @@ Should no Component be used then the default will be provided by the default loo
4384
4384
 
4385
4385
  #### Defined in
4386
4386
 
4387
- packages/envoc-form/src/Field/Field.tsx:60
4387
+ packages/envoc-form/src/Field/Field.tsx:61
4388
4388
 
4389
4389
  ___
4390
4390
 
@@ -18,11 +18,12 @@ export type FieldProps<TForm extends object, TProp extends keyof TForm, TRenderC
18
18
  validate?: ValidationFunction<TForm[TProp]> | ValidationFunction<TForm[TProp]>[];
19
19
  /** Function to modify the field value without making the form dirty. (e.g. phone number) */
20
20
  normalize?: NormalizationFunction<TForm[TProp]>;
21
+ required?: boolean;
21
22
  } & Omit<RenderComponentProps<TForm[TProp], TRenderComponent>, keyof InjectedFieldProps<TForm[TProp]>>;
22
23
  /**
23
24
  * Renders whatever Component is passed - injecting the formik values needed to finish wiring up that individual field.
24
25
  * Should no Component be used then the default will be provided by the default lookup based on typeof(TForm[TProp])
25
26
  */
26
- declare function Field<TForm extends object, TProp extends keyof TForm, TRenderComponent extends ElementType>({ name, Component, id, disabled, validate, normalize, ...rest }: FieldProps<TForm, TProp, TRenderComponent>, ref: LegacyRef<any>): import("react/jsx-runtime").JSX.Element;
27
+ declare function Field<TForm extends object, TProp extends keyof TForm, TRenderComponent extends ElementType>({ name, Component, id, disabled, validate, normalize, required, ...rest }: FieldProps<TForm, TProp, TRenderComponent>, ref: LegacyRef<any>): import("react/jsx-runtime").JSX.Element;
27
28
  declare const _default: typeof Field;
28
29
  export default _default;
package/es/Field/Field.js CHANGED
@@ -30,18 +30,26 @@ import { required as requiredValidator } from '../Validation/validators';
30
30
  * Should no Component be used then the default will be provided by the default lookup based on typeof(TForm[TProp])
31
31
  */
32
32
  function Field(_a, ref) {
33
- var name = _a.name, Component = _a.Component, id = _a.id, disabled = _a.disabled, validate = _a.validate, normalize = _a.normalize, rest = __rest(_a, ["name", "Component", "id", "disabled", "validate", "normalize"]);
33
+ var name = _a.name, Component = _a.Component, id = _a.id, disabled = _a.disabled, validate = _a.validate, normalize = _a.normalize, required = _a.required, rest = __rest(_a, ["name", "Component", "id", "disabled", "validate", "normalize", "required"]);
34
+ // enforces that validate is an array and adds the required validator if required prop is true
35
+ var validateArray = Array.isArray(validate)
36
+ ? validate
37
+ : validate !== undefined
38
+ ? [validate]
39
+ : [];
40
+ var validateWithRequired = required
41
+ ? validateArray
42
+ .filter(function (x) { return x !== requiredValidator; })
43
+ .concat([requiredValidator])
44
+ : validateArray;
45
+ var isRequired = required || validateWithRequired.includes(requiredValidator);
34
46
  var _b = useStandardFormInput({
35
47
  name: String(name),
36
48
  id: id,
37
49
  disabled: disabled,
38
- validate: validate,
50
+ validate: validateWithRequired,
39
51
  normalize: normalize,
40
52
  }), input = _b[0], meta = _b[1];
41
- var isRequired = (rest === null || rest === void 0 ? void 0 : rest.required) !== undefined
42
- ? rest.required
43
- : validate === requiredValidator ||
44
- (Array.isArray(validate) && validate.includes(requiredValidator));
45
53
  // a bit of a hack so JSX is happy with us
46
54
  var Wrapped = Component;
47
55
  return (_jsx(FieldNameContext.Provider, { value: input.name, children: _jsx(Wrapped, __assign({}, rest, { ref: ref, id: input.id, input: input, meta: meta, required: isRequired, disabled: disabled })) }));
@@ -18,11 +18,12 @@ export type FieldProps<TForm extends object, TProp extends keyof TForm, TRenderC
18
18
  validate?: ValidationFunction<TForm[TProp]> | ValidationFunction<TForm[TProp]>[];
19
19
  /** Function to modify the field value without making the form dirty. (e.g. phone number) */
20
20
  normalize?: NormalizationFunction<TForm[TProp]>;
21
+ required?: boolean;
21
22
  } & Omit<RenderComponentProps<TForm[TProp], TRenderComponent>, keyof InjectedFieldProps<TForm[TProp]>>;
22
23
  /**
23
24
  * Renders whatever Component is passed - injecting the formik values needed to finish wiring up that individual field.
24
25
  * Should no Component be used then the default will be provided by the default lookup based on typeof(TForm[TProp])
25
26
  */
26
- declare function Field<TForm extends object, TProp extends keyof TForm, TRenderComponent extends ElementType>({ name, Component, id, disabled, validate, normalize, ...rest }: FieldProps<TForm, TProp, TRenderComponent>, ref: LegacyRef<any>): import("react/jsx-runtime").JSX.Element;
27
+ declare function Field<TForm extends object, TProp extends keyof TForm, TRenderComponent extends ElementType>({ name, Component, id, disabled, validate, normalize, required, ...rest }: FieldProps<TForm, TProp, TRenderComponent>, ref: LegacyRef<any>): import("react/jsx-runtime").JSX.Element;
27
28
  declare const _default: typeof Field;
28
29
  export default _default;
@@ -35,18 +35,26 @@ var validators_1 = require("../Validation/validators");
35
35
  * Should no Component be used then the default will be provided by the default lookup based on typeof(TForm[TProp])
36
36
  */
37
37
  function Field(_a, ref) {
38
- var name = _a.name, Component = _a.Component, id = _a.id, disabled = _a.disabled, validate = _a.validate, normalize = _a.normalize, rest = __rest(_a, ["name", "Component", "id", "disabled", "validate", "normalize"]);
38
+ var name = _a.name, Component = _a.Component, id = _a.id, disabled = _a.disabled, validate = _a.validate, normalize = _a.normalize, required = _a.required, rest = __rest(_a, ["name", "Component", "id", "disabled", "validate", "normalize", "required"]);
39
+ // enforces that validate is an array and adds the required validator if required prop is true
40
+ var validateArray = Array.isArray(validate)
41
+ ? validate
42
+ : validate !== undefined
43
+ ? [validate]
44
+ : [];
45
+ var validateWithRequired = required
46
+ ? validateArray
47
+ .filter(function (x) { return x !== validators_1.required; })
48
+ .concat([validators_1.required])
49
+ : validateArray;
50
+ var isRequired = required || validateWithRequired.includes(validators_1.required);
39
51
  var _b = (0, useStandardField_1.default)({
40
52
  name: String(name),
41
53
  id: id,
42
54
  disabled: disabled,
43
- validate: validate,
55
+ validate: validateWithRequired,
44
56
  normalize: normalize,
45
57
  }), input = _b[0], meta = _b[1];
46
- var isRequired = (rest === null || rest === void 0 ? void 0 : rest.required) !== undefined
47
- ? rest.required
48
- : validate === validators_1.required ||
49
- (Array.isArray(validate) && validate.includes(validators_1.required));
50
58
  // a bit of a hack so JSX is happy with us
51
59
  var Wrapped = Component;
52
60
  return ((0, jsx_runtime_1.jsx)(FieldNameContext_1.FieldNameContext.Provider, { value: input.name, children: (0, jsx_runtime_1.jsx)(Wrapped, __assign({}, rest, { ref: ref, id: input.id, input: input, meta: meta, required: isRequired, disabled: disabled })) }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "envoc-form",
3
- "version": "5.0.6",
3
+ "version": "5.0.7",
4
4
  "description": "Envoc form components",
5
5
  "keywords": [
6
6
  "react-component",
@@ -37,7 +37,7 @@
37
37
  "dependencies": {
38
38
  "axios": "^0.21.1",
39
39
  "date-fns": "^2.22.1",
40
- "envoc-request": "^5.0.6",
40
+ "envoc-request": "^5.0.7",
41
41
  "lru-cache": "^6.0.0",
42
42
  "prop-types": "^15.7.2",
43
43
  "react-date-picker": "^8.2.0",
@@ -48,6 +48,7 @@ export type FieldProps<
48
48
  | ValidationFunction<TForm[TProp]>[];
49
49
  /** Function to modify the field value without making the form dirty. (e.g. phone number) */
50
50
  normalize?: NormalizationFunction<TForm[TProp]>;
51
+ required?: boolean;
51
52
  } & Omit<
52
53
  RenderComponentProps<TForm[TProp], TRenderComponent>,
53
54
  keyof InjectedFieldProps<TForm[TProp]>
@@ -69,24 +70,37 @@ function Field<
69
70
  disabled,
70
71
  validate,
71
72
  normalize,
73
+ required,
72
74
  ...rest
73
75
  }: FieldProps<TForm, TProp, TRenderComponent>,
74
76
  ref: LegacyRef<any>
75
77
  ) {
78
+ // enforces that validate is an array and adds the required validator if required prop is true
79
+ const validateArray: ValidationFunction<TForm[TProp]>[] = Array.isArray(
80
+ validate
81
+ )
82
+ ? validate
83
+ : validate !== undefined
84
+ ? [validate]
85
+ : [];
86
+
87
+ const validateWithRequired = required
88
+ ? validateArray
89
+ .filter((x) => x !== requiredValidator)
90
+ .concat([requiredValidator])
91
+ : validateArray;
92
+
93
+ const isRequired =
94
+ required || validateWithRequired.includes(requiredValidator);
95
+
76
96
  const [input, meta] = useStandardFormInput<TForm[TProp]>({
77
97
  name: String(name),
78
98
  id: id,
79
99
  disabled: disabled,
80
- validate: validate,
100
+ validate: validateWithRequired,
81
101
  normalize: normalize,
82
102
  });
83
103
 
84
- const isRequired =
85
- rest?.required !== undefined
86
- ? rest.required
87
- : validate === requiredValidator ||
88
- (Array.isArray(validate) && validate.includes(requiredValidator));
89
-
90
104
  // a bit of a hack so JSX is happy with us
91
105
  const Wrapped = Component as React.ComponentType<
92
106
  InjectedFieldProps<TForm[TProp]>