@colisweb/rescript-toolkit 3.4.0 → 3.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colisweb/rescript-toolkit",
3
- "version": "3.4.0",
3
+ "version": "3.4.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "clean": "rescript clean",
@@ -23,7 +23,7 @@ let make = () => {
23
23
  Js.log(tabValue)
24
24
  }}
25
25
  buttons={[
26
- {label: "Option 1"->React.string, value: "test"},
26
+ {label: "Option 1"->React.string, value: "test", count: 10},
27
27
  {label: "Option 2"->React.string, onClick: _ => alert("clicked on option 2")},
28
28
  {label: "Option 3"->React.string},
29
29
  ]}
@@ -7,6 +7,7 @@ type button<'value> = {
7
7
  onClick?: ReactEvent.Mouse.t => unit,
8
8
  label: React.element,
9
9
  value?: 'value,
10
+ count?: int,
10
11
  }
11
12
 
12
13
  type buttons<'value> = array<button<'value>>
@@ -21,13 +22,18 @@ let make = (
21
22
  ) => {
22
23
  let isMd = size === #md
23
24
  let (activeIndex, setActiveIndex) = React.useState(() => defaultActiveIndex)
25
+ let previousIndex = Toolkit__Hooks.usePrevious(activeIndex)
24
26
 
25
- React.useEffect1(() => {
26
- onIndexChange->Option.forEach(fn =>
27
- fn(activeIndex, (buttons[activeIndex]->Option.getUnsafe).value)
28
- )
27
+ React.useEffect2(() => {
28
+ onIndexChange->Option.forEach(fn => {
29
+ switch previousIndex {
30
+ | Some(previousIndex) if previousIndex !== activeIndex =>
31
+ fn(activeIndex, (buttons[activeIndex]->Option.getUnsafe).value)
32
+ | _ => ()
33
+ }
34
+ })
29
35
  None
30
- }, [activeIndex])
36
+ }, (activeIndex, previousIndex))
31
37
 
32
38
  <div
33
39
  className={cx([
@@ -48,6 +54,19 @@ let make = (
48
54
  }}
49
55
  className={cx([activeIndex === i ? "selected" : ""])}>
50
56
  {button.label}
57
+ {button.count->Option.mapWithDefault(React.null, count => {
58
+ <span
59
+ className={cx([
60
+ "ml-2 bg-gray-300/40 rounded px-1.5 ",
61
+ switch size {
62
+ | #md => "text-xs"
63
+ | #xl => "text-sm"
64
+ },
65
+ activeIndex === i ? "text-white fn" : "text-black",
66
+ ])}>
67
+ {count->React.int}
68
+ </span>
69
+ })}
51
70
  </button>
52
71
  })
53
72
  ->React.array}
@@ -7,6 +7,7 @@ type button<'value> = {
7
7
  onClick?: ReactEvent.Mouse.t => unit,
8
8
  label: React.element,
9
9
  value?: 'value,
10
+ count?: int,
10
11
  }
11
12
 
12
13
  type buttons<'value> = array<button<'value>>