@companix/uikit 0.0.2 → 0.0.3

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.
Files changed (254) hide show
  1. package/dist/bundle.es.js +61 -0
  2. package/dist/bundle.es10.js +87 -0
  3. package/dist/bundle.es11.js +75 -0
  4. package/dist/bundle.es12.js +38 -0
  5. package/dist/bundle.es13.js +33 -0
  6. package/dist/bundle.es14.js +18 -0
  7. package/dist/bundle.es15.js +11 -0
  8. package/dist/bundle.es16.js +35 -0
  9. package/dist/bundle.es17.js +27 -0
  10. package/dist/bundle.es18.js +23 -0
  11. package/dist/bundle.es19.js +24 -0
  12. package/dist/bundle.es20.js +15 -0
  13. package/dist/bundle.es21.js +41 -0
  14. package/dist/bundle.es22.js +18 -0
  15. package/dist/bundle.es23.js +36 -0
  16. package/dist/bundle.es24.js +23 -0
  17. package/dist/bundle.es25.js +42 -0
  18. package/dist/bundle.es26.js +114 -0
  19. package/dist/bundle.es27.js +115 -0
  20. package/dist/bundle.es28.js +38 -0
  21. package/dist/bundle.es29.js +13 -0
  22. package/dist/bundle.es3.js +45 -0
  23. package/dist/bundle.es30.js +27 -0
  24. package/dist/bundle.es31.js +38 -0
  25. package/dist/bundle.es35.js +43 -0
  26. package/dist/bundle.es36.js +442 -0
  27. package/dist/bundle.es37.js +24 -0
  28. package/dist/bundle.es38.js +26 -0
  29. package/dist/bundle.es39.js +83 -0
  30. package/dist/bundle.es4.js +14 -0
  31. package/dist/bundle.es40.js +20 -0
  32. package/dist/bundle.es41.js +16 -0
  33. package/dist/bundle.es43.js +7 -0
  34. package/dist/bundle.es44.js +23 -0
  35. package/dist/bundle.es45.js +16 -0
  36. package/dist/bundle.es46.js +72 -0
  37. package/dist/bundle.es47.js +70 -0
  38. package/dist/bundle.es48.js +34 -0
  39. package/dist/bundle.es49.js +70 -0
  40. package/dist/bundle.es5.js +88 -0
  41. package/dist/bundle.es50.js +33 -0
  42. package/dist/bundle.es51.js +78 -0
  43. package/dist/bundle.es52.js +51 -0
  44. package/dist/bundle.es53.js +45 -0
  45. package/dist/bundle.es54.js +40 -0
  46. package/dist/bundle.es55.js +36 -0
  47. package/dist/bundle.es56.js +9 -0
  48. package/dist/bundle.es57.js +9 -0
  49. package/dist/bundle.es58.js +7 -0
  50. package/dist/bundle.es59.js +9 -0
  51. package/dist/bundle.es6.js +87 -0
  52. package/dist/bundle.es60.js +8 -0
  53. package/dist/bundle.es61.js +19 -0
  54. package/dist/bundle.es62.js +15 -0
  55. package/dist/bundle.es63.js +22 -0
  56. package/dist/bundle.es64.js +45 -0
  57. package/dist/bundle.es65.js +28 -0
  58. package/dist/bundle.es66.js +34 -0
  59. package/dist/bundle.es67.js +6 -0
  60. package/dist/bundle.es68.js +12 -0
  61. package/dist/bundle.es69.js +47 -0
  62. package/dist/bundle.es7.js +80 -0
  63. package/dist/bundle.es70.js +11 -0
  64. package/dist/bundle.es71.js +18 -0
  65. package/dist/bundle.es72.js +32 -0
  66. package/dist/bundle.es73.js +35 -0
  67. package/dist/bundle.es74.js +6 -0
  68. package/dist/bundle.es75.js +7 -0
  69. package/dist/bundle.es76.js +7 -0
  70. package/dist/bundle.es77.js +11 -0
  71. package/dist/bundle.es78.js +17 -0
  72. package/dist/bundle.es79.js +7 -0
  73. package/dist/bundle.es8.js +29 -0
  74. package/dist/bundle.es80.js +17 -0
  75. package/dist/bundle.es81.js +25 -0
  76. package/dist/bundle.es9.js +47 -0
  77. package/dist/types/Button/index.d.ts +20 -0
  78. package/dist/types/ButtonGroup/index.d.ts +7 -0
  79. package/dist/types/Checkbox/index.d.ts +10 -0
  80. package/dist/types/Countdown/index.d.ts +7 -0
  81. package/dist/types/DateInput/index.d.ts +13 -0
  82. package/dist/types/DatePicker/Calendar.d.ts +14 -0
  83. package/dist/types/DatePicker/CalendarHeader.d.ts +10 -0
  84. package/dist/types/DatePicker/index.d.ts +12 -0
  85. package/dist/types/Dialog/index.d.ts +12 -0
  86. package/dist/types/DialogAlert/Alert.d.ts +19 -0
  87. package/dist/types/DialogAlert/Viewport.d.ts +8 -0
  88. package/dist/types/DialogAlert/index.d.ts +11 -0
  89. package/dist/types/Drawer/index.d.ts +13 -0
  90. package/dist/types/File/index.d.ts +10 -0
  91. package/dist/types/Form/index.d.ts +17 -0
  92. package/dist/types/Icon/index.d.ts +7 -0
  93. package/dist/types/LoadButton/index.d.ts +5 -0
  94. package/dist/types/NumberInput/index.d.ts +6 -0
  95. package/dist/types/OptionItem/OptionItem.d.ts +7 -0
  96. package/dist/types/OptionItem/OptionsList.d.ts +8 -0
  97. package/dist/types/Popover/index.d.ts +26 -0
  98. package/dist/types/Radio/index.d.ts +20 -0
  99. package/dist/types/Scrollable/ImitateScroll.d.ts +10 -0
  100. package/dist/types/Scrollable/index.d.ts +33 -0
  101. package/dist/types/Select/SelectInput.d.ts +14 -0
  102. package/dist/types/Select/index.d.ts +15 -0
  103. package/dist/types/SelectTags/index.d.ts +15 -0
  104. package/dist/types/Spinner/index.d.ts +8 -0
  105. package/dist/types/Stepper/index.d.ts +8 -0
  106. package/dist/types/Switch/index.d.ts +10 -0
  107. package/dist/types/Tabs/index.d.ts +13 -0
  108. package/dist/types/TextArea/index.d.ts +7 -0
  109. package/dist/types/Toaster/RemoveListener.d.ts +3 -0
  110. package/dist/types/Toaster/Toast.d.ts +9 -0
  111. package/dist/types/Toaster/Viewport.d.ts +13 -0
  112. package/dist/types/Toaster/index.d.ts +15 -0
  113. package/dist/types/Tooltip/index.d.ts +8 -0
  114. package/dist/types/__hooks/use-frooze-closing.d.ts +5 -0
  115. package/dist/types/__hooks/use-loading.d.ts +8 -0
  116. package/dist/types/__hooks/use-local-storage.d.ts +1 -0
  117. package/dist/types/__hooks/use-popover-position.d.ts +8 -0
  118. package/dist/types/__hooks/use-previos.d.ts +2 -0
  119. package/dist/types/__hooks/use-resize.d.ts +3 -0
  120. package/dist/types/__hooks/use-scrollbox.d.ts +7 -0
  121. package/dist/types/__hooks/use-stepper-input.d.ts +13 -0
  122. package/dist/types/__hooks/use-update.d.ts +1 -0
  123. package/dist/types/__hooks/useCalendar.d.ts +17 -0
  124. package/dist/types/__hooks/useCalendarOptions-copy.d.ts +22 -0
  125. package/dist/types/__hooks/useCalendarOptions.d.ts +31 -0
  126. package/dist/types/__libs/calendar.d.ts +25 -0
  127. package/dist/types/__utils/utils.d.ts +25 -0
  128. package/dist/types/index.d.ts +31 -0
  129. package/dist/types/types.d.ts +13 -0
  130. package/dist/uikit.css +1 -0
  131. package/package.json +10 -6
  132. package/.eslintrc +0 -54
  133. package/declaration.d.ts +0 -4
  134. package/index.html +0 -12
  135. package/playground/App.tsx +0 -166
  136. package/playground/Example.tsx +0 -14
  137. package/playground/Test.tsx +0 -44
  138. package/playground/animation-test-1/index.scss +0 -20
  139. package/playground/animation-test-1/index.tsx +0 -17
  140. package/playground/animation-test-2/index.scss +0 -62
  141. package/playground/animation-test-2/index.tsx +0 -32
  142. package/playground/bootstrap.tsx +0 -19
  143. package/playground/buttons/index.tsx +0 -132
  144. package/playground/checkbox/index.tsx +0 -64
  145. package/playground/date-input/index.tsx +0 -45
  146. package/playground/date-picker/index.tsx +0 -41
  147. package/playground/dialog/index.tsx +0 -92
  148. package/playground/dialog-alert/index.tsx +0 -47
  149. package/playground/drawer/index.tsx +0 -55
  150. package/playground/index.css +0 -33
  151. package/playground/index.scss +0 -270
  152. package/playground/input/index.tsx +0 -112
  153. package/playground/number-inputs/index.tsx +0 -50
  154. package/playground/popovers/index.tsx +0 -70
  155. package/playground/radio-group/index.tsx +0 -69
  156. package/playground/select/index.tsx +0 -72
  157. package/playground/select-tags/index.tsx +0 -36
  158. package/playground/styles.scss +0 -2
  159. package/playground/switch/index.tsx +0 -44
  160. package/playground/tabs/index.tsx +0 -16
  161. package/playground/test.scss +0 -0
  162. package/playground/text-area/index.tsx +0 -17
  163. package/playground/text-input/index.tsx +0 -12
  164. package/playground/toaster/index.tsx +0 -156
  165. package/playground/tooltip/index.tsx +0 -26
  166. package/src/Button/Button.scss +0 -128
  167. package/src/Button/index.tsx +0 -72
  168. package/src/ButtonGroup/ButtonGroup.scss +0 -18
  169. package/src/ButtonGroup/index.tsx +0 -20
  170. package/src/Checkbox/Checkbox.scss +0 -115
  171. package/src/Checkbox/index.tsx +0 -46
  172. package/src/Countdown/index.tsx +0 -54
  173. package/src/DateInput/DateInput.scss +0 -11
  174. package/src/DateInput/index.tsx +0 -96
  175. package/src/DatePicker/Calendar.scss +0 -125
  176. package/src/DatePicker/Calendar.tsx +0 -157
  177. package/src/DatePicker/CalendarHeader.tsx +0 -139
  178. package/src/DatePicker/DatePicker.scss +0 -0
  179. package/src/DatePicker/index.tsx +0 -177
  180. package/src/Dialog/Dialog.scss +0 -25
  181. package/src/Dialog/Popup.scss +0 -55
  182. package/src/Dialog/index.tsx +0 -31
  183. package/src/DialogAlert/Alert.scss +0 -52
  184. package/src/DialogAlert/Alert.tsx +0 -78
  185. package/src/DialogAlert/Viewport.tsx +0 -52
  186. package/src/DialogAlert/index.tsx +0 -37
  187. package/src/Drawer/Drawer.scss +0 -112
  188. package/src/Drawer/index.tsx +0 -46
  189. package/src/File/index.tsx +0 -60
  190. package/src/Form/Form.scss +0 -70
  191. package/src/Form/Input.scss +0 -24
  192. package/src/Form/index.tsx +0 -131
  193. package/src/Icon/icon.scss +0 -18
  194. package/src/Icon/index.tsx +0 -43
  195. package/src/LoadButton/index.tsx +0 -17
  196. package/src/NumberInput/index.tsx +0 -74
  197. package/src/OptionItem/Option.scss +0 -89
  198. package/src/OptionItem/OptionItem.tsx +0 -49
  199. package/src/OptionItem/OptionsList.tsx +0 -26
  200. package/src/Popover/Popover.scss +0 -80
  201. package/src/Popover/index.tsx +0 -117
  202. package/src/Radio/Radio.scss +0 -148
  203. package/src/Radio/index.tsx +0 -68
  204. package/src/Scrollable/ImitateScroll.tsx +0 -141
  205. package/src/Scrollable/Scrollable.scss +0 -50
  206. package/src/Scrollable/index.tsx +0 -141
  207. package/src/Select/Select.scss +0 -80
  208. package/src/Select/SelectInput.tsx +0 -131
  209. package/src/Select/index.tsx +0 -134
  210. package/src/SelectTags/SelectTags.scss +0 -66
  211. package/src/SelectTags/index.tsx +0 -192
  212. package/src/Spinner/Spinner.scss +0 -14
  213. package/src/Spinner/index.tsx +0 -19
  214. package/src/Stepper/StepperInput.scss +0 -35
  215. package/src/Stepper/index.tsx +0 -76
  216. package/src/Switch/Switch.scss +0 -102
  217. package/src/Switch/index.tsx +0 -49
  218. package/src/Tabs/Tabs.scss +0 -58
  219. package/src/Tabs/index.tsx +0 -89
  220. package/src/TextArea/TextArea.scss +0 -34
  221. package/src/TextArea/index.tsx +0 -51
  222. package/src/Toaster/RemoveListener.tsx +0 -11
  223. package/src/Toaster/Toast.tsx +0 -69
  224. package/src/Toaster/Toaster.scss +0 -151
  225. package/src/Toaster/Viewport.tsx +0 -117
  226. package/src/Toaster/index.tsx +0 -52
  227. package/src/Tooltip/Tooltip.scss +0 -28
  228. package/src/Tooltip/index.tsx +0 -33
  229. package/src/__hooks/use-frooze-closing.ts +0 -51
  230. package/src/__hooks/use-loading.ts +0 -34
  231. package/src/__hooks/use-local-storage.ts +0 -19
  232. package/src/__hooks/use-popover-position.ts +0 -24
  233. package/src/__hooks/use-previos.ts +0 -25
  234. package/src/__hooks/use-resize.ts +0 -41
  235. package/src/__hooks/use-scrollbox.ts +0 -45
  236. package/src/__hooks/use-stepper-input.ts +0 -82
  237. package/src/__hooks/use-update.ts +0 -19
  238. package/src/__hooks/useCalendar.ts +0 -104
  239. package/src/__hooks/useCalendarOptions-copy.ts +0 -87
  240. package/src/__hooks/useCalendarOptions.ts +0 -68
  241. package/src/__libs/calendar.ts +0 -175
  242. package/src/__utils/utils.ts +0 -137
  243. package/src/css.scss +0 -120
  244. package/src/index.scss +0 -22
  245. package/src/index.ts +0 -36
  246. package/src/mixins.scss +0 -99
  247. package/src/theme.scss +0 -103
  248. package/src/types.ts +0 -14
  249. package/tailwind.config.js +0 -91
  250. package/themes/classic/animations.scss +0 -179
  251. package/themes/classic/classic.scss +0 -493
  252. package/tsconfig.json +0 -27
  253. package/vite.build.ts +0 -35
  254. package/vite.config.ts +0 -33
