@@ -45,6 +45,7 @@ const stringToPath = (string) => {
|
45 | 45 | }; |
46 | 46 | |
47 | 47 | const keysCache: { [string]: string[] } = {}; |
| 48 | +const keysRegex = /[.[\]]+/; |
48 | 49 | |
49 | 50 | const toPath = (key: string): string[] => { |
50 | 51 | if (key === null || key === undefined || !key.length) { |
@@ -54,7 +55,26 @@ const toPath = (key: string): string[] => {
|
54 | 55 | throw new Error("toPath() expects a string"); |
55 | 56 | } |
56 | 57 | if (keysCache[key] == null) { |
57 | | -keysCache[key] = stringToPath(key); |
| 58 | +/** |
| 59 | + * The following patch fixes issue 456, introduced since v4.20.3: |
| 60 | + * |
| 61 | + * Before v4.20.3, i.e. in v4.20.2, a `key` like 'choices[]' would map to ['choices'] |
| 62 | + * (e.g. an array of choices used where 'choices[]' is name attribute of an input of type checkbox). |
| 63 | + * |
| 64 | + * Since v4.20.3, a `key` like 'choices[]' would map to ['choices', ''] which is wrong and breaks |
| 65 | + * this kind of inputs e.g. in React. |
| 66 | + * |
| 67 | + * v4.20.3 introduced an unwanted breaking change, this patch fixes it, see the issue at the link below. |
| 68 | + * |
| 69 | + * @see https://github.com/final-form/final-form/issues/456 |
| 70 | + */ |
| 71 | +if (key.endsWith("[]")) { |
| 72 | +// v4.20.2 (a `key` like 'choices[]' should map to ['choices'], which is fine). |
| 73 | +keysCache[key] = key.split(keysRegex).filter(Boolean); |
| 74 | +} else { |
| 75 | +// v4.20.3 (a `key` like 'choices[]' maps to ['choices', ''], which breaks applications relying on inputs like `<input type="checkbox" name="choices[]" />`). |
| 76 | +keysCache[key] = stringToPath(key); |
| 77 | +} |
58 | 78 | } |
59 | 79 | return keysCache[key]; |
60 | 80 | }; |
|