cozy-ui-plus 4.1.0 → 4.2.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [4.2.0](https://github.com/cozy/cozy-libs/compare/cozy-ui-plus@4.1.0...cozy-ui-plus@4.2.0) (2026-01-14)
7
+
8
+ ### Features
9
+
10
+ - **AddModal:** Add possibility to set field default value ([db6e8f7](https://github.com/cozy/cozy-libs/commit/db6e8f723cf3c155765458c8e022005006403351))
11
+
6
12
  # [4.1.0](https://github.com/cozy/cozy-libs/compare/cozy-ui-plus@4.0.0...cozy-ui-plus@4.1.0) (2025-12-10)
7
13
 
8
14
  ### Features
@@ -13,8 +13,6 @@ var _uniqueId = _interopRequireDefault(require("lodash/uniqueId"));
13
13
 
14
14
  var _contact = require("cozy-client/dist/models/contact");
15
15
 
16
- var _fieldsConfig = require("./fieldsConfig");
17
-
18
16
  var _helpers = require("./helpers");
19
17
 
20
18
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -22,97 +20,84 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
22
20
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
23
21
 
24
22
  var contactToFormValues = function contactToFormValues(_ref) {
23
+ var _impp$find;
24
+
25
25
  var contact = _ref.contact,
26
26
  makeCustomContactValues = _ref.makeCustomContactValues,
27
27
  t = _ref.t;
28
-
29
- // initialize the form values, required so that array fields start with at least one editable field
30
- var initialFieldValues = _fieldsConfig.fields.reduce(function (initialValues, _ref2) {
31
- var name = _ref2.name,
32
- layout = _ref2.layout;
33
- initialValues[name] = layout === 'array' ? [undefined] : undefined;
34
- return initialValues;
35
- }, {});
36
-
37
- if (contact) {
38
- var _impp$find;
39
-
40
- var gender = contact.gender,
41
- address = contact.address,
42
- birthday = contact.birthday,
43
- birthplace = contact.birthplace,
44
- company = contact.company,
45
- jobTitle = contact.jobTitle,
46
- cozy = contact.cozy,
47
- email = contact.email,
48
- impp = contact.impp,
49
- name = contact.name,
50
- note = contact.note,
51
- phone = contact.phone;
52
- var addressValue = address && address.length > 0 ? (0, _helpers.movePrimaryToHead)(address).map(function (addressInfo) {
53
- var _addressInfo$extended, _addressInfo$extended2, _addressInfo$extended3, _addressInfo$extended4, _addressInfo$extended5, _addressInfo$extended6;
54
-
55
- return {
56
- fieldId: (0, _uniqueId.default)('fieldId_'),
57
- address: (0, _contact.getFormattedAddress)(addressInfo, t),
58
- addressnumber: addressInfo.number,
59
- addressstreet: addressInfo.street || (0, _contact.getFormattedAddress)(addressInfo, t),
60
- addresscode: addressInfo.postcode || addressInfo.code,
61
- addresscity: addressInfo.city,
62
- addressregion: addressInfo.region,
63
- addresscountry: addressInfo.country,
64
- addresslocality: (_addressInfo$extended = addressInfo.extendedAddress) === null || _addressInfo$extended === void 0 ? void 0 : _addressInfo$extended.locality,
65
- addressbuilding: (_addressInfo$extended2 = addressInfo.extendedAddress) === null || _addressInfo$extended2 === void 0 ? void 0 : _addressInfo$extended2.building,
66
- addressstairs: (_addressInfo$extended3 = addressInfo.extendedAddress) === null || _addressInfo$extended3 === void 0 ? void 0 : _addressInfo$extended3.stairs,
67
- addressfloor: (_addressInfo$extended4 = addressInfo.extendedAddress) === null || _addressInfo$extended4 === void 0 ? void 0 : _addressInfo$extended4.floor,
68
- addressapartment: (_addressInfo$extended5 = addressInfo.extendedAddress) === null || _addressInfo$extended5 === void 0 ? void 0 : _addressInfo$extended5.apartment,
69
- addressentrycode: (_addressInfo$extended6 = addressInfo.extendedAddress) === null || _addressInfo$extended6 === void 0 ? void 0 : _addressInfo$extended6.entrycode,
70
- addressLabel: (0, _helpers.makeItemLabel)(addressInfo)
71
- };
72
- }) : [undefined];
73
- var cozyValue = cozy && cozy.length > 0 ? cozy[0].url : undefined;
74
- var cozyLabel = cozy && cozy.length > 0 ? (0, _helpers.makeItemLabel)(cozy[0]) : undefined;
75
- var emailValue = email && email.length > 0 ? (0, _helpers.movePrimaryToHead)(email).map(function (item) {
76
- return {
77
- fieldId: (0, _uniqueId.default)('fieldId_'),
78
- email: item === null || item === void 0 ? void 0 : item.address,
79
- emailLabel: (0, _helpers.makeItemLabel)(item)
80
- };
81
- }) : [undefined];
82
- var matrixValue = impp && impp.length > 0 ? ((_impp$find = impp.find(function (item) {
83
- return item.label === 'work' && item.protocol === 'matrix';
84
- })) === null || _impp$find === void 0 ? void 0 : _impp$find.uri) || undefined : undefined;
85
- var phoneValue = phone && phone.length > 0 ? (0, _helpers.movePrimaryToHead)(phone).map(function (item) {
86
- return {
87
- fieldId: (0, _uniqueId.default)('fieldId_'),
88
- phone: item === null || item === void 0 ? void 0 : item.number,
89
- phoneLabel: (0, _helpers.makeItemLabel)(item)
90
- };
91
- }) : [undefined];
92
- var relatedContactValue = (0, _helpers.makeRelatedContact)(contact);
93
- var customValues = (makeCustomContactValues === null || makeCustomContactValues === void 0 ? void 0 : makeCustomContactValues(contact)) || {};
94
- return _objectSpread(_objectSpread({}, customValues), {}, {
95
- gender: gender,
96
- givenName: name === null || name === void 0 ? void 0 : name.givenName,
97
- additionalName: name === null || name === void 0 ? void 0 : name.additionalName,
98
- surname: name === null || name === void 0 ? void 0 : name.surname,
99
- familyName: name === null || name === void 0 ? void 0 : name.familyName,
100
- phone: phoneValue,
101
- email: emailValue,
102
- matrix: matrixValue,
103
- address: addressValue,
104
- cozy: cozyValue,
105
- cozyLabel: cozyLabel,
106
- company: company,
107
- jobTitle: jobTitle,
108
- birthday: birthday,
109
- birthplace: birthplace,
110
- note: note,
111
- relatedContact: relatedContactValue
112
- });
113
- }
114
-
115
- return initialFieldValues;
28
+ var gender = contact.gender,
29
+ address = contact.address,
30
+ birthday = contact.birthday,
31
+ birthplace = contact.birthplace,
32
+ company = contact.company,
33
+ jobTitle = contact.jobTitle,
34
+ cozy = contact.cozy,
35
+ email = contact.email,
36
+ impp = contact.impp,
37
+ name = contact.name,
38
+ note = contact.note,
39
+ phone = contact.phone;
40
+ var addressValue = address && address.length > 0 ? (0, _helpers.movePrimaryToHead)(address).map(function (addressInfo) {
41
+ var _addressInfo$extended, _addressInfo$extended2, _addressInfo$extended3, _addressInfo$extended4, _addressInfo$extended5, _addressInfo$extended6;
42
+
43
+ return {
44
+ fieldId: (0, _uniqueId.default)('fieldId_'),
45
+ address: (0, _contact.getFormattedAddress)(addressInfo, t),
46
+ addressnumber: addressInfo.number,
47
+ addressstreet: addressInfo.street || (0, _contact.getFormattedAddress)(addressInfo, t),
48
+ addresscode: addressInfo.postcode || addressInfo.code,
49
+ addresscity: addressInfo.city,
50
+ addressregion: addressInfo.region,
51
+ addresscountry: addressInfo.country,
52
+ addresslocality: (_addressInfo$extended = addressInfo.extendedAddress) === null || _addressInfo$extended === void 0 ? void 0 : _addressInfo$extended.locality,
53
+ addressbuilding: (_addressInfo$extended2 = addressInfo.extendedAddress) === null || _addressInfo$extended2 === void 0 ? void 0 : _addressInfo$extended2.building,
54
+ addressstairs: (_addressInfo$extended3 = addressInfo.extendedAddress) === null || _addressInfo$extended3 === void 0 ? void 0 : _addressInfo$extended3.stairs,
55
+ addressfloor: (_addressInfo$extended4 = addressInfo.extendedAddress) === null || _addressInfo$extended4 === void 0 ? void 0 : _addressInfo$extended4.floor,
56
+ addressapartment: (_addressInfo$extended5 = addressInfo.extendedAddress) === null || _addressInfo$extended5 === void 0 ? void 0 : _addressInfo$extended5.apartment,
57
+ addressentrycode: (_addressInfo$extended6 = addressInfo.extendedAddress) === null || _addressInfo$extended6 === void 0 ? void 0 : _addressInfo$extended6.entrycode,
58
+ addressLabel: (0, _helpers.makeItemLabel)(addressInfo)
59
+ };
60
+ }) : [undefined];
61
+ var cozyValue = cozy && cozy.length > 0 ? cozy[0].url : undefined;
62
+ var cozyLabel = cozy && cozy.length > 0 ? (0, _helpers.makeItemLabel)(cozy[0]) : undefined;
63
+ var emailValue = email && email.length > 0 ? (0, _helpers.movePrimaryToHead)(email).map(function (item) {
64
+ return {
65
+ fieldId: (0, _uniqueId.default)('fieldId_'),
66
+ email: item === null || item === void 0 ? void 0 : item.address,
67
+ emailLabel: (0, _helpers.makeItemLabel)(item)
68
+ };
69
+ }) : [undefined];
70
+ var matrixValue = impp && impp.length > 0 ? ((_impp$find = impp.find(function (item) {
71
+ return item.label === 'work' && item.protocol === 'matrix';
72
+ })) === null || _impp$find === void 0 ? void 0 : _impp$find.uri) || undefined : undefined;
73
+ var phoneValue = phone && phone.length > 0 ? (0, _helpers.movePrimaryToHead)(phone).map(function (item) {
74
+ return {
75
+ fieldId: (0, _uniqueId.default)('fieldId_'),
76
+ phone: item === null || item === void 0 ? void 0 : item.number,
77
+ phoneLabel: (0, _helpers.makeItemLabel)(item)
78
+ };
79
+ }) : [undefined];
80
+ var relatedContactValue = (0, _helpers.makeRelatedContact)(contact);
81
+ var customValues = (makeCustomContactValues === null || makeCustomContactValues === void 0 ? void 0 : makeCustomContactValues(contact)) || {};
82
+ return _objectSpread(_objectSpread({}, customValues), {}, {
83
+ gender: gender,
84
+ givenName: name === null || name === void 0 ? void 0 : name.givenName,
85
+ additionalName: name === null || name === void 0 ? void 0 : name.additionalName,
86
+ surname: name === null || name === void 0 ? void 0 : name.surname,
87
+ familyName: name === null || name === void 0 ? void 0 : name.familyName,
88
+ phone: phoneValue,
89
+ email: emailValue,
90
+ matrix: matrixValue,
91
+ address: addressValue,
92
+ cozy: cozyValue,
93
+ cozyLabel: cozyLabel,
94
+ company: company,
95
+ jobTitle: jobTitle,
96
+ birthday: birthday,
97
+ birthplace: birthplace,
98
+ note: note,
99
+ relatedContact: relatedContactValue
100
+ });
116
101
  };
117
102
 
118
103
  var _default = contactToFormValues;
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.validateFields = exports.removeRelatedContactRelationships = exports.removeField = exports.removeAsscociatedData = exports.moveToHead = exports.movePrimaryToHead = exports.makeTypeAndLabel = exports.makeRelatedContact = exports.makeItemLabel = exports.makeIsRequiredError = exports.makeInitialCustomValue = exports.makeImppValues = exports.makeFields = exports.makeCustomLabel = exports.hasNoValues = exports.hasExtendedAddress = exports.getRelatedContactRelationships = exports.getFirstValueIfArray = exports.createAddress = exports.addField = void 0;
8
+ exports.validateFields = exports.removeRelatedContactRelationships = exports.removeField = exports.removeAsscociatedData = exports.moveToHead = exports.movePrimaryToHead = exports.makeTypeAndLabel = exports.makeRelatedContact = exports.makeItemLabel = exports.makeIsRequiredError = exports.makeInitialCustomValue = exports.makeImppValues = exports.makeFields = exports.makeCustomLabel = exports.initializationFieldValues = exports.hasNoValues = exports.hasExtendedAddress = exports.getRelatedContactRelationships = exports.getFirstValueIfArray = exports.createAddress = exports.addField = void 0;
9
9
 
10
10
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
11
 
@@ -428,6 +428,19 @@ var makeImppValues = function makeImppValues(oldContact, value) {
428
428
  }
429
429
  }).filter(Boolean)) || [];
