@@ -5,6 +5,8 @@ import { Icons } from '../core/icons';
55
66export interface ButtonComponent extends Component {
77 onClick : SimpleEvent < void > ;
8+ setIcon ( d : string ) : void ;
9+ setLabel ( label : string ) : void ;
810}
911
1012export interface ButtonComponentConfiguration {
@@ -19,6 +21,22 @@ export function buttonComponent(label: string, configuration?: ButtonComponentCo
1921 onClick . forward ( ) ;
2022 }
2123
24+ function setIcon ( d : string ) {
25+ if ( icon ) {
26+ icon . getElementsByTagName ( 'path' ) [ 0 ] . setAttribute ( 'd' , d ) ;
27+ } else {
28+ throw new Error ( 'This button does not have icon' ) ;
29+ }
30+ }
31+
32+ function setLabel ( label : string ) {
33+ if ( configuration ?. icon ) {
34+ throw new Error ( 'Cannot change label on button with icon' ) ;
35+ } else {
36+ view . innerText = label ;
37+ }
38+ }
39+
2240 const onClick = new SimpleEvent < void > ( ) ;
2341
2442 let className = 'swe-button' ;
@@ -33,16 +51,19 @@ export function buttonComponent(label: string, configuration?: ButtonComponentCo
3351 title : label ,
3452 'aria-label' : label
3553 } ) ;
54+ let icon : SVGElement | undefined ;
3655 if ( configuration ?. icon ) {
37- const svg = Icons . createSvg ( configuration . icon , 'swe-button-icon' ) ;
38- view . appendChild ( svg ) ;
56+ icon = Icons . createSvg ( configuration . icon , 'swe-button-icon' ) ;
57+ view . appendChild ( icon ) ;
3958 } else {
4059 view . innerText = label ;
4160 }
4261 view . addEventListener ( 'click' , onClicked , false ) ;
4362
4463 return {
4564 view,
46- onClick
65+ onClick,
66+ setIcon,
67+ setLabel
4768 } ;
4869}
0 commit comments