angularngx-formly

Angular Material 15: How can I apply the panelClass property to `<mat-select>` using ngx-formly?


I'd like to style the dropdown panel of Angular Material <mat-select> with a specific width. We're using formly to generate forms in the project.

This is how an example configuration of a select field looks like:

      form: new FormGroup({}),
      options: {} as FormlyFormOptions,
      fields: [
        {
          key: 'actionSelect',
          type: 'select',
          props: {
            attributes: {
              panelClass: 'example-panel-class',
            },
            translate: true,
            required: true,
            options: [
              { value: 'exampleValue-1', label: 'exampleLabel-1' },
              { value: 'exampleValue-2', label: 'exampleLabel-2' },
              { value: 'exampleValue-3', label: 'exampleLabel-3' },
            ],
          },
        },
      ]

I tried to add Angular Material panelClass via the attributes property, as seen above but it does not lead to the expected behaviour of adding the css class example-panel-class to the dropdown panel.


Solution

  • May be I am late to the party, but for those who are still looking for answers -

    You can add panelClass prop directly in the FormlyFieldConfig like given below -

        {
          key: 'select',
          type: 'select',
          props: {
            label: 'Select',
            placeholder: 'Select placeholder',
            required: true,
            options: [
              { label: 'Option 1', value: '1' },
              { label: 'Option 2', value: '2' },
              { label: 'Option 3', value: '3' },
            ],
            panelClass: 'test-class',
          },
        },
    

    Stackblitz demo