430
430
  };
431
+
432
+ exports.makeImppValues = makeImppValues;
433
+
434
+ var initializationFieldValues = function initializationFieldValues(fields) {
435
+ var initialFieldValues = fields.reduce(function (initialValues, _ref8) {
436
+ var name = _ref8.name,
437
+ layout = _ref8.layout,
438
+ defaultValue = _ref8.defaultValue;
439
+ initialValues[name] = layout === 'array' ? [undefined] : defaultValue;
440
+ return initialValues;
441
+ }, {});
442
+ return initialFieldValues;
443
+ };
431
444
  /**
432
445
  *
433
446
  * @param {string} name
@@ -436,7 +449,7 @@ var makeImppValues = function makeImppValues(oldContact, value) {
436
449
  */
437
450
 
438
451
 
439
- exports.makeImppValues = makeImppValues;
452
+ exports.initializationFieldValues = initializationFieldValues;
440
453
 
441
454
  var makeInitialCustomValue = function makeInitialCustomValue(name, value) {
442
455
  // gender input doesn't support custom label
@@ -98,7 +98,7 @@ var ContactForm = function ContactForm(_ref) {
98
98
 
99
99
  return /*#__PURE__*/_react.default.createElement(_reactFinalForm.Form, {
100
100
  mutators: _objectSpread({}, _finalFormArrays.default),
101
- initialValues: (0, _contactToFormValues.default)({
101
+ initialValues: !contact ? (0, _helpers.initializationFieldValues)(_fields) : (0, _contactToFormValues.default)({
102
102
  contact: contact,
103
103
  makeCustomContactValues: makeCustomContactValues,
104
104
  t: t
@@ -33,5 +33,6 @@ export function makeImppValues(oldContact: import("cozy-client/types/types").IOC
33
33
  label: string;
34
34
  primary?: boolean;
35
35
  }[];
36
+ export function initializationFieldValues(fields: any): any;
36
37
  export function makeInitialCustomValue(name: string, value: string): string;
37
38
  export function makeFields(customFields: (import("../types").Field)[] | undefined, defaultFields: (import("../types").Field)[]): (import("../types").Field)[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-ui-plus",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "Cozy-ui-plus is an extension of cozy-ui and provides components based on cozy-client",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -74,5 +74,5 @@
74
74
  "react-dom": "^16 || ^17 || ^18",
75
75
  "twake-i18n": ">=0.3.0"
76
76
  },
77
- "gitHead": "463474b57578fbca2e4fa04165e94abb82627d03"
77
+ "gitHead": "1fe28398fc8315ebb5eba401731f8e318c00f05a"
78
78
  }
@@ -2,108 +2,93 @@ import uniqueId from 'lodash/uniqueId'
2
2
 
3
3
  import { getFormattedAddress } from 'cozy-client/dist/models/contact'
4
4
 
5
- import { fields } from './fieldsConfig'
6
5
  import { makeItemLabel, makeRelatedContact, movePrimaryToHead } from './helpers'
7
6
 
8
7
  const contactToFormValues = ({ contact, makeCustomContactValues, t }) => {
9
- // initialize the form values, required so that array fields start with at least one editable field
10
- const initialFieldValues = fields.reduce(
11
- (initialValues, { name, layout }) => {
12
- initialValues[name] = layout === 'array' ? [undefined] : undefined
13
- return initialValues
14
- },
15
- {}
16
- )
8
+ const {
9
+ gender,
10
+ address,
11
+ birthday,
12
+ birthplace,
13
+ company,
14
+ jobTitle,
15
+ cozy,
16
+ email,
17
+ impp,
18
+ name,
19
+ note,
20
+ phone
21
+ } = contact
17
22
 
18
- if (contact) {
19
- const {
20
- gender,
21
- address,
22
- birthday,
23
- birthplace,
24
- company,
25
- jobTitle,
26
- cozy,
27
- email,
28
- impp,
29
- name,
30
- note,
31
- phone
32
- } = contact
23
+ const addressValue =
24
+ address && address.length > 0
25
+ ? movePrimaryToHead(address).map(addressInfo => ({
26
+ fieldId: uniqueId('fieldId_'),
27
+ address: getFormattedAddress(addressInfo, t),
28
+ addressnumber: addressInfo.number,
29
+ addressstreet:
30
+ addressInfo.street || getFormattedAddress(addressInfo, t),
31
+ addresscode: addressInfo.postcode || addressInfo.code,
32
+ addresscity: addressInfo.city,
33
+ addressregion: addressInfo.region,
34
+ addresscountry: addressInfo.country,
35
+ addresslocality: addressInfo.extendedAddress?.locality,
36
+ addressbuilding: addressInfo.extendedAddress?.building,
37
+ addressstairs: addressInfo.extendedAddress?.stairs,
38
+ addressfloor: addressInfo.extendedAddress?.floor,
39
+ addressapartment: addressInfo.extendedAddress?.apartment,
40
+ addressentrycode: addressInfo.extendedAddress?.entrycode,
41
+ addressLabel: makeItemLabel(addressInfo)
42
+ }))
43
+ : [undefined]
44
+ const cozyValue = cozy && cozy.length > 0 ? cozy[0].url : undefined
45
+ const cozyLabel = cozy && cozy.length > 0 ? makeItemLabel(cozy[0]) : undefined
46
+ const emailValue =
47
+ email && email.length > 0
48
+ ? movePrimaryToHead(email).map(item => ({
49
+ fieldId: uniqueId('fieldId_'),
50
+ email: item?.address,
51
+ emailLabel: makeItemLabel(item)
52
+ }))
53
+ : [undefined]
54
+ const matrixValue =
55
+ impp && impp.length > 0
56
+ ? impp.find(item => item.label === 'work' && item.protocol === 'matrix')
57
+ ?.uri || undefined
58
+ : undefined
59
+ const phoneValue =
60
+ phone && phone.length > 0
61
+ ? movePrimaryToHead(phone).map(item => ({
62
+ fieldId: uniqueId('fieldId_'),
63
+ phone: item?.number,
64
+ phoneLabel: makeItemLabel(item)
65
+ }))
66
+ : [undefined]
33
67
 
34
- const addressValue =
35
- address && address.length > 0
36
- ? movePrimaryToHead(address).map(addressInfo => ({
37
- fieldId: uniqueId('fieldId_'),
38
- address: getFormattedAddress(addressInfo, t),
39
- addressnumber: addressInfo.number,
40
- addressstreet:
41
- addressInfo.street || getFormattedAddress(addressInfo, t),
42
- addresscode: addressInfo.postcode || addressInfo.code,
43
- addresscity: addressInfo.city,
44
- addressregion: addressInfo.region,
45
- addresscountry: addressInfo.country,
46
- addresslocality: addressInfo.extendedAddress?.locality,
47
- addressbuilding: addressInfo.extendedAddress?.building,
48
- addressstairs: addressInfo.extendedAddress?.stairs,
49
- addressfloor: addressInfo.extendedAddress?.floor,
50
- addressapartment: addressInfo.extendedAddress?.apartment,
51
- addressentrycode: addressInfo.extendedAddress?.entrycode,
52
- addressLabel: makeItemLabel(addressInfo)
53
- }))
54
- : [undefined]
55
- const cozyValue = cozy && cozy.length > 0 ? cozy[0].url : undefined
56
- const cozyLabel =
57
- cozy && cozy.length > 0 ? makeItemLabel(cozy[0]) : undefined
58
- const emailValue =
59
- email && email.length > 0
60
- ? movePrimaryToHead(email).map(item => ({
61
- fieldId: uniqueId('fieldId_'),
62
- email: item?.address,
63
- emailLabel: makeItemLabel(item)
64
- }))
65
- : [undefined]
66
- const matrixValue =
67
- impp && impp.length > 0
68
- ? impp.find(item => item.label === 'work' && item.protocol === 'matrix')
69
- ?.uri || undefined
70
- : undefined
71
- const phoneValue =
72
- phone && phone.length > 0
73
- ? movePrimaryToHead(phone).map(item => ({
74
- fieldId: uniqueId('fieldId_'),
75
- phone: item?.number,
76
- phoneLabel: makeItemLabel(item)
77
- }))
78
- : [undefined]
68
+ const relatedContactValue = makeRelatedContact(contact)
79
69
 
80
- const relatedContactValue = makeRelatedContact(contact)
70
+ const customValues = makeCustomContactValues?.(contact) || {}
81
71
 
82
- const customValues = makeCustomContactValues?.(contact) || {}
83
-
84
- return {
85
- ...customValues,
86
- gender,
87
- givenName: name?.givenName,
88
- additionalName: name?.additionalName,
89
- surname: name?.surname,
90
- familyName: name?.familyName,
91
- phone: phoneValue,
92
- email: emailValue,
93
- matrix: matrixValue,
94
- address: addressValue,
95
- cozy: cozyValue,
96
- cozyLabel: cozyLabel,
97
- company,
98
- jobTitle,
99
- birthday,
100
- birthplace,
101
- note,
102
- relatedContact: relatedContactValue
103
- }
72
+ return {
73
+ ...customValues,
74
+ gender,
75
+ givenName: name?.givenName,
76
+ additionalName: name?.additionalName,
77
+ surname: name?.surname,
78
+ familyName: name?.familyName,
79
+ phone: phoneValue,
80
+ email: emailValue,
81
+ matrix: matrixValue,
82
+ address: addressValue,
83
+ cozy: cozyValue,
84
+ cozyLabel: cozyLabel,
85
+ company,
86
+ jobTitle,
87
+ birthday,
88
+ birthplace,
89
+ note,
90
+ relatedContact: relatedContactValue
104
91
  }
105
-
106
- return initialFieldValues
107
92
  }
108
93
 
109
94
  export default contactToFormValues
@@ -5,29 +5,6 @@ import { johnDoeContact, johnDoeFormValues } from '../mocks'
5
5
  describe('contactToFormValues function', () => {
6
6
  const tSpy = jest.fn()
7
7
 
8
- it('should return default initial values', () => {
9
- const expected = {
10
- address: [undefined],
11
- birthday: undefined,
12
- birthplace: undefined,
13
- gender: undefined,
14
- company: undefined,
15
- jobTitle: undefined,
16
- cozy: undefined,
17
- cozyLabel: undefined,
18
- email: [undefined],
19
- matrix: undefined,
20
- familyName: undefined,
21
- givenName: undefined,
22
- note: undefined,
23
- phone: [undefined],
24
- relatedContact: [undefined]
25
- }
26
-
27
- const result = contactToFormValues({ contact: null, t: tSpy })
28
- expect(result).toEqual(expected)
29
- })
30
-
31
8
  it('should return initial values when a contact is given (edit)', () => {
32
9
  const expected = { ...johnDoeFormValues, relatedContact: [undefined] }
33
10
  tSpy.mockReturnValue(
@@ -366,6 +366,18 @@ export const makeImppValues = (oldContact, value) => {
366
366
  )
367
367
  }
368
368
 
369
+ export const initializationFieldValues = fields => {
370
+ const initialFieldValues = fields.reduce(
371
+ (initialValues, { name, layout, defaultValue }) => {
372
+ initialValues[name] = layout === 'array' ? [undefined] : defaultValue
373
+ return initialValues
374
+ },
375
+ {}
376
+ )
377
+
378
+ return initialFieldValues
379
+ }
380
+
369
381
  /**
370
382
  *
371
383
  * @param {string} name
@@ -10,7 +10,12 @@ import FieldInputLayout from './FieldInputLayout'
10
10
  import contactToFormValues from './contactToFormValues'
11
11
  import { fields as defaultFields } from './fieldsConfig'
12
12
  import formValuesToContact from './formValuesToContact'
13
- import { validateFields, makeFields, hasNoValues } from './helpers'
13
+ import {
14
+ validateFields,
15
+ makeFields,
16
+ hasNoValues,
17
+ initializationFieldValues
18
+ } from './helpers'
14
19
  import { locales } from './locales'
15
20
  // import { fullContactPropTypes } from '../../ContactPropTypes' // !!
16
21
 
@@ -50,11 +55,15 @@ const ContactForm = ({ contacts, contact, customFieldsProps, onSubmit }) => {
50
55
  return (
51
56
  <Form
52
57
  mutators={{ ...arrayMutators }}
53
- initialValues={contactToFormValues({
54
- contact,
55
- makeCustomContactValues,
56
- t
57
- })}
58
+ initialValues={
59
+ !contact
60
+ ? initializationFieldValues(_fields)
61
+ : contactToFormValues({
62
+ contact,
63
+ makeCustomContactValues,
64
+ t
65
+ })
66
+ }
58
67
  validate={values => validateFields(values, _fields, t)}
59
68
  onSubmit={formValues => {
60
69
  // avoid creating an empty contact