@@ -298,7 +298,7 @@ module Props =
298
298
[<KeyValueList>]
299
299
type Prop =
300
300
| Key of string
301
- | Ref of ( obj -> unit )
301
+ | Ref of ( Browser.Element -> unit )
302
302
interface IHTMLProp
303
303
304
304
[<KeyValueList>]
@@ -578,44 +578,44 @@ open Props
578
578
open Fable.AST
579
579
open Fable.AST .Fable .Util
580
580
581
+ /// Only used internally to emit inline Fable AST
581
582
type Emitter () =
582
583
let createEl = makeImport " createElement" React
583
- let toPlainJsObj ( expr : Fable.Expr ) =
584
- CoreLibCall( " Util" , Some " toPlainJsObj" , false , [ expr])
585
- |> makeCall expr.Range expr.Type
586
- let (| CoreMeth | _ |) coreMod meth = function
587
- | Fable.Value( Fable.ImportRef( meth', coreMod', Fable.CoreLib))
588
- when meth' = meth && coreMod' = coreMod -> Some CoreMeth
589
- | _ -> None
590
- let toArrayNonEmpty = function
591
- | Fable.Apply( CoreMeth " List" " default" ,[],_,_,_) ->
592
- Fable.Value Fable.Null
593
- | Fable.Apply( CoreMeth " List" " ofArray" , [ arr], Fable.ApplyMeth,_,_) -> arr
594
- | expr ->
595
- GlobalCall ( " Array" , Some " from" , false , [ expr])
596
- |> makeCall expr.Range ( Fable.Array Fable.Any)
584
+ let spread args =
585
+ let inline (| CoreMeth | _ |) coreMod meth = function
586
+ | Fable.Value( Fable.ImportRef( meth', coreMod', Fable.CoreLib))
587
+ when meth' = meth && coreMod' = coreMod -> Some CoreMeth
588
+ | _ -> None
589
+ let inline (| ArrayConst | _ |) e =
590
+ match e with
591
+ | Fable.Value( Fable.ArrayConst( Fable.ArrayValues vals, _)) -> Some vals
592
+ | _ -> None
593
+ match args with
594
+ | Fable.Apply( CoreMeth " List" " default" ,[],_,_,_) -> []
595
+ | Fable.Apply( CoreMeth " List" " ofArray" , [ ArrayConst vals], Fable.ApplyMeth,_,_) -> vals
596
+ | expr -> [ Fable.Value( Fable.Spread expr)]
597
597
598
598
member x.Com ( _com : Fable.ICompiler , i : Fable.ApplyInfo ) =
599
599
let args =
600
600
match i.args with
601
601
| [ props; children] ->
602
602
let com = makeNonGenTypeRef i.methodTypeArgs.Head
603
- [ com; toPlainJsObj props; toArrayNonEmpty children]
603
+ [ com; props] @ spread children
604
604
| _ -> failwith " Unexpected arguments"
605
605
Fable.Apply( createEl, args, Fable.ApplyMeth, i.returnType, i.range)
606
606
member x.From ( _com : Fable.ICompiler , i : Fable.ApplyInfo ) =
607
607
let args =
608
608
match i.args with
609
609
| [ com; props; children] ->
610
- [ com; toPlainJsObj props; toArrayNonEmpty children]
610
+ [ com; props] @ spread children
611
611
| _ -> failwith " Unexpected arguments"
612
612
Fable.Apply( createEl, args, Fable.ApplyMeth, i.returnType, i.range)
613
613
614
614
member x.DomEl ( _com : Fable.ICompiler , i : Fable.ApplyInfo ) =
615
615
let args =
616
616
match i.args with
617
617
| [ tag; props; children] ->
618
- [ tag; props; toArrayNonEmpty children]
618
+ [ tag; props] @ spread children
619
619
| _ -> failwith " Unexpected arguments"
620
620
Fable.Apply( createEl, args, Fable.ApplyMeth, i.returnType, i.range)
621
621
@@ -624,29 +624,31 @@ type Emitter() =
624
624
match i.args with
625
625
| [ props; children] ->
626
626
let tag = Fable.Value( Fable.StringConst tag)
627
- [ tag; props; toArrayNonEmpty children]
627
+ [ tag; props] @ spread children
628
628
| _ -> failwith " Unexpected arguments"
629
629
Fable.Apply( createEl, args, Fable.ApplyMeth, i.returnType, i.range)
630
630
631
+ open Fable.Import .React
632
+
631
633
/// Instantiate a React component from a type inheriting React.Component<>
632
634
[<Emit( typeof< Emitter>, " Com" ) >]
633
- let com < 'T , 'P , 'S when 'T :> React. Component< 'P , 'S >> ( props : 'P ) ( children : React. ReactElement< obj > list ): React. ReactElement< obj > = jsNative
635
+ let com < 'T , 'P , 'S when 'T :> Component < 'P , 'S >> ( props : 'P ) ( children : ReactElement list ): ReactElement = jsNative
634
636
/// Instantiate a stateless component from a function
635
637
636
638
[<Emit( typeof< Emitter>, " From" ) >]
637
- let fn ( f : 'Props -> # React. ReactElement<obj> ) ( props : 'Props ) ( children : React. ReactElement< obj > list ): React. ReactElement< obj > = jsNative
639
+ let fn <[< Pojo >] 'P > ( f : 'P -> ReactElement ) ( props : 'P ) ( children : ReactElement list ): ReactElement = jsNative
638
640
639
641
/// Instantiate an imported React component
640
642
[<Emit( typeof< Emitter>, " From" ) >]
641
- let from < 'P > ( com : React. ComponentClass< 'P >) ( props : 'P ) ( children : React. ReactElement< obj > list ): React. ReactElement< obj > = jsNative
643
+ let from <[< Pojo >] 'P > ( com : ComponentClass < 'P >) ( props : 'P ) ( children : ReactElement list ): ReactElement = jsNative
642
644
643
645
/// Instantiate a DOM React element
644
646
[<Emit( typeof< Emitter>, " DomEl" ) >]
645
- let domEl ( tag : string ) ( props : IHTMLProp list ) ( children : React. ReactElement< obj > list ): React. ReactElement< obj > = jsNative
647
+ let domEl ( tag : string ) ( props : IHTMLProp list ) ( children : ReactElement list ): ReactElement = jsNative
646
648
647
649
/// Instantiate an SVG React element
648
650
[<Emit( typeof< Emitter>, " DomEl" ) >]
649
- let svgEl ( tag : string ) ( props : #IProp list ) ( children : React. ReactElement< obj > list ): React. ReactElement< obj > = jsNative
651
+ let svgEl ( tag : string ) ( props : #IProp list ) ( children : ReactElement list ): ReactElement = jsNative
650
652
651
653
[<Emit( typeof< Emitter>, " Tagged" , " a" ) >]
652
654
let a b c = domEl " a" b c
@@ -914,6 +916,6 @@ let text b c = svgEl "text" b c
914
916
let tspan b c = svgEl " tspan" b c
915
917
916
918
/// Cast a string to a React element (erased in runtime)
917
- let [<Emit("$0")>] str ( s : string ): React. ReactElement< obj > = unbox s
919
+ let [<Emit("$0")>] str ( s : string ): ReactElement = unbox s
918
920
/// Cast an option value to a React element (erased in runtime)
919
- let [<Emit("$0")>] opt ( o : React. ReactElement< obj > option ): React. ReactElement< obj > = unbox o
921
+ let [<Emit("$0")>] opt ( o : ReactElement option ): ReactElement = unbox o
0 commit comments