@@ -1,117 +0,0 @@
1
- import * as ToasterPrimitive from '@radix-ui/react-toast'
2
- import { varToStyle } from '@companix/utils-browser'
3
- import { useCallback, useMemo, useState, useImperativeHandle, forwardRef } from 'react'
4
- import { Toast } from './Toast'
5
- import { InnerToast } from '.'
6
-
7
- enum SwipesDirections {
8
- 'top-center' = 'up',
9
- 'top-left' = 'left',
10
- 'top-right' = 'right',
11
- 'bottom-center' = 'down',
12
- 'bottom-left' = 'left',
13
- 'bottom-right' = 'right'
14
- }
15
-
16
- export interface ViewportProps {
17
- side?: 'top' | 'bottom'
18
- align?: 'center' | 'left' | 'right'
19
- gap?: number // gap between toasts
20
- swipeThreshold?: number
21
- duration?: number
22
- closeIcon?: React.ReactNode
23
- }
24
-
25
- export interface ViewportRef {
26
- addToast: (toast: InnerToast) => void
27
- }
28
-
29
- export const Viewport = forwardRef<ViewportRef, ViewportProps>((props, ref) => {
30
- const { side = 'top', align = 'center', gap = 14, duration, swipeThreshold, closeIcon } = props
31
- const [toasters, setToasters] = useState<InnerToast[]>([])
32
-
33
- const refs = useMemo((): { [id: string]: HTMLLIElement } => {
34
- return {}
35
- }, [])
36
-
37
- useImperativeHandle(
38
- ref,
39
- () => {
40
- return {
41
- addToast: (toast) => {
42
- setToasters((state) => [...state, toast])
43
- }
44
- }
45
- },
46
- []
47
- )
48
-
49
- const applyOffsets = useCallback((toasters: InnerToast[]) => {
50
- toasters.forEach(({ id }, index) => {
51
- let offset = 0
52
-
53
- for (let i = index + 1; i < toasters.length; i++) {
54
- if (refs[toasters[i].id]) {
55
- offset += refs[toasters[i].id].clientHeight + gap
56
- }
57
- }
58
-
59
- if (refs[id]) {
60
- refs[id].style.setProperty('--offset', `${offset}px`)
61
- }
62
- })
63
- }, [])
64
-
65
- const handleClose = (id: string) => {
66
- setToasters((state) => {
67
- const nextState = [...state]
68
- const index = nextState.findIndex((item) => item.id === id)
69
-
70
- if (index !== -1) {
71
- nextState.splice(index, 1)
72
- }
73
-
74
- return nextState
75
- })
76
- }
77
-
78
- return (
79
- <ToasterPrimitive.Provider
80
- duration={duration}
81
- swipeThreshold={swipeThreshold}
82
- swipeDirection={SwipesDirections[`${side}-${align}`]}
83
- >
84
- {toasters.map(({ id, ...toast }) => (
85
- <Toast
86
- {...toast}
87
- key={`toaster-${id}`}
88
- closeIcon={closeIcon}
89
- onInitialized={(ref) => {
90
- refs[id] = ref
91
- applyOffsets(toasters)
92
- }}
93
- onRemoving={() => {
94
- delete refs[id]
95
- applyOffsets(toasters)
96
- }}
97
- onRemoved={() => {
98
- if (refs[id]) {
99
- // Если коллбек onRemoved вызвается при истечении duration,
100
- // мы не перехватываем событие onRemoving и не изменяем позиции текущих тостов
101
- // (автоматическое закрытие тостов всегда происходит в порядке открытия)
102
- delete refs[id]
103
- }
104
-
105
- handleClose(id)
106
- }}
107
- />
108
- ))}
109
- <ToasterPrimitive.Viewport
110
- data-side={side}
111
- data-align={align}
112
- className="toaster-viewport"
113
- style={varToStyle({ '--toasters-gap': `${gap}px` })}
114
- />
115
- </ToasterPrimitive.Provider>
116
- )
117
- })
@@ -1,52 +0,0 @@
1
- import { hash } from '@companix/utils-js'
2
- import { Viewport, ViewportProps, ViewportRef } from './Viewport'
3
- import { useMemo, useRef } from 'react'
4
-
5
- export interface ToastOptions {
6
- appearance?: 'primary' | 'neutral' | 'positive' | 'negative' | 'warning'
7
- icon?: React.ReactNode
8
- title?: React.ReactNode
9
- description?: React.ReactNode
10
- duration?: number
11
- }
12
-
13
- export interface InnerToast extends ToastOptions {
14
- id: string
15
- }
16
-
17
- export const createToaster = (rootProps: ViewportProps = {}) => {
18
- const store = {
19
- emit: (toast: InnerToast) => {
20
- console.error('uninitialized', toast)
21
- }
22
- }
23
-
24
- return {
25
- send: (toast: ToastOptions) => {
26
- store.emit({ ...toast, id: hash() })
27
- },
28
- Viewport: (props: ViewportProps = {}) => {
29
- const ref = useRef<ViewportRef>(null)
30
-
31
- useMemo(() => {
32
- store.emit = (value) => {
33
- if (ref.current) {
34
- ref.current.addToast(value)
35
- }
36
- }
37
- }, [])
38
-
39
- return (
40
- <Viewport
41
- ref={ref}
42
- align={props.align ?? rootProps.align}
43
- closeIcon={props.closeIcon ?? rootProps.closeIcon}
44
- duration={props.duration ?? rootProps.duration}
45
- gap={props.gap ?? rootProps.gap}
46
- side={props.side ?? rootProps.side}
47
- swipeThreshold={props.swipeThreshold ?? rootProps.swipeThreshold}
48
- />
49
- )
50
- }
51
- }
52
- }
@@ -1,28 +0,0 @@
1
- @use '../mixins.scss';
2
-
3
- .tooltip .popover {
4
- &-content {
5
- @include mixins.use-styles(tooltip);
6
- max-height: none;
7
- }
8
-
9
- &-arrow {
10
- &-fill {
11
- fill: mixins.get-var-name(tooltip, background);
12
- }
13
- }
14
- }
15
-
16
- .tooltip {
17
- &[data-appearance='default'] {
18
- &[data-state='open'] {
19
- transform-origin: var(--radix-popover-content-transform-origin);
20
- @include mixins.use-styles(tooltip, in);
21
- }
22
-
23
- &[data-state='closed'] {
24
- transform-origin: var(--radix-popover-content-transform-origin);
25
- @include mixins.use-styles(tooltip, out);
26
- }
27
- }
28
- }
@@ -1,33 +0,0 @@
1
- import { useState } from 'react'
2
- import { Popover } from '..'
3
- import { Side } from '../Popover'
4
-
5
- interface TooltipProps {
6
- children: React.ReactNode
7
- content: React.ReactNode
8
- side?: Side
9
- }
10
-
11
- export const Tooltip = ({ children, content, side }: TooltipProps) => {
12
- const [open, setOpen] = useState(false)
13
-
14
- return (
15
- <Popover
16
- showArrows
17
- className="tooltip"
18
- open={open}
19
- side={side}
20
- content={() => content}
21
- triggerProps={{
22
- onMouseEnter: () => {
23
- setOpen(true)
24
- },
25
- onMouseLeave: () => {
26
- setOpen(false)
27
- }
28
- }}
29
- >
30
- {children}
31
- </Popover>
32
- )
33
- }
@@ -1,51 +0,0 @@
1
- import { useCallback, useRef } from 'react'
2
-
3
- // При смене выбранного значения в select-компоненте может измениться ширина кнопки.
4
- // Это приводит к пересчёту позиции popover, привязанного к её размерам и координатам.
5
- // Во время анимированного закрытия popover это способно вызвать нежелательные визуальные артефакты.
6
-
7
- // Чтобы запретить данное поведение, разработан хук useFroozeClosing, который фиксирует и применяет значения ширины и позиции открытого popover-окна перед событием закрытия.
8
-
9
- export const useFroozeClosing = () => {
10
- const popoverRef = useRef<HTMLDivElement>(null)
11
- const stateRef = useRef<{ cb: null | (() => void) }>({ cb: null })
12
-
13
- const froozePopoverPosition = useCallback(() => {
14
- if (popoverRef.current && popoverRef.current.parentElement) {
15
- const parent = popoverRef.current.parentElement
16
-
17
- const width = parent.style.getPropertyValue('--radix-popper-anchor-width')
18
- const position = parent.style.getPropertyValue('transform')
19
-
20
- const observer = new MutationObserver(() => {
21
- if (parent.style.transform !== position) {
22
- parent.style.setProperty('transform', position)
23
- }
24
- })
25
-
26
- observer.observe(parent, {
27
- attributes: true,
28
- attributeFilter: ['style']
29
- })
30
-
31
- stateRef.current.cb = () => {
32
- observer.disconnect()
33
- }
34
-
35
- popoverRef.current.style.setProperty('--radix-popover-trigger-width', width)
36
- }
37
- }, [])
38
-
39
- const handleAnimationEnd = useCallback(() => {
40
- if (stateRef.current.cb) {
41
- stateRef.current.cb()
42
- stateRef.current.cb = null
43
- }
44
- }, [])
45
-
46
- return {
47
- popoverRef,
48
- handleAnimationEnd,
49
- froozePopoverPosition
50
- }
51
- }
@@ -1,34 +0,0 @@
1
- import { useState } from 'react'
2
-
3
- export interface UseLoadingProps<T = unknown> {
4
- onClick: (startLoad: () => void, param: T) => Promise<any>
5
- }
6
-
7
- export const useLoading = <T = unknown>({ onClick }: UseLoadingProps<T>) => {
8
- const [state, setState] = useState({ isLoading: false, isError: false })
9
-
10
- const handleClick = (param: T) => {
11
- if (!state.isLoading) {
12
- onClick(() => setState({ isLoading: true, isError: false }), param)
13
- .then(() => {
14
- setState({
15
- isLoading: false,
16
- isError: false
17
- })
18
- })
19
- .catch((e) => {
20
- console.log(e)
21
-
22
- setState({
23
- isLoading: false,
24
- isError: true
25
- })
26
- })
27
- }
28
- }
29
-
30
- return {
31
- ...state,
32
- handleClick
33
- }
34
- }
@@ -1,19 +0,0 @@
1
- import { useState, useEffect } from 'react'
2
-
3
- export const useLocalStorage = <T>(token: string, defaultValue: T) => {
4
- const [state, setState] = useState<T>(() => {
5
- const initialValue = localStorage.getItem(token)
6
-
7
- if (initialValue) {
8
- return JSON.parse(initialValue)
9
- }
10
-
11
- return defaultValue
12
- })
13
-
14
- useEffect(() => {
15
- localStorage.setItem(token, JSON.stringify(state))
16
- }, [state])
17
-
18
- return [state, setState] as [T, React.Dispatch<React.SetStateAction<T>>]
19
- }
@@ -1,24 +0,0 @@
1
- import { useRef } from 'react'
2
- import { px } from '@companix/utils-browser'
3
-
4
- export const usePopoverLeftValue = () => {
5
- const popoverRef = useRef<HTMLDivElement>(null)
6
-
7
- return {
8
- popoverRef,
9
- getLeftValue: () => {
10
- return popoverRef.current?.style.left ?? '0px'
11
- }
12
- }
13
- }
14
-
15
- export const useButtonWidth = () => {
16
- const buttonRef = useRef<HTMLButtonElement>(null)
17
-
18
- return {
19
- buttonRef,
20
- getWidthValue: () => {
21
- return px(buttonRef.current?.offsetWidth ?? 0)
22
- }
23
- }
24
- }
@@ -1,25 +0,0 @@
1
- import { useRef } from 'react'
2
-
3
- export const usePrevious = <T>(value: T) => {
4
- const currentRef = useRef(value)
5
- const previousRef = useRef<T>()
6
-
7
- if (currentRef.current !== value) {
8
- previousRef.current = currentRef.current
9
- currentRef.current = value
10
- }
11
-
12
- return previousRef.current
13
- }
14
-
15
- export const usePreviousChanged = <T>(value: T) => {
16
- const currentRef = useRef(value)
17
- const previousRef = useRef<T>()
18
-
19
- if (currentRef.current !== value) {
20
- previousRef.current = currentRef.current
21
- currentRef.current = value
22
- }
23
-
24
- return previousRef.current
25
- }
@@ -1,41 +0,0 @@
1
- import { useCallback, useRef } from 'react'
2
-
3
- type ResizeText = [React.RefObject<HTMLTextAreaElement>, () => void]
4
-
5
- const useResizeTextarea = (
6
- onResize: ((el: HTMLTextAreaElement) => void) | undefined,
7
- grow: boolean
8
- ): ResizeText => {
9
- const elementRef = useRef<HTMLTextAreaElement>(null)
10
- const currentScrollHeight = useRef<number>(undefined)
11
-
12
- const resizeElement = useCallback(
13
- (el: HTMLTextAreaElement) => {
14
- if (grow && el.offsetParent) {
15
- el.style.height = ''
16
- el.style.height = `${el.scrollHeight}px`
17
-
18
- if (el.scrollHeight !== currentScrollHeight.current && onResize) {
19
- onResize(el)
20
- currentScrollHeight.current = el.scrollHeight
21
- }
22
- }
23
- },
24
- [grow, onResize]
25
- )
26
-
27
- const resize = useCallback(() => {
28
- const el = elementRef.current
29
-
30
- if (!el) {
31
- /* istanbul ignore next: нет возможности воспроизвести */
32
- return
33
- }
34
-
35
- resizeElement(el)
36
- }, [elementRef, resizeElement])
37
-
38
- return [elementRef, resize]
39
- }
40
-
41
- export { useResizeTextarea }
@@ -1,45 +0,0 @@
1
- import { useCallback, useRef } from 'react'
2
-
3
- interface UseScrollListControllerReturn {
4
- scrollBoxRef: React.RefObject<HTMLDivElement>
5
- optionsWrapperRef: React.RefObject<HTMLDivElement>
6
- scrollToElement: (index: number, center?: boolean) => void
7
- }
8
-
9
- export const useScrollListController = (): UseScrollListControllerReturn => {
10
- const scrollBoxRef = useRef<HTMLDivElement>(null)
11
- const optionsWrapperRef = useRef<HTMLDivElement>(null)
12
-
13
- const scrollToElement = useCallback(
14
- (index: number, center = false) => {
15
- const dropdown = scrollBoxRef.current
16
- const optionsWrapper = optionsWrapperRef.current
17
-
18
- if (!dropdown || !optionsWrapper || index < 0 || index > optionsWrapper.children.length) {
19
- return
20
- }
21
-
22
- const item = optionsWrapper.children[index] as HTMLElement | null
23
- /* istanbul ignore if: проверка для TS (ситуация, когда среди children нет элемента с index, маловероятна) */
24
- if (!item) {
25
- return
26
- }
27
-
28
- const dropdownHeight = dropdown.offsetHeight
29
- const scrollTop = dropdown.scrollTop
30
- const itemTop = item.offsetTop
31
- const itemHeight = item.offsetHeight
32
-
33
- if (center) {
34
- dropdown.scrollTop = itemTop - dropdownHeight / 2 + itemHeight / 2
35
- } else if (itemTop + itemHeight > dropdownHeight + scrollTop) {
36
- dropdown.scrollTop = itemTop - dropdownHeight + itemHeight
37
- } else if (itemTop < scrollTop) {
38
- dropdown.scrollTop = itemTop
39
- }
40
- },
41
- [optionsWrapperRef, scrollBoxRef]
42
- )
43
-
44
- return { scrollToElement, scrollBoxRef, optionsWrapperRef }
45
- }
@@ -1,82 +0,0 @@
1
- import { useRef, useState, useMemo, useCallback, useLayoutEffect } from 'react'
2
- import { normalize, truncateNumber, getFloatDigits } from '@companix/utils-js'
3
-
4
- export interface StepperInputOptions {
5
- value: number
6
- onChange: (value: number) => void
7
- step: number
8
- minValue?: number
9
- }
10
-
11
- // TODO: если onChange будет меньше minValue произойдет ошибка
12
- export const useStepperInput = ({ minValue, value, onChange, step }: StepperInputOptions) => {
13
- const inputRef = useRef<HTMLInputElement>(null)
14
-
15
- const precision = useMemo(() => {
16
- return getFloatDigits(step.toString())
17
- }, [step])
18
-
19
- const formatting = useCallback(
20
- (value: number) => {
21
- if (minValue && value < minValue) {
22
- return minValue.toFixed(precision)
23
- }
24
-
25
- return truncateNumber(value, precision)
26
- },
27
- [minValue, precision]
28
- )
29
-
30
- const [inputValue, setInputValue] = useState({
31
- value: formatting(value),
32
- cursor: 0
33
- })
34
-
35
- const setChanges = (value: number, i: number = 0) => {
36
- const nextValue = formatting(value)
37
-
38
- setInputValue({ value: nextValue, cursor: (inputRef.current?.selectionStart || 0) + i })
39
- onChange(+nextValue)
40
- }
41
-
42
- if (+inputValue.value !== value) {
43
- setInputValue({ value: formatting(value), cursor: 0 })
44
- }
45
-
46
- useLayoutEffect(() => {
47
- if (inputRef.current) {
48
- inputRef.current.setSelectionRange(inputValue.cursor, inputValue.cursor)
49
- inputRef.current.focus()
50
- }
51
- }, [inputValue])
52
-
53
- return {
54
- inputRef,
55
- value: inputValue.value,
56
- increment: () => {
57
- setChanges(normalize(value + step, precision))
58
- },
59
- decrement: () => {
60
- setChanges(normalize(value - step, precision))
61
- },
62
- handleChange: (e: React.ChangeEvent<HTMLInputElement>) => {
63
- const value = e.currentTarget.value.trim()
64
-
65
- if (value) {
66
- if (isNaN(+value) || value.includes('e')) {
67
- setChanges(+inputValue.value, -1)
68
- return
69
- }
70
-
71
- if (inputValue.value.includes('.') && inputValue.value.replace('.', '') === value) {
72
- if (inputRef.current && inputRef.current.selectionStart) {
73
- setChanges(+inputValue.value)
74
- return
75
- }
76
- }
77
- }
78
-
79
- setChanges(+value)
80
- }
81
- }
82
- }
@@ -1,19 +0,0 @@
1
- import { useEffect, useLayoutEffect, useRef } from 'react'
2
-
3
- export const useLayoutAndUpdate = (callback: () => void, deps?: React.DependencyList | undefined) => {
4
- const isRendered = useRef(false)
5
-
6
- useLayoutEffect(() => {
7
- isRendered.current = true
8
- callback()
9
- }, [])
10
-
11
- useEffect(() => {
12
- if (isRendered.current) {
13
- isRendered.current = false
14
- return
15
- }
16
-
17
- callback()
18
- }, deps)
19
- }
@@ -1,104 +0,0 @@
1
- import { CalendarProps } from '../DatePicker/Calendar'
2
- import { addMonths, subMonths, useDayDisableCheker } from '../__libs/calendar'
3
- import { DEFAULT_MAX_YEAR, DEFAULT_MIN_YEAR } from '../__utils/utils'
4
- import { useCallback, useState } from 'react'
5
-
6
- export interface UseCalendarDependencies
7
- extends Pick<
8
- CalendarProps,
9
- 'minDateTime' | 'maxDateTime' | 'shouldDisableDate' | 'disableFuture' | 'disablePast'
10
- > {
11
- disablePast?: boolean
12
- disableFuture?: boolean
13
- minDateTime?: Date
14
- maxDateTime?: Date
15
- value?: Date | null
16
- }
17
-
18
- export const useCalendar = ({
19
- value,
20
- disablePast,
21
- disableFuture,
22
- shouldDisableDate,
23
- minDateTime,
24
- maxDateTime
25
- }: UseCalendarDependencies) => {
26
- const [viewDate, setViewDate] = useState(value || new Date())
27
-
28
- const setPrevMonth = useCallback(() => {
29
- // onPrevMonth?.();
30
- setViewDate(subMonths(viewDate, 1))
31
- }, [viewDate])
32
- const setNextMonth = useCallback(() => {
33
- // onNextMonth?.();
34
- setViewDate(addMonths(viewDate, 1))
35
- }, [viewDate])
36
-
37
- const isDayDisabled = useDayDisableCheker({
38
- disableFuture,
39
- disablePast,
40
- shouldDisableDate,
41
- minDateTime,
42
- maxDateTime
43
- })
44
-
45
- const isMonthDisabled = useCallback(
46
- (month: number, year?: number): boolean => {
47
- const now = new Date()
48
- year = year || viewDate.getFullYear()
49
- const minMonth = minDateTime ? minDateTime.getMonth() : 0
50
- const maxMonth = maxDateTime ? maxDateTime.getMonth() : 11
51
- const minYear = minDateTime?.getFullYear() || DEFAULT_MIN_YEAR
52
- const maxYear = maxDateTime?.getFullYear() || DEFAULT_MAX_YEAR
53
-
54
- let isDisabled =
55
- year >= minYear && year <= maxYear
56
- ? (year === minYear && minMonth > month) || (year === maxYear && month > maxMonth)
57
- : true
58
-
59
- if (disableFuture) {
60
- isDisabled =
61
- isDisabled || (year === now.getFullYear() ? month > now.getMonth() : year > now.getFullYear())
62
- }
63
-
64
- if (disablePast) {
65
- isDisabled =
66
- isDisabled || (year === now.getFullYear() ? month < now.getMonth() : year < now.getFullYear())
67
- }
68
-
69
- return isDisabled
70
- },
71
- [disableFuture, disablePast, viewDate, minDateTime, maxDateTime]
72
- )
73
-
74
- const isYearDisabled = useCallback(
75
- (year: number): boolean => {
76
- const now = new Date()
77
- const minYear = minDateTime?.getFullYear() || DEFAULT_MIN_YEAR
78
- const maxYear = maxDateTime?.getFullYear() || DEFAULT_MAX_YEAR
79
-
80
- let isDisabled = minYear > year || year > maxYear
81
-
82
- if (disableFuture) {
83
- isDisabled = isDisabled || year > now.getFullYear()
84
- }
85
-
86
- if (disablePast) {
87
- isDisabled = isDisabled || year < now.getFullYear()
88
- }
89
-
90
- return isDisabled
91
- },
92
- [disableFuture, disablePast, minDateTime, maxDateTime]
93
- )
94
-
95
- return {
96
- viewDate,
97
- setViewDate,
98
- setPrevMonth,
99
- setNextMonth,
100
- isDayDisabled,
101
- isMonthDisabled,
102
- isYearDisabled
103
- }
104
- }