@companix/uikit 0.0.2 → 0.0.4

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 (222) hide show
  1. package/dist/Button/index.d.ts +20 -0
  2. package/dist/ButtonGroup/index.d.ts +7 -0
  3. package/dist/Checkbox/index.d.ts +10 -0
  4. package/dist/Countdown/index.d.ts +7 -0
  5. package/dist/DateInput/index.d.ts +13 -0
  6. package/dist/DatePicker/Calendar.d.ts +14 -0
  7. package/dist/DatePicker/CalendarHeader.d.ts +10 -0
  8. package/dist/DatePicker/index.d.ts +12 -0
  9. package/dist/Dialog/index.d.ts +12 -0
  10. package/dist/DialogAlert/Alert.d.ts +19 -0
  11. package/dist/DialogAlert/Viewport.d.ts +8 -0
  12. package/dist/DialogAlert/index.d.ts +11 -0
  13. package/dist/Drawer/index.d.ts +13 -0
  14. package/dist/File/index.d.ts +10 -0
  15. package/dist/Form/index.d.ts +17 -0
  16. package/dist/Icon/index.d.ts +7 -0
  17. package/dist/LoadButton/index.d.ts +5 -0
  18. package/dist/NumberInput/index.d.ts +6 -0
  19. package/dist/OptionItem/OptionItem.d.ts +7 -0
  20. package/dist/OptionItem/OptionsList.d.ts +8 -0
  21. package/dist/Popover/index.d.ts +26 -0
  22. package/dist/Radio/index.d.ts +20 -0
  23. package/dist/Scrollable/ImitateScroll.d.ts +10 -0
  24. package/dist/Scrollable/index.d.ts +33 -0
  25. package/dist/Select/SelectInput.d.ts +14 -0
  26. package/dist/Select/index.d.ts +15 -0
  27. package/dist/SelectTags/index.d.ts +15 -0
  28. package/dist/Spinner/index.d.ts +8 -0
  29. package/dist/Stepper/index.d.ts +8 -0
  30. package/dist/Switch/index.d.ts +10 -0
  31. package/dist/Tabs/index.d.ts +13 -0
  32. package/dist/TextArea/index.d.ts +7 -0
  33. package/dist/Toaster/RemoveListener.d.ts +3 -0
  34. package/dist/Toaster/Toast.d.ts +9 -0
  35. package/dist/Toaster/Viewport.d.ts +13 -0
  36. package/dist/Toaster/index.d.ts +15 -0
  37. package/dist/Tooltip/index.d.ts +8 -0
  38. package/dist/__hooks/use-frooze-closing.d.ts +5 -0
  39. package/dist/__hooks/use-loading.d.ts +8 -0
  40. package/dist/__hooks/use-local-storage.d.ts +1 -0
  41. package/dist/__hooks/use-popover-position.d.ts +8 -0
  42. package/dist/__hooks/use-previos.d.ts +2 -0
  43. package/dist/__hooks/use-resize.d.ts +3 -0
  44. package/dist/__hooks/use-scrollbox.d.ts +7 -0
  45. package/dist/__hooks/use-stepper-input.d.ts +13 -0
  46. package/dist/__hooks/use-update.d.ts +1 -0
  47. package/dist/__hooks/useCalendar.d.ts +17 -0
  48. package/dist/__hooks/useCalendarOptions-copy.d.ts +22 -0
  49. package/dist/__hooks/useCalendarOptions.d.ts +31 -0
  50. package/dist/__libs/calendar.d.ts +25 -0
  51. package/dist/__utils/utils.d.ts +25 -0
  52. package/dist/bundle.es.js +58 -0
  53. package/dist/bundle.es10.js +38 -0
  54. package/dist/bundle.es11.js +33 -0
  55. package/dist/bundle.es12.js +18 -0
  56. package/dist/bundle.es13.js +10 -0
  57. package/dist/bundle.es14.js +35 -0
  58. package/dist/bundle.es15.js +27 -0
  59. package/dist/bundle.es16.js +23 -0
  60. package/dist/bundle.es17.js +24 -0
  61. package/dist/bundle.es18.js +15 -0
  62. package/dist/bundle.es19.js +40 -0
  63. package/dist/bundle.es2.js +45 -0
  64. package/dist/bundle.es20.js +18 -0
  65. package/dist/bundle.es21.js +36 -0
  66. package/dist/bundle.es22.js +23 -0
  67. package/dist/bundle.es23.js +42 -0
  68. package/dist/bundle.es24.js +113 -0
  69. package/dist/bundle.es25.js +114 -0
  70. package/dist/bundle.es26.js +38 -0
  71. package/dist/bundle.es27.js +13 -0
  72. package/dist/bundle.es28.js +27 -0
  73. package/dist/bundle.es29.js +38 -0
  74. package/dist/bundle.es3.js +13 -0
  75. package/dist/bundle.es30.js +26 -0
  76. package/dist/bundle.es31.js +83 -0
  77. package/dist/bundle.es32.js +20 -0
  78. package/dist/bundle.es33.js +16 -0
  79. package/dist/bundle.es34.js +24 -0
  80. package/dist/bundle.es35.js +7 -0
  81. package/dist/bundle.es36.js +23 -0
  82. package/dist/bundle.es37.js +16 -0
  83. package/dist/bundle.es38.js +72 -0
  84. package/dist/bundle.es39.js +70 -0
  85. package/dist/bundle.es4.js +87 -0
  86. package/dist/bundle.es40.js +34 -0
  87. package/dist/bundle.es41.js +70 -0
  88. package/dist/bundle.es42.js +33 -0
  89. package/dist/bundle.es43.js +77 -0
  90. package/dist/bundle.es44.js +51 -0
  91. package/dist/bundle.es45.js +45 -0
  92. package/dist/bundle.es5.js +87 -0
  93. package/dist/bundle.es6.js +80 -0
  94. package/dist/bundle.es7.js +28 -0
  95. package/dist/bundle.es8.js +87 -0
  96. package/dist/bundle.es9.js +75 -0
  97. package/dist/index.d.ts +30 -0
  98. package/{src → dist}/index.scss +3 -0
  99. package/{src → dist}/mixins.scss +4 -3
  100. package/{src → dist}/theme.scss +0 -2
  101. package/dist/types.d.ts +13 -0
  102. package/package.json +10 -6
  103. package/.eslintrc +0 -54
  104. package/declaration.d.ts +0 -4
  105. package/index.html +0 -12
  106. package/playground/App.tsx +0 -166
  107. package/playground/Example.tsx +0 -14
  108. package/playground/Test.tsx +0 -44
  109. package/playground/animation-test-1/index.scss +0 -20
  110. package/playground/animation-test-1/index.tsx +0 -17
  111. package/playground/animation-test-2/index.scss +0 -62
  112. package/playground/animation-test-2/index.tsx +0 -32
  113. package/playground/bootstrap.tsx +0 -19
  114. package/playground/buttons/index.tsx +0 -132
  115. package/playground/checkbox/index.tsx +0 -64
  116. package/playground/date-input/index.tsx +0 -45
  117. package/playground/date-picker/index.tsx +0 -41
  118. package/playground/dialog/index.tsx +0 -92
  119. package/playground/dialog-alert/index.tsx +0 -47
  120. package/playground/drawer/index.tsx +0 -55
  121. package/playground/index.css +0 -33
  122. package/playground/index.scss +0 -270
  123. package/playground/input/index.tsx +0 -112
  124. package/playground/number-inputs/index.tsx +0 -50
  125. package/playground/popovers/index.tsx +0 -70
  126. package/playground/radio-group/index.tsx +0 -69
  127. package/playground/select/index.tsx +0 -72
  128. package/playground/select-tags/index.tsx +0 -36
  129. package/playground/styles.scss +0 -2
  130. package/playground/switch/index.tsx +0 -44
  131. package/playground/tabs/index.tsx +0 -16
  132. package/playground/test.scss +0 -0
  133. package/playground/text-area/index.tsx +0 -17
  134. package/playground/text-input/index.tsx +0 -12
  135. package/playground/toaster/index.tsx +0 -156
  136. package/playground/tooltip/index.tsx +0 -26
  137. package/src/Button/index.tsx +0 -72
  138. package/src/ButtonGroup/index.tsx +0 -20
  139. package/src/Checkbox/index.tsx +0 -46
  140. package/src/Countdown/index.tsx +0 -54
  141. package/src/DateInput/index.tsx +0 -96
  142. package/src/DatePicker/Calendar.tsx +0 -157
  143. package/src/DatePicker/CalendarHeader.tsx +0 -139
  144. package/src/DatePicker/index.tsx +0 -177
  145. package/src/Dialog/index.tsx +0 -31
  146. package/src/DialogAlert/Alert.tsx +0 -78
  147. package/src/DialogAlert/Viewport.tsx +0 -52
  148. package/src/DialogAlert/index.tsx +0 -37
  149. package/src/Drawer/index.tsx +0 -46
  150. package/src/File/index.tsx +0 -60
  151. package/src/Form/index.tsx +0 -131
  152. package/src/Icon/index.tsx +0 -43
  153. package/src/LoadButton/index.tsx +0 -17
  154. package/src/NumberInput/index.tsx +0 -74
  155. package/src/OptionItem/OptionItem.tsx +0 -49
  156. package/src/OptionItem/OptionsList.tsx +0 -26
  157. package/src/Popover/index.tsx +0 -117
  158. package/src/Radio/index.tsx +0 -68
  159. package/src/Scrollable/ImitateScroll.tsx +0 -141
  160. package/src/Scrollable/index.tsx +0 -141
  161. package/src/Select/SelectInput.tsx +0 -131
  162. package/src/Select/index.tsx +0 -134
  163. package/src/SelectTags/index.tsx +0 -192
  164. package/src/Spinner/index.tsx +0 -19
  165. package/src/Stepper/index.tsx +0 -76
  166. package/src/Switch/index.tsx +0 -49
  167. package/src/Tabs/index.tsx +0 -89
  168. package/src/TextArea/index.tsx +0 -51
  169. package/src/Toaster/RemoveListener.tsx +0 -11
  170. package/src/Toaster/Toast.tsx +0 -69
  171. package/src/Toaster/Viewport.tsx +0 -117
  172. package/src/Toaster/index.tsx +0 -52
  173. package/src/Tooltip/index.tsx +0 -33
  174. package/src/__hooks/use-frooze-closing.ts +0 -51
  175. package/src/__hooks/use-loading.ts +0 -34
  176. package/src/__hooks/use-local-storage.ts +0 -19
  177. package/src/__hooks/use-popover-position.ts +0 -24
  178. package/src/__hooks/use-previos.ts +0 -25
  179. package/src/__hooks/use-resize.ts +0 -41
  180. package/src/__hooks/use-scrollbox.ts +0 -45
  181. package/src/__hooks/use-stepper-input.ts +0 -82
  182. package/src/__hooks/use-update.ts +0 -19
  183. package/src/__hooks/useCalendar.ts +0 -104
  184. package/src/__hooks/useCalendarOptions-copy.ts +0 -87
  185. package/src/__hooks/useCalendarOptions.ts +0 -68
  186. package/src/__libs/calendar.ts +0 -175
  187. package/src/__utils/utils.ts +0 -137
  188. package/src/index.ts +0 -36
  189. package/src/types.ts +0 -14
  190. package/tailwind.config.js +0 -91
  191. package/themes/classic/animations.scss +0 -179
  192. package/themes/classic/classic.scss +0 -493
  193. package/tsconfig.json +0 -27
  194. package/vite.build.ts +0 -35
  195. package/vite.config.ts +0 -33
  196. /package/{src → dist}/Button/Button.scss +0 -0
  197. /package/{src → dist}/ButtonGroup/ButtonGroup.scss +0 -0
  198. /package/{src → dist}/Checkbox/Checkbox.scss +0 -0
  199. /package/{src → dist}/DateInput/DateInput.scss +0 -0
  200. /package/{src → dist}/DatePicker/Calendar.scss +0 -0
  201. /package/{src → dist}/DatePicker/DatePicker.scss +0 -0
  202. /package/{src → dist}/Dialog/Dialog.scss +0 -0
  203. /package/{src → dist}/Dialog/Popup.scss +0 -0
  204. /package/{src → dist}/DialogAlert/Alert.scss +0 -0
  205. /package/{src → dist}/Drawer/Drawer.scss +0 -0
  206. /package/{src → dist}/Form/Form.scss +0 -0
  207. /package/{src → dist}/Form/Input.scss +0 -0
  208. /package/{src → dist}/Icon/icon.scss +0 -0
  209. /package/{src → dist}/OptionItem/Option.scss +0 -0
  210. /package/{src → dist}/Popover/Popover.scss +0 -0
  211. /package/{src → dist}/Radio/Radio.scss +0 -0
  212. /package/{src → dist}/Scrollable/Scrollable.scss +0 -0
  213. /package/{src → dist}/Select/Select.scss +0 -0
  214. /package/{src → dist}/SelectTags/SelectTags.scss +0 -0
  215. /package/{src → dist}/Spinner/Spinner.scss +0 -0
  216. /package/{src → dist}/Stepper/StepperInput.scss +0 -0
  217. /package/{src → dist}/Switch/Switch.scss +0 -0
  218. /package/{src → dist}/Tabs/Tabs.scss +0 -0
  219. /package/{src → dist}/TextArea/TextArea.scss +0 -0
  220. /package/{src → dist}/Toaster/Toaster.scss +0 -0
  221. /package/{src → dist}/Tooltip/Tooltip.scss +0 -0
  222. /package/{src/css.scss → dist/css-properties.scss} +0 -0
