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

Customize tags with inner html #42

Closed
da-liii opened this issue Jan 22, 2017 · 12 comments
Closed

Customize tags with inner html #42

da-liii opened this issue Jan 22, 2017 · 12 comments

Comments

@da-liii
Copy link

da-liii commented Jan 22, 2017

For example, https://ant.design/components/popconfirm/

// confirm, cancel are functions
<Popconfirm title="Are you sure delete this task?" onConfirm={confirm} onCancel={cancel} okText="Yes" cancelText="No">
    <a href="#">Delete</a>
</Popconfirm>

What I expect:

class Popconfirm(title: String, okText: String, cancelText: String, onConfirm: Event => Unit, onCancel: Event => Unit) extend SomeTrait {
  @some_anno override def render(body: Element): Element = ???
}

here render is the function used in macros.

But this test case(https://github.com/ThoughtWorksInc/Binding.scala/blob/11.0.x/fxml/.js/src/test/scala/com/thoughtworks/binding/DateSpec.scala) shows that <Date></Date> is Binding[Date] or another type relating to Date. It seems @fxml is not suitable for customizing tags to create something like Ant Design.

In #4 (comment) ,
there are tricks to make custom tags work, but it is without any inner html (eg. <a href="#">Delete</a>).

@Atry
Copy link
Collaborator

Atry commented Jan 22, 2017

Is the syntax difference between custom tags and custom functions important for you?

If it is not important for you, you can simply create a popconfirm function and use it like this:

popconfirm(title="Are you sure delete this task?",
           onConfirm=confirm,
           onCancel=cancel,
           okText="Yes",
           cancelText="No") {
  <a href=" ">Delete</a >
}

@da-liii
Copy link
Author

da-liii commented Jan 22, 2017

I have to admit that the syntax is important for me. I'm not sure for other people whether it is important or not? In my opinion, syntax is one reason why Binding.scala is more charming than other alternatives.

For example, in ScalaTags and HTML literal which one you will choose to use?

Syntax matters a lot. In this particular example, using the popconfirm function is indeed more flexible and general. But custom tags syntax has the power to reduce the flexibility and make it simple and consistent.

As for me, I'm developing a library(https://github.com/sadhen/Binding-SemanticUI) based on Binding.scala and SemanticUI to provide easy-to-use custom tags like Ant Design(http://ant.design), so that programmers already familiar with Ant Design would be happy to use Binding.scala.

@da-liii da-liii changed the title Custom Tag Questions Customize tags with inner html Jan 22, 2017
@da-liii
Copy link
Author

da-liii commented Jan 22, 2017

I found a more acceptable work around:

<div>
<Popconfirm title="Are you sure delete this task?"
                       onConfirm={confirm}
                       onCancel={cancel}
                       okText="Yes"
                       cancelText="No"
                       inner={
      <a href="#">Delete</a>
}/>
</div>

Comparing with

<div> {
  popconfirm(title="Are you sure delete this task?",
           onConfirm=confirm,
           onCancel=cancel,
           okText="Yes",
           cancelText="No") {
    <a href="#">Delete</a >
  }
} </div>

, which one do you prefer ?

I prefer the former one. However, using the latter one, we would have more IDE support.

@maxkorolev
Copy link

+1, that would be awesome

@csoren
Copy link

csoren commented Feb 20, 2017

This would be great for consuming web components, such as the ones defined by Polymer.

@skaz1970
Copy link

skaz1970 commented Oct 19, 2018

Is the syntax difference between custom tags and custom functions important for you?

If it is not important for you, you can simply create a popconfirm function and use it like this:

popconfirm(title="Are you sure delete this task?",
           onConfirm=confirm,
           onCancel=cancel,
           okText="Yes",
           cancelText="No") {
  <a href=" ">Delete</a >
}

Please, show the body (with return type) of popconfirm

@Atry
Copy link
Collaborator

Atry commented Oct 19, 2018

import com.thoughtworks.binding.dom
import org.scalajs.dom.raw._
import org.scalajs.dom.document

@dom def popconfirm(
  title: String,
  okText: String, cancelText: String,
  onConfirm: Event => Unit, onCancel: Event => Unit
)(
  // For FXML, use `Binding[Node]` instead of `Node`
  message: Node 
) = {
  <section>
    <h3>{ title }</h3>
    <div>
    { message }
    </div>
    <button type="button" onclick={onConfirm}>{
      okText
    }</button>
    <button type="button" onclick={onCancel}>{
      cancelText
    }</button>
  </section>
}

@dom
def render = popconfirm(title="Are you sure delete this task?",
          onConfirm={ _ =>
            println("confirm")
          },
          onCancel={ _ =>
            println("cancel")
          },
          okText="Yes",
          cancelText="No") {
  <a href=" ">Delete</a >
}.bind // .bind should be avoided for FXML

dom.render(document.body, render)

@skaz1970
Copy link

Thanks a lot!

@skaz1970
Copy link

skaz1970 commented Oct 19, 2018

But if I'd like to get instead of message: Node different types, Binding[Node], BindingSeq[Node], Binding[Option[Node]]? Can you propose anything? Is there any common type?

@Atry
Copy link
Collaborator

Atry commented Oct 19, 2018 via email

@skaz1970
Copy link

Thanks, my thought were around BindingSeq[Node], but converting makes code less readable.

@da-liii
Copy link
Author

da-liii commented Dec 30, 2018

I'm trying to implement the inner html feature in https://github.com/sadhen/Binding-SemanticUI

@da-liii da-liii closed this as completed Dec 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants