Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce an interface for elements which can be disabled #87

Open
robstoll opened this issue May 2, 2018 · 2 comments
Open

introduce an interface for elements which can be disabled #87

robstoll opened this issue May 2, 2018 · 2 comments
Labels

Comments

@robstoll
Copy link

robstoll commented May 2, 2018

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

@cy6erGn0m
Copy link
Contributor

cy6erGn0m commented May 3, 2018

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 }

@robstoll
Copy link
Author

robstoll commented May 4, 2018

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.

@cy6erGn0m cy6erGn0m added the JS label May 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants