ReasonML bindings for react-flatpickr.
yarn add bs-react-flatpickr
Add bs-react-flatpickr
to your bsconfig.json
.
Because accepting different types types for a single prop is not type safe these bindings use polymorphic variants instead to represent each one of the possible types:
open BsReactFlatpickr;
[@react.component]
let make = () =>
<Flatpickr value=`date(Js.Date.make()) />;
open BsReactFlatpickr
let make () =
((Flatpickr.createElement ~value:(`date (Js.Date.make ())) ~children:[] ())
[@JSX ])[@@react.component ]
All props are optional
string
This will be passed to the inner input
This can either be string
, array(string)
, Js.Date.t
or array(Js.Date.t)
you need to use
polymorphic variants for these to be typesafe:
string
array(string)
Js.Date.t
array(Js.Date.t)
You use them by wrapping your value in the right variant like this:
<Flatpickr value=`str("2020-06-12") />
let _ =
((Flatpickr.createElement ~value:(`str "2020-06-12") ~children:[] ())
[@JSX ])
Js.t({..})
we provide a function that will help you generate these options for you FlatpickrOptions.make
all argument are optional and those not set will be set to flatpickr.js
defaults.
Flatpickr options
: you can pass all Flatpickr parameters
here.Flatpickr
hooks can be passed within this option too.Example:
open BsReactFlatpickr;
[@react.component]
let make = () => {
let today = Js.Date.make();
let daysAgo = today |> DateFns.subDays(3.0);
let daysFromNow = today |> DateFns.addDays(3.0);
<Flatpickr
options={
FlatpickrOptions.make(
~dateFormat="F j, Y",
~maxDate=`date(daysFromNow),
~minDate=`date(daysAgo),
(),
)
}
value=today
/>;
};
open BsReactFlatpickr
let make () =
let today = Js.Date.make () in
let daysAgo = today |> (DateFns.subDays 3.0) in
let daysFromNow = today |> (DateFns.addDays 3.0) in
((Flatpickr.createElement
~options:(FlatpickrOptions.make ~dateFormat:"F j, Y"
~maxDate:(`date daysFromNow) ~minDate:(`date daysAgo) ())
~value:today ~children:[] ())[@JSX ])[@@react.component ]
React.element
this prop is closely related with the wrap option
from Flatpickr
, please refer to the former link for more information.
string
This class will be applied to the inner <input />
element.
(Value.t, string) => unit
every event handler prop has this type.
Value.t
- first arugment is an array(Js.Date.t)
representing selected dates, if
no dates have been selected array will be empty.string
- second argument is a representation of the latest selected date as a string
formatted by the dateFormat
(see options section).The following props
are provided in order to customize the Flatpickr's functions
default behaviour.
Please refer to the Events & Hooks section from Flatpickr
library.
Note: Event handlers for flatpickr.js
have a third argument which is a flatpickr
instance we've
ommitted that since the idea is to handle everything via react, that said if a valid usecase for it arises
we can add it in the future.
onChange
onClose
onDayCreate
onDestroy
onMonthChange
onOpen
onReady
onValueUpdate
Just import any of the already present flatpickr.js
themes:
[%bs.raw {| require("flatpickr/dist/flatpickr.css") |}];
let _ = [%bs.raw {| require("flatpickr/dist/flatpickr.css") |}]
Note: Both FlatpickrTypes.Hooks
and FlatpickrTypes.Value
have a classify
function for boxing
and a reduce
function for unboxing
. This is because both the value and options
prop can be of more than one type:
open BsReactFlatpickr;
[@react.component]
let make = () => {
let (date, setDate) = React.useState(_ => `date(Js.Date.make()));
FlatpickrTypes.(
<Flatpickr
value=date
onChange={
(value, _) => setDate(_ => Value.classify(value))
}
/>
);
};
949: <UNKNOWN SYNTAX ERROR>
yarn build
yarn storybook