Open inouiw opened 3 years ago
I'm always confused where ReactElementType
is involved. @Zaid-Ajaj, do you know how this works?
Hi,
I got a working solution. It requires creating a function. Since I did not know what type the argument should be, I inserted a console.log in the function to see what was passed and then created the IPaperProps type. A solution without needing to create a function would be nice although it is not urgent for me.
Edit: removed [<ReactComponent>]
because just a function is needed.
open Feliz
open HtmlEx
open Feliz.MaterialUI
type IPaperProps =
abstract member className: string
abstract member children: ReactElement []
let PaperComponent (props: IPaperProps) =
Mui.paper props.children
// https://material-ui.com/components/tables/#basic-table
let helptextElement2 =
Mui.tableContainer [
tableContainer.component' (Fable.React.ReactElementType.ofFunction PaperComponent)
tableContainer.children [
Mui.table [
table.size.small
table.children [
Mui.tableHead [
Mui.tableRow [
Mui.tableCell "col 1"
Mui.tableCell "col 2"
]
]
Mui.tableBody [
Mui.tableRow [
Mui.tableCell "lala"
Mui.tableCell "liauh"
]
]
]
]
]
]
Yes, that seems like an ugly and brittle workaround. I think there are better ways, but I'm not sure how. Perhaps @Zaid-Ajaj or other Fable/React wizards know this? 🧙♂️
So I believe this would work: tableContainer.component' (importDefault "@material-ui/core/Paper")
.
The library could be adjusted to have something like this:
type MuiTypes =
static member inline paper : ReactElementType = importDefault "@material-ui/core/Paper"
@Shmew thanks. That works, and using MuiTypes it looks nice. Maybe also consider adding a method MuiTypes.fromReactElement (elem: ReactElement) and/or ReactElementType.fromReactElement (elem: ReactElement)
Does that work for you for any component you define? I haven't personally tried it, I don't think that will work with any imported components (which is why we need the MuiTypes here).
@Shmew sorry I am not sure if I understand your question.
By the way the version with MuiTypes compiles to
MuiHelpers_createElement(TableContainer, [["component", Paper], ["children", reactApi.Children.toArray([MuiHelpers_createElement(Table, [["size", "small"], ["children", reactApi.Children.toArray([MuiHelpers_createElement(TableHead, [["children", reactApi.Children.toArray([MuiHelpers_createElement(TableRow, [["children", reactApi.Children.toArray([MuiHelpers_createElement(TableCell, [["children", "Command"]]), MuiHelpers_createElement(TableCell, [["children", "Keybinding"]]), MuiHelpers_createElement(TableCell, [["children", "When"]])])]])])]]), MuiHelpers_createElement(TableBody, [["children", reactApi.Children.toArray([MuiHelpers_createElement(TableRow, [["children", reactApi.Children.toArray([MuiHelpers_createElement(TableCell, [["children", reactApi.Children.toArray([(() => {
Paper is declared in Paper.d.ts as export default function Paper(props: PaperProps): JSX.Element;
I like the MuiTypes approach. Though I am thinking if it could be more easily discoverable. The most intuitive would be if I could just write
tableContainer.component' Mui.Paper
Instead of
tableContainer.component' MuiTypes.Paper
But that would mean that for every "component'" method there needs to be an additional overload that does some conversion.
The following alternatives may also be worth to consider:
tableContainer.component' ReactElementType.Paper
tableContainer.component' MuiTypes.Paper.ElementType
Yeah the issue is Mui.paper
won't work because it's expecting a list of properties, not the actual object. I was asking if you had a component like React.functionComponent(fun props -> Html.div [])
does it work for tableContainer.component'
.
Adding a MuiTypes
module as described by @Shmew in https://github.com/cmeeren/Feliz.MaterialUI/issues/67#issuecomment-766088269 seems simple, but I'm still fuzzy enough about ReactElementType
and associated APIs that I'm unsure whether this is the best solution. I don't have better options, I'd just like someone knowledgeable about these matters to reassure me (or provide a better alternative).
Will probably do this at some point if no-one picks up this torch, but it'll be done sooner if someone can help me out with these clarifications.
The component' property accepts a string or a ReactElementType.
I want to set the component' property of tableContainer to Mui.paper. The code below compiles but then I the following error in the console
The generated JavaScript is:
The F# code is:
It would be nice if somebody could help me with the problem.