ngx-schema-form icon indicating copy to clipboard operation
ngx-schema-form copied to clipboard

Apply Custom Classes to Buttons

Open jpchip opened this issue 7 years ago • 13 comments

It would be nice if in the schema for the buttons I could assign them custom classes, like:

"buttons": [{
			"id": "save",
                        "class": "btn btn-primary"
			"label": "Save"
		},
                {
			"id": "cancel",
                        "class": "btn btn-default"
			"label": "Cancel"
		}
]

or is there some way to do that already that I'm totally missing?

jpchip avatar Mar 24 '17 04:03 jpchip

The best approach would be to allow to extend the action renderer so we would be free to change its layout (and possibly add classes or whatever we want). We would need something similar to field widget for actions.

ebrehault avatar Mar 26 '17 17:03 ebrehault

I was able to add classes through document.getElementsByTagName("button").classList.add("btn"); in ngAfterViewInit @jpchip

jrymer avatar Aug 02 '17 14:08 jrymer

  1. Open /node_modules/angular2-schema-form/dist/formelement.component.js in your project.

  2. Find a section: FormElementComponent.decorators = [ { type: Component, args: [{ selector: 'sf-form-element', template: "<div *ngIf=\"formProperty.visible\">\n\t<sf-widget-chooser\n\t(widgetInstanciated)=\"onWidgetInstanciated($event)\"\n\t[widgetInfo]=\"formProperty.schema.widget\">\n\t</sf-widget-chooser>\n\t<button *ngFor=\"let button of buttons\" (click)=\"button.action($event)\" >{{button.label}}</button>\n</div>" },] }, ];

  3. Add in button tag: [attr.class]=\"button.class\" [attr.id]=\"button.id\"

  4. After recompiling of your project you can use "class" in schema in "buttons" section. You can add more attributes to button tag in a similar way. Probably, need changes formelement.component.metadata.json but it works the same way.

AndzejChwalko avatar Aug 10 '17 06:08 AndzejChwalko

This should be easy to achieve. Your may define/override custom buttons in your WidgetRegistry. See README section "Render buttons"

daniele-pecora avatar Aug 29 '17 10:08 daniele-pecora

I've been trying to achieve this through a custom widget and for whatever reason it's not being picked up:

@Component({ selector: 'sf-button-widget', template: '<button class="btn btn-outline-primary">{{button.label}}</button>' }) export class NgxButtonComponent extends ButtonWidget { }

export class CustomButtonWidgetRegistry extends DefaultWidgetRegistry { constructor() { super(); this.register('button', NgxButtonComponent); } }

and then in my app.module: providers: [{ provide: WidgetRegistry, useClass: CustomButtonWidgetRegistry }]

I simply wish to override the default button to have my bootstrap styling

jpeterson88 avatar Feb 22 '19 17:02 jpeterson88

Did you add your component in your module entryComponents?

entryComponents: [NgxButtonComponent],

ebrehault avatar Feb 22 '19 17:02 ebrehault

@ebrehault yes I did, also to declarations:

declarations: [AppComponent, MockEditComponent, NgxButtonComponent], entryComponents: [NgxButtonComponent],

jpeterson88 avatar Feb 22 '19 17:02 jpeterson88

I do not see what's wrong in your code. Here is a working implementation: https://github.com/guillotinaweb/nsf-pastanaga/blob/master/projects/pastanaga/src/lib/registry.ts#L37

Try to compare with yours to find the missing thing.

ebrehault avatar Feb 22 '19 18:02 ebrehault

Thanks @ebrehault i'll have a look at this code, compare notes and report back

jpeterson88 avatar Feb 22 '19 18:02 jpeterson88

It looks like I'm doing everything this repo is doing. I do not have a separate component and bundled it all into AppComponent. Have a look: NGX Schema Form Test

jpeterson88 avatar Feb 22 '19 18:02 jpeterson88

After reviewing that repo more thoroughly, I have a question...if I override the button, will that actually override the button that's in the array, or do i have to override the entire array Item? I'm currently just overriding the button widget and expecting the add / remove in the "array" widget to be updated

jpeterson88 avatar Feb 22 '19 18:02 jpeterson88

No ButtonWidget is only used for the form actions. ArrayWidget does use this component (it uses a basic HTML button), so if you want to change that button, you need to override ArrayWidget.

ebrehault avatar Feb 22 '19 18:02 ebrehault

That explains what I'm doing. I overrode the array widget and it works like a charm. Thank you!

jpeterson88 avatar Feb 22 '19 18:02 jpeterson88