-
Notifications
You must be signed in to change notification settings - Fork 305
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
How to simulate the javascript spread operator? #4002
Comments
This issue is similar, but using |
@joprice Thanks for the suggestion. For now I took another approach as I write all my UI code as JSX. This is working now: JSX.jsx
$"""
import {{ MuiMarkdown, getOverrides }} from 'mui-markdown';
import Box from '@mui/material/Box';
<Box sx={sx}>
<MuiMarkdown
options={{ {{
disableParsingRawHTML : true,
wrapper : "React.Fragment",
overrides : {{
...getOverrides(),
h1 :
{{
component : "h1",
props : {{ sx : {{ fontSize : 24, fontWeight : "bold" }} }}
}},
h2 :
{{
component : "h2",
props : {{ sx : {{ fontSize : 20, fontWeight : "bold" }} }}
}}, /// etc....
}}
}} }}
>
{report.Markdown}
</MuiMarkdown>
</Box>
""" So, by escaping all "{" I can write a regular javascript object and apply the spread operator in that object. But still open for potential better alternatives. |
You can use open Fable.Core
open Fable.Core.JsInterop
let inline spread (value : obj) : obj =
emitJsStatement value """...$0"""
let myJsObject =
{|
Name = "Maxime"
Age = "32"
|}
let test () =
spread myJsObject generates export const myJsObject = {
Age: "32",
Name: "Maxime",
};
export function test() {
...myJsObject;
} What I mean is that I don't think we can write something equivalent to this while maintaining type safety every where. function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers)); |
@halcwb Can we close this issue? We could in the future decide to add a |
@MangelMaxime Sure, thanks for your suggestions and attention! |
I think the below is the most elegant solution I came up with: namespace Components
open Fable.Core
open Fable.Core.JsInterop
module Markdown =
[<Import("getOverrides", from = "mui-markdown")>]
let private getOverrides () = emitJsExpr () "getOverrides()"
[<Emit "Object.assign({}, $0, $1)">]
let private objectAssign (x: obj) (y: obj) : obj = jsNative
[<Import("Typography", from = "@mui/material")>]
let private Typography: obj = jsNative
[<JSX.Component>]
let View (text: {| md: string |}) =
let headerSx =
{|
fontWeight = "bold"
color = "primary.main"
marginTop = 2
marginBottom = 1
|}
let h1 =
{|
``component`` = Typography
props =
{|
sx = {| headerSx with fontSize = 24 |}
|}
|}
let h2 =
{|
``component`` = Typography
props =
{|
sx = {| headerSx with fontSize = 20 |}
|}
|}
let p =
{|
``component`` = Typography
props =
{|
fontSize = 12
paddingTop = 2
paddingBottom = 2
|}
|}
let a =
{|
``component`` = Typography
props = {| fontSize = 12 |}
|}
let overrides =
{|
h1 = h1
h2 = h2
p = p
a = a
|}
// merge the overrides with the default overrides
let overrides = objectAssign (getOverrides ()) overrides
let options =
{|
disableParsingRawHTML = true
overrides = overrides
|}
JSX.jsx
$"""
import {{ MuiMarkdown }} from 'mui-markdown';
<MuiMarkdown
options={options}
>
{text.md}
</MuiMarkdown>
""" |
Question
When this code is transpiled:
You will get:
Only of course not the commented spread operator spreading the default settings.
So, the question is, if there is a way to spread a javascript object or array to another array or object from the F# perspective?
The text was updated successfully, but these errors were encountered: