Comments (7)
Thanks @alfonsogarciacaro ! Woot woot. That did the trick.
I'm posting this code for reference purpose:
Types.ts
module App.Types
open Fable.Core
open Elmish
type Feature = {
Id: string
Name: string
State: string
Tags: string list
Link: string
}
type Model = Feature list
type Msg =
| LoadFeatures
| FeaturesLoaded of Feature list
| Sync
| Reorder of (string * int)
// Types of https://github.com/clauderic/react-sortable-hoc/
type [<Pojo>] SortEnd = {
oldIndex: int;
newIndex: int;
}
type [<Pojo>] SortableContainerProp<'t> = {
axis: string;
lockAxis: string;
helperClass: string;
onSortEnd: SortEnd -> unit
items: 't list
}
type [<Pojo>] SortableContainerInput<'t> = {
items:'t list
}
type [<Pojo>] SortableElementProp<'t> = {
index: int;
key: obj;
value: 't;
}
type [<Pojo>] SortableElementInput<'t> = {
value: 't;
index: int;
}
View.fs
module App.View
open Fable.Core.JsInterop
open Fable.Helpers.React.Props
module R = Fable.Helpers.React
open Types
open Fable.Import
open Fable.Core
let SortableContainer<'t>(render: SortableContainerInput<'t> -> React.ReactElement): React.ComponentClass<SortableContainerProp<'t>> = import "SortableContainer" "react-sortable-hoc"
let SortableElement<'t>(render: SortableElementInput<'t> -> React.ReactElement): React.ComponentClass<SortableElementProp<'t>> = import "SortableElement" "react-sortable-hoc"
let getStateColor state =
match state with
| "Active" -> "has-text-info"
| "Resolved" -> "has-text-warning"
| "New" -> "has-text-grey"
| _ -> "has-text-black"
let FeatureRow = SortableElement(fun (props:SortableElementInput<Feature>) ->
let feature = props.value
let tags =
feature.Tags
|> List.map (fun tag -> R.span [ClassName "tag"] [R.str tag])
R.tr [] [
R.td [] [R.ofInt props.index]
R.td [] [R.span [getStateColor feature.State |> ClassName] [R.str feature.State]]
R.td [] [R.str feature.Name]
R.td [] [R.div [ClassName "tags"] tags ]
R.td [] [R.a [Target "blank"; Href feature.Link] [feature.Id |> R.str]]
]
)
let FeatureTable = SortableContainer(fun (props:SortableContainerInput<Feature>) ->
let rows =
props.items
|> List.mapi (fun idx feature ->
R.from FeatureRow {key = idx; index = idx; value = feature} []
)
R.table [ClassName "table is-fullwidth is-hoverable"] [
R.thead [] [
R.tr [] [
R.th [] [R.str "No."]
R.th [] [R.str "State"]
R.th [] [R.str "Name"]
R.th [] [R.str "Tags"]
R.th [] [R.str "Open"]
]
]
R.tbody [] rows
]
)
let view model dispatch =
let onSortEnd (input:SortEnd) =
printfn "sort ended, %A" input
R.div [] [
R.div [ClassName "has-text-right"; Id "commandbar"] [
R.button [ClassName "button is-primary"] [R.str "Refresh"]
R.from FeatureTable { axis = "y"; lockAxis = "y"; helperClass = ""; onSortEnd = onSortEnd; items = model } []
]
]
from fable-react.
Thanks @MangelMaxime, console.log
was solid advice π
I managed to get it working, here's the relevant code for future reference:
[<Pojo>]
type SliderProps =
{ min: int
max: int
step: int
defaultValue: int }
let inline slider (props : SliderProps) (elems : ReactElement list) : ReactElement =
ofImport "default" "rc-slider" props elems
let createSliderWithTooltip (render: SliderProps -> ReactElement list -> ReactElement) : ComponentClass<SliderProps> =
import "createSliderWithTooltip" "rc-slider"
let SliderWithTooltip = createSliderWithTooltip slider
let root model dispatch =
...
div
[ ClassName "column is-three-fifths"]
[ from SliderWithTooltip { min = 1; max = 10; step = 1; defaultValue = 2; } [] ]
from fable-react.
I think the next line should work:
let createSliderWithTooltip : obj = importMember "rc-slider"
You need to adapt the signature obj
.
from fable-react.
Hmm, the module value signature seems to be causing problems. Could you please try with a method signature (see below)? Also take into account the function will return a React component, that you use later to instantiate React elements.
I haven't tested it, but maybe something like this works?
open Fable.Import.React
open Fable.Helpers.React
let SortableContainer<'Props>(render: 'Props -> ReactElement): ComponentClass<'Props> = import "SortableContainer" "react-sortable-hoc"
let SortableList = SortableContainer(fun props -> failwith "render element")
// Code to render the element
from SortableList myProps []
I just noticed I forgot to rename from
when updating the helpers π
It probably should be ofObject
or similar.
from fable-react.
Sorry to resurrect this closed issue, but I've got a similar problem using react-component/slider. Following the docs, I was able to create and render a Slider
, but struggle with rendering a SliderWithTooltip
. I fail to figure out how to import the creation function and call it with the Slider
component as argument.
Here's how the component has to be created (from http://react-component.github.io/slider/examples/slider.html):
import Slider, { createSliderWithTooltip } from 'rc-slider';
const SliderWithTooltip = createSliderWithTooltip(Slider);
Source: https://github.com/react-component/slider/blob/master/src/createSliderWithTooltip.jsx#L6
Any help appreciated!
For reference, this is what I have working so far:
type SliderProps =
| Min of int
| Max of int
| Step of int
| DefaultValue of int
let inline slider (props : SliderProps list) (elems : ReactElement list) : ReactElement =
ofImport "default" "rc-slider" (keyValueList CaseRules.LowerFirst props) elems
let root model dispatch =
...
div
[ ClassName "column is-three-fifths"]
[ slider [ Min 1; Max 10; DefaultValue 1; ] [] ]
...
from fable-react.
Thanks @MangelMaxime, but I'm at a complete loss as to how to make that work, sorry. Doesn't createSliderWithTooltip
need to be declared as function? I don't understand how it would compile when declared as obj?
If I declare it like this, it compiles, but doesn't render anything.
let inline slider (props : SliderProps list) (elems : ReactElement list) : ReactElement =
ofImport "default" "rc-slider" (keyValueList CaseRules.LowerFirst props) elems
let createSliderWithTooltip (el : ReactElement) : ReactElement =
importMember "rc-slider"
let root model dispatch =
...
div
[ ClassName "column is-three-fifths"]
[ createSliderWithTooltip ( slider [ Min 1; Max 10; DefaultValue 1; ] [] ) ]
Sorry for being thick...
from fable-react.
Indeed to need to adapt the signature of createSliderWithTooltip
You need to adapt the signature obj.
When working with import first thing I like to test is: Is my import working ?
You can test that by doing: Fable.Import.Browser.console.log createSliderWithTooltip
If you get undefined
or equivalent then the import isn't working. If you get a function then the import is working and it's your way of using it that is incorrect.
from fable-react.
Related Issues (20)
- React Server Components integration HOT 2
- Can't figure out how to wrap my view with a Higher Order Component HOT 4
- JsInterop helper for wrapping a higher order component HOT 2
- Can't reference React in Fable HOT 2
- Fable.ReactServer.cssProp very slow HOT 4
- Add Custom DU to DOMAttr HOT 1
- FunctionComponent.Lazy and SSR HOT 1
- Update samples to Fable 3
- ReactDOM.hydrate is no longer supported in React 18 HOT 17
- Can't update shared state passed with useContext? HOT 3
- Q: Use ReactElement import member for Storybook HOT 3
- Missing 'open' from Fable.React.Props? HOT 2
- Last Fable.React upgrade breaks my code HOT 7
- Publish source files from Fable.React.Types and Fable.ReactDom.Types to nuget HOT 5
- Fable.React version 8 depends on React.Dom gte 18 lt 19 according to femto
- Add Pointer Events HOT 3
- Why is useCallback not implemented? HOT 3
- How to obtain a component ref in a function component? HOT 5
- FunctionComponent.Of functions cache breaks reusable code HOT 1
- voidEl breaks with: $ELEMENT is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fable-react.