Commit 693eea23 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

Added couple of new functions

These were used in Gargantext, it is ported for common functionality.
parent ccfd0649
{
"name": "reactix",
"version": "0.4.5",
"version": "0.4.6",
"description": "",
"scripts": {
"rebase-set": "spago package-set-upgrade && spago psc-package-insdhall",
......
......@@ -3,5 +3,5 @@ module Reactix
) where
import Reactix.React (class IsComponent, Component, Consumer, Context, Element, Hooks, Memo, Provider, React, ReactDOM, Ref, consume, consumeContext, consumer, createContext, createDOMElement, createElement, rawCreateElement, createPortal, createRef, fragment, hooksComponent, isValid, memo, memo', provide, provideContext, provider, react, reactDOM, readNullableRef, readRef, render, runHooks, setRef, staticComponent, unsafeHooksEffect) as Exports
import Reactix.Hooks (Reducer, State, nothing, thenNothing, unsafeUseCallback, unsafeUseEffect, unsafeUseImperativeHandle, unsafeUseLayoutEffect, unsafeUseMemo, useCallback, useCallback1, useCallback2, useCallback3, useCallback4, useCallback5, useContext, useDebugValue, useDebugValue', useEffect, useEffect', useEffect1, useEffect1', useEffect2, useEffect2', useEffect3, useEffect3', useEffect4, useEffect4', useEffect5, useEffect5', useEffectFn1, useEffectFn1', useEffectFn2, useEffectFn2', useEffectFn3, useEffectFn3', useEffectFn4, useEffectFn4', useEffectFn5, useEffectFn5', useEffectOnce, useEffectOnce', useImperativeHandle, useImperativeHandle1, useImperativeHandle2, useImperativeHandle3, useImperativeHandle4, useImperativeHandle5, useLayoutEffect, useLayoutEffect', useLayoutEffect1, useLayoutEffect1', useLayoutEffect2, useLayoutEffect2', useLayoutEffect3, useLayoutEffect3', useLayoutEffect4, useLayoutEffect4', useLayoutEffect5, useLayoutEffect5', useLayoutEffectFn1, useLayoutEffectFn1', useLayoutEffectFn2, useLayoutEffectFn2', useLayoutEffectFn3, useLayoutEffectFn3', useLayoutEffectFn4, useLayoutEffectFn4', useLayoutEffectFn5, useLayoutEffectFn5', useMemo, useMemo1, useMemo2, useMemo3, useMemo4, useMemo5, useReducer, useReducer', useRef, useState, useState') as Exports
import Reactix.Hooks (Reducer, State, Setter, nothing, thenNothing, unsafeUseCallback, unsafeUseEffect, unsafeUseImperativeHandle, unsafeUseLayoutEffect, unsafeUseMemo, useCallback, useCallback1, useCallback2, useCallback3, useCallback4, useCallback5, useContext, useDebugValue, useDebugValue', useEffect, useEffect', useEffect1, useEffect1', useEffect2, useEffect2', useEffect3, useEffect3', useEffect4, useEffect4', useEffect5, useEffect5', useEffectFn1, useEffectFn1', useEffectFn2, useEffectFn2', useEffectFn3, useEffectFn3', useEffectFn4, useEffectFn4', useEffectFn5, useEffectFn5', useEffectOnce, useEffectOnce', useImperativeHandle, useImperativeHandle1, useImperativeHandle2, useImperativeHandle3, useImperativeHandle4, useImperativeHandle5, useLayoutEffect, useLayoutEffect', useLayoutEffect1, useLayoutEffect1', useLayoutEffect2, useLayoutEffect2', useLayoutEffect3, useLayoutEffect3', useLayoutEffect4, useLayoutEffect4', useLayoutEffect5, useLayoutEffect5', useLayoutEffectFn1, useLayoutEffectFn1', useLayoutEffectFn2, useLayoutEffectFn2', useLayoutEffectFn3, useLayoutEffectFn3', useLayoutEffectFn4, useLayoutEffectFn4', useLayoutEffectFn5, useLayoutEffectFn5', useMemo, useMemo1, useMemo2, useMemo3, useMemo4, useMemo5, useReducer, useReducer', useRef, useState, useState') as Exports
import Reactix.SyntheticEvent (SyntheticEvent) as Exports
module Reactix.Hooks
( State, useState, useState'
( State, Setter, useState, useState'
, Reducer, useReducer, useReducer'
, useContext
, useRef
......@@ -49,6 +49,9 @@ delayEffect = delay
-- | A state hook is a tuple of value and setter
type State state = Tuple state ((state -> state) -> Effect Unit)
-- a setter function, for useState
type Setter t = (t -> t) -> Effect Unit
-- | Given an Effect function returning an initial value, returns a State
useState :: forall s. (Unit -> s) -> Hooks (State s)
useState s =
......
......@@ -12,7 +12,8 @@ module Reactix.React
, class IsComponent
, Component, createElement, createDOMElement
, rawCreateElement
, staticComponent, hooksComponent
, StaticComponent, staticComponent, staticComponentWithModule
, HooksComponent, hooksComponent, hooksComponentWithModule
, fragment
, Ref, createRef, readRef, readNullableRef, setRef
......@@ -89,6 +90,8 @@ instance consumerIsComponent :: IsComponent (Consumer v) () (v -> Element) where
-- | The type of a function that can be turned into a component with
-- | `staticComponent`. Will not have access to the `Hooks` Monad.
type Module = String
type StaticComponent props = Record props -> Array Element -> Element
-- | Turns a `StaticComponent` function into a Component
......@@ -98,6 +101,9 @@ staticComponent name c = Component $ named name $ mkEffectFn1 c'
c' :: Record props -> Effect Element
c' props = pure $ c props (children props)
staticComponentWithModule :: forall props. Module -> String -> StaticComponent props -> R.Component props
staticComponentWithModule module' name c = R.staticComponent (module' <> "." <> name) c
-- | The type of a function that can be turned into a component with
-- | `hooksComponent`. Will have access to the `Hooks` Monad.
type HooksComponent props = Record props -> Array Element -> Hooks Element
......@@ -109,6 +115,9 @@ hooksComponent name c = Component $ named name $ mkEffectFn1 c'
c' :: Record props -> Effect Element
c' props = runHooks $ c props (children props)
hooksComponentWithModule :: forall props. Module -> String -> HooksComponent props -> R.Component props
hooksComponentWithModule module' name c = hooksComponent (module' <> "." <> name) c
rawCreateElement :: forall c p cs. c -> p -> Array cs -> Element
rawCreateElement c p cs = react ... "createElement" $ args
where args = PA.unshift c $ PA.unshift p cs
......
......@@ -6,9 +6,16 @@ import DOM.Simple as DOM
import DOM.Simple.Event (class HasModifierKeys, class IsEvent, class IsMouseEvent, KeyboardEvent, MouseButtonEvent)
import Effect ( Effect )
import FFI.Simple ( (..), (...), delay )
import Unsafe.Coerce (unsafeCoerce)
foreign import data SyntheticEvent :: Type -> Type
unsafeEventValue :: forall event. event -> String
unsafeEventValue e = (unsafeCoerce e).target.value
unsafeEventTarget :: forall event. event -> DOM.Element
unsafeEventTarget e = (unsafeCoerce e).target
bubbles :: forall e. IsEvent e => SyntheticEvent e -> Boolean
bubbles e = e .. "bubbles"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment