Code Monkey home page Code Monkey logo

Comments (7)

nojaf avatar nojaf commented on July 19, 2024 3

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.

dstockhammer avatar dstockhammer commented on July 19, 2024 3

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.

MangelMaxime avatar MangelMaxime commented on July 19, 2024 1

@dstockhammer

I think the next line should work:

let createSliderWithTooltip : obj = importMember "rc-slider"

You need to adapt the signature obj.

from fable-react.

alfonsogarciacaro avatar alfonsogarciacaro commented on July 19, 2024

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.

dstockhammer avatar dstockhammer commented on July 19, 2024

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.

dstockhammer avatar dstockhammer commented on July 19, 2024

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.

MangelMaxime avatar MangelMaxime commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.