Skip to content

Commit

Permalink
fix(core): improve parse array notation in expression property (#2924)
Browse files Browse the repository at this point in the history
fix #2916
  • Loading branch information
aitboudad committed Jul 10, 2021
1 parent ce1ca29 commit 05d714f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Expand Up @@ -559,12 +559,18 @@ describe('FieldExpressionExtension', () => {

it('should supports array notation in expression property', () => {
const fields: FormlyFieldConfig[] = [
{ expressionProperties: { 'model[0]': '"ddd"' } },
{
expressionProperties: {
'model[0]': '1',
'model["1"]': '2',
'model[\'2\']': '3',
},
},
];

const model = [];
builder.buildForm(form, fields, model, options);
expect(model).toEqual(['ddd']);
expect(model).toEqual([1, 2, 3]);
});

it('should throw error when assign to an undefined prop', () => {
Expand Down
Expand Up @@ -269,7 +269,13 @@ export class FieldExpressionExtension implements FormlyExtension {
private setExprValue(field: FormlyFieldConfigCache, prop: string, value: any) {
try {
let target = field;
const paths = (prop.indexOf('[') === -1 ? prop : prop.replace(/\[(\w+)\]/g, '.$1')).split('.');
const paths = prop.indexOf('[') === -1
? prop.split('.')
: prop
.replace(/\'|\"/g, '')
.split(/[[\]]{1,2}/) // https://stackoverflow.com/a/20198206
.filter(v => v)
;
const lastIndex = paths.length - 1;
for (let i = 0; i < lastIndex; i++) {
target = target[paths[i]];
Expand Down

0 comments on commit 05d714f

Please sign in to comment.