@@ -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,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
- }
@@ -1,87 +0,0 @@
1
- import { DefaultMonths, getMonthMaxDay } from '../__utils/utils'
2
- import { range } from '@companix/utils-js'
3
- import { useMemo } from 'react'
4
-
5
- export const defaultMax = new Date(2050, 0, 1)
6
- export const defaultMin = new Date(1925, 0, 1)
7
-
8
- interface Options {
9
- min?: Date
10
- max?: Date
11
- now: Date
12
- }
13
-
14
- const getDate = (date: Date) => {
15
- return {
16
- day: date.getDate(),
17
- month: date.getMonth(),
18
- year: date.getFullYear()
19
- }
20
- }
21
-
22
- export const useCalendarOptions2 = ({ min = defaultMin, max = defaultMax, now }: Options) => {
23
- const max_values = useMemo(() => getDate(max), [max])
24
- const min_values = useMemo(() => getDate(min), [min])
25
- const now_values = useMemo(() => getDate(now), [now])
26
-
27
- const years = useMemo(() => {
28
- return range(max_values.year, min_values.year).map((value) => ({
29
- title: value.toString(),
30
- value
31
- }))
32
- }, [max_values.year, min_values.year])
33
-
34
- const months = useMemo(() => {
35
- const options = DefaultMonths.map((name, index) => ({
36
- title: name,
37
- value: index
38
- }))
39
-
40
- if (min_values.month && now_values.year === min_values.year) {
41
- const i = options.findIndex(({ value }) => value === min_values.month)
42
- options.splice(0, i)
43
- }
44
-
45
- if (max_values.month && now_values.year === max_values.year) {
46
- const i = options.findIndex(({ value }) => value === max_values.month)
47
- options.splice(i + 1, options.length)
48
- }
49
-
50
- return options
51
- }, [now_values.year, min_values.year, min_values.month, max_values.year, max_values.month])
52
-
53
- const days = useMemo(() => {
54
- if (now_values.month === 0) {
55
- return []
56
- }
57
-
58
- const maxDays = getMonthMaxDay(now_values.month, now_values.year)
59
-
60
- const options = Array.from({ length: maxDays }, (_, i) => ({
61
- title: `${i + 1}`,
62
- value: i + 1
63
- }))
64
-
65
- if (
66
- min_values.day &&
67
- now_values.month === min_values.month &&
68
- now_values.year === min_values.year
69
- ) {
70
- const i = options.findIndex(({ value }) => value === min_values.day)
71
- options.splice(0, i)
72
- }
73
-
74
- if (
75
- max_values.day &&
76
- now_values.month === max_values.month &&
77
- now_values.year === max_values.year
78
- ) {
79
- const i = options.findIndex(({ value }) => value === max_values.day)
80
- options.splice(i + 1, options.length)
81
- }
82
-
83
- return options
84
- }, [now_values.month, now_values.year, min, max])
85
-
86
- return { years, months, days }
87
- }
@@ -1,68 +0,0 @@
1
- import { DefaultMonths, getMonthMaxDay } from '../__utils/utils'
2
- import { useMemo } from 'react'
3
- import { DateFormat } from '..'
4
- import { range } from '@companix/utils-js'
5
-
6
- export const defaultMax = { day: 31, month: 12, year: 2050 }
7
- export const defaultMin = { day: 1, month: 1, year: 1900 }
8
-
9
- interface Options {
10
- min?: DateFormat
11
- max?: DateFormat
12
- now: DateFormat
13
- }
14
-
15
- export const useCalendarOptions = ({ min = defaultMin, max = defaultMax, now }: Options) => {
16
- const years = useMemo(() => {
17
- return range(max.year, min.year).map((value) => ({
18
- title: value.toString(),
19
- value
20
- }))
21
- }, [max.year, min.year])
22
-
23
- const months = useMemo(() => {
24
- const options = DefaultMonths.map((name, index) => ({
25
- title: name,
26
- value: index + 1
27
- }))
28
-
29
- if (min.month && now.year === min.year) {
30
- const i = options.findIndex(({ value }) => value === min.month)
31
- options.splice(0, i)
32
- }
33
-
34
- if (max.month && now.year === max.year) {
35
- const i = options.findIndex(({ value }) => value === max.month)
36
- options.splice(i + 1, options.length)
37
- }
38
-
39
- return options
40
- }, [now.year, min.year, min.month, max.year, max.month])
41
-
42
- const days = useMemo(() => {
43
- if (now.month === 0) {
44
- return []
45
- }
46
-
47
- const maxDays = getMonthMaxDay(now.month, now.year)
48
-
49
- const options = Array.from({ length: maxDays }, (_, i) => ({
50
- title: `${i + 1}`,
51
- value: i + 1
52
- }))
53
-
54
- if (min.day && now.month === min.month && now.year === min.year) {
55
- const i = options.findIndex(({ value }) => value === min.day)
56
- options.splice(0, i)
57
- }
58
-
59
- if (max.day && now.month === max.month && now.year === max.year) {
60
- const i = options.findIndex(({ value }) => value === max.day)
61
- options.splice(i + 1, options.length)
62
- }
63
-
64
- return options
65
- }, [now.month, now.year, min, max])
66
-
67
- return { years, months, days }
68
- }