-
Notifications
You must be signed in to change notification settings - Fork 43
Fix key prop type #74
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
Changes from 5 commits
5e09771
10f6400
9b0fe1b
adc1a55
2f48356
3f0f2e9
234798b
b8fa0f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,17 +14,13 @@ type component<'props> = Jsx.component<'props> | |
|
||
let component = Jsx.component | ||
|
||
%%private( | ||
@inline | ||
let addKeyProp = (p: 'props, k: string): 'props => | ||
Obj.magic(Js.Obj.assign(Obj.magic(p), {"key": k})) | ||
) | ||
|
||
@module("react") | ||
external createElement: (component<'props>, 'props) => element = "createElement" | ||
|
||
let createElementWithKey = (component, props, key) => | ||
createElement(component, addKeyProp(props, key)) | ||
let createElementWithKey = (~key=?, component, props) => { | ||
let _ = Obj.magic(props)["key"] = key | ||
createElement(component, props) | ||
} | ||
|
||
@module("react") | ||
external cloneElement: (element, 'props) => element = "cloneElement" | ||
|
@@ -36,21 +32,35 @@ external isValidElement: 'a => bool = "isValidElement" | |
external createElementVariadic: (component<'props>, 'props, array<element>) => element = | ||
"createElement" | ||
|
||
let createElementVariadicWithKey = (component, props, elements, key) => | ||
createElementVariadic(component, addKeyProp(props, key), elements) | ||
let createElementVariadicWithKey = (~key=?, component, props, elements) => { | ||
let _ = Obj.magic(props)["key"] = key | ||
createElementVariadic(component, props, elements) | ||
} | ||
|
||
@module("react/jsx-runtime") | ||
external jsxKeyed: (component<'props>, 'props, string) => element = "jsx" | ||
external jsxNotKeyed: (component<'props>, 'props) => element = "jsx" | ||
|
||
@module("react/jsx-runtime") | ||
external jsx: (component<'props>, 'props) => element = "jsx" | ||
external jsxKeyed: (component<'props>, 'props, string) => element = "jsx" | ||
|
||
let jsx = (~key=?, component, props) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering whether this can be achieved directly with an external. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. external jsxKeyed: (component<'props>, 'props, ~key: option<string>=?) => element = "jsx" Like this? Shouldn't we have to move the labelled key argument position to first? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that can't be done because it needs to be before mandatory arguments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this work? type was wrong_edited_to_fix_type_of_key external jsx: (component<'props>, 'props, ~key: string=?, @ignore unit) => element = "jsx" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think it is working. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for cleanest generated output, one needs to expose 2 functions: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, I couldn't find the doc about the third argument as key, but I found it from the source code. https://github.com/facebook/react/blob/8e2bde6f2751aa6335f3cef488c05c3ea08e074a/packages/react/src/jsx/ReactJSXElement.js#L210 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the key argument needs to be optional not <C key=Some("k") /> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It does not have to be optional. The PPX can take care of passing the value in the appropriate way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This would be easy fix, I'll fix the binding and make a PR for PPX. |
||
switch key { | ||
| Some(key) => jsxKeyed(component, props, key) | ||
| None => jsxNotKeyed(component, props) | ||
} | ||
|
||
@module("react/jsx-runtime") | ||
external jsxs: (component<'props>, 'props) => element = "jsxs" | ||
external jsxsNotKeyed: (component<'props>, 'props) => element = "jsxs" | ||
|
||
@module("react/jsx-runtime") | ||
external jsxsKeyed: (component<'props>, 'props, string) => element = "jsxs" | ||
|
||
let jsxs = (~key=?, component, props) => | ||
switch key { | ||
| Some(key) => jsxsKeyed(component, props, key) | ||
| None => jsxsNotKeyed(component, props) | ||
} | ||
|
||
type fragmentProps<'children> = {children: 'children} | ||
|
||
@module("react/jsx-runtime") external jsxFragment: component<fragmentProps<'children>> = "Fragment" | ||
|
@@ -413,13 +423,13 @@ external useInsertionEffect7: ( | |
|
||
@module("react") | ||
external useSyncExternalStore: ( | ||
~subscribe: @uncurry ((unit => unit) => ((. unit) => unit)), | ||
~subscribe: @uncurry (unit => unit, . unit) => unit, | ||
~getSnapshot: @uncurry unit => 'state, | ||
) => 'state = "useSyncExternalStore" | ||
|
||
@module("react") | ||
external useSyncExternalStoreWithServerSnapshot: ( | ||
~subscribe: @uncurry ((unit => unit) => ((. unit) => unit)), | ||
~subscribe: @uncurry (unit => unit, . unit) => unit, | ||
~getSnapshot: @uncurry unit => 'state, | ||
~getServerSnapshot: @uncurry unit => 'state, | ||
) => 'state = "useSyncExternalStore" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for missing changelog and tests too often 😅
Updated b8fa0f3