| 34 | * A generic widget that displays a button with icon or text content. |
| 35 | */ |
| 36 | export class ToggleWidget extends Widget<ToggleWidgetProps> { |
| 37 | static defaultProps: Required<ToggleWidgetProps> = { |
| 38 | ...Widget.defaultProps, |
| 39 | id: 'icon', |
| 40 | placement: 'top-left', |
| 41 | viewId: null, |
| 42 | initialChecked: false, |
| 43 | icon: '', |
| 44 | onIcon: undefined!, |
| 45 | label: '', |
| 46 | onLabel: undefined!, |
| 47 | color: '', |
| 48 | onColor: undefined!, |
| 49 | onChange: undefined! |
| 50 | }; |
| 51 | |
| 52 | className = 'deck-widget-toggle'; |
| 53 | placement: WidgetPlacement = 'top-left'; |
| 54 | checked: boolean; |
| 55 | |
| 56 | constructor(props: ToggleWidgetProps) { |
| 57 | super(props); |
| 58 | this.checked = this.props.initialChecked; |
| 59 | this.setProps(this.props); |
| 60 | } |
| 61 | |
| 62 | setProps(props: Partial<ToggleWidgetProps>) { |
| 63 | this.placement = props.placement ?? this.placement; |
| 64 | this.viewId = props.viewId; |
| 65 | super.setProps(props); |
| 66 | } |
| 67 | |
| 68 | onRenderHTML(rootElement: HTMLElement): void { |
| 69 | const { |
| 70 | className, |
| 71 | style, |
| 72 | icon, |
| 73 | label, |
| 74 | color, |
| 75 | onIcon = icon, |
| 76 | onLabel = label, |
| 77 | onColor = color |
| 78 | } = this.props; |
| 79 | const on = this.checked; |
| 80 | |
| 81 | rootElement.dataset.checked = String(on); |
| 82 | |
| 83 | render( |
| 84 | <IconButton |
| 85 | className={className} |
| 86 | style={style as JSX.CSSProperties} |
| 87 | icon={on ? onIcon : icon} |
| 88 | label={on ? onLabel : label} |
| 89 | color={on ? onColor : color} |
| 90 | onClick={this._toggle} |
| 91 | />, |
| 92 | rootElement |
| 93 | ); |
nothing calls this directly
no test coverage detected
searching dependent graphs…