Skip to content

Implement Notification.getText() #2088

@mvysny

Description

@mvysny

Describe your motivation

Please add Notification.getText(). There's setText() already, so it makes sense to have the getText() function.

Also, I'm trying to create an utility function to add a close button (see vaadin/web-components#438 for details), but I am unable to do so. Adding a component clears the text, so I need to get the text, wrap it in a Span, then add the Span. However, I can't do that since there's no getText().

Describe the solution you'd like

getText() which would return the current text set via setText() or an empty string if the notification has children components.

Describe alternatives you've considered

private val _Notification_templateElement: Field by lazy(LazyThreadSafetyMode.PUBLICATION) {
    val f: Field = Notification::class.java.getDeclaredField("templateElement")
    f.isAccessible = true
    f
}

public val Notification._templateElement: Element
    get() = _Notification_templateElement.get(this) as Element

/**
 * Returns the notification text.
 */
public fun Notification.getText(): String {
    if (isNotEmpty) {
        // adding components to the notification clears the notification text
        return ""
    }
    val e: Element = _templateElement
    return e.getProperty("innerHTML") ?: ""
}

But it doesn't work as well since it will return a <template foo bar text if there are child components.

Additional context

Is there anything else you can add about the proposal?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions