kotlinx.html icon indicating copy to clipboard operation
kotlinx.html copied to clipboard

introduce an interface for elements which can be disabled

Open robstoll opened this issue 7 years ago • 2 comments
trafficstars

I have one functions which inter alia disabled HTMLInputElement, HTMLTextAreaElement and others. Currently I use an unsafeCastto HTMLInputElement for other types because I know that it works as internally it only uses disabled which HTMLTextAreaElement, HTMLSelectElement provide as well. However, it would be nicer if I could rely on a common interface and do not have to use unsafeCast

robstoll avatar May 02 '18 08:05 robstoll

All HTMLInputElement, HTMLTextAreaElement, HTMLSelectElement and so on are generated based on the specification IDLs. According to the specification HTMLTextAreaElement is not a HTMLInputElement and they even don't have any useful base type except HTMLElement. See, for example https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element

The workaround could be to write an extension property, something like this (untested)

inline var HTMLElement.disabled: Boolean
    get() = asDynamic().disabled ?: false
    set(value) { asDynamic().disabled = value }

cy6erGn0m avatar May 03 '18 16:05 cy6erGn0m

Ah nice, was not even aware of that such a spec exist. But anyway, not all specs are perfect aren't they ;) I understand if you do not want to change your generation approach. How about adding interfaces to kotlinx.html and provide extension functions, something like asFormSubElement() and the interface FormSubElement would look something like:

interface FormSubElement {
  val disabled
  val value
  val required
  ...
}

and the extension function:

fun HTMLInputElement.asFormSubElement() = this.unsafeCast<FormSubElement >()

I think the use case of disabling all input, select, textarea or to fetch all value etc. is common enough that it would be worth that kotlinx.html provides such an interface and that not every user has to reinvent the wheel.

robstoll avatar May 04 '18 08:05 robstoll