@codeleap/web 2.4.6 → 3.0.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.
Files changed (212) hide show
  1. package/package.json +20 -19
  2. package/src/components/ActionIcon/index.tsx +59 -0
  3. package/src/components/ActionIcon/styles.ts +9 -0
  4. package/src/components/ActivityIndicator/index.tsx +78 -0
  5. package/src/components/ActivityIndicator/styles.ts +11 -0
  6. package/src/components/Button/index.tsx +125 -0
  7. package/src/components/Button/styles.ts +22 -0
  8. package/src/components/Checkbox/index.tsx +138 -0
  9. package/src/components/Checkbox/styles.ts +20 -0
  10. package/src/components/Collapse/index.tsx +87 -0
  11. package/src/components/Collapse/styles.ts +8 -0
  12. package/src/components/Drawer/index.tsx +148 -0
  13. package/src/components/Drawer/styles.ts +8 -0
  14. package/src/components/FileInput.tsx +51 -0
  15. package/src/components/Icon/index.tsx +53 -0
  16. package/src/components/Icon/styles.ts +9 -0
  17. package/src/components/InputBase/index.tsx +104 -0
  18. package/src/components/InputBase/styles.ts +167 -0
  19. package/src/components/InputBase/types.ts +28 -0
  20. package/src/components/InputBase/utils.ts +41 -0
  21. package/src/components/Link/index.tsx +69 -0
  22. package/src/components/Link/styles.ts +11 -0
  23. package/src/components/List/index.tsx +103 -0
  24. package/src/components/List/styles.ts +8 -0
  25. package/src/components/LoadingOverlay/index.tsx +34 -0
  26. package/src/components/LoadingOverlay/styles.ts +12 -0
  27. package/src/components/Modal/index.tsx +189 -0
  28. package/src/components/Modal/styles.ts +26 -0
  29. package/src/components/NumberIncrement/index.tsx +292 -0
  30. package/src/components/NumberIncrement/styles.ts +15 -0
  31. package/src/components/Overlay/index.tsx +42 -0
  32. package/src/components/Overlay/styles.ts +8 -0
  33. package/src/components/RadioInput/index.tsx +155 -0
  34. package/src/components/RadioInput/styles.ts +14 -0
  35. package/src/components/Scroll/index.tsx +29 -0
  36. package/src/components/Scroll/styles.ts +8 -0
  37. package/src/components/Select/index.tsx +438 -0
  38. package/src/components/Select/styles.ts +179 -0
  39. package/src/components/Select/types.ts +100 -0
  40. package/src/components/Slider/index.tsx +303 -0
  41. package/src/components/Slider/styles.ts +11 -0
  42. package/src/components/Switch/index.tsx +128 -0
  43. package/src/components/Switch/styles.ts +20 -0
  44. package/src/components/Text/index.tsx +62 -0
  45. package/src/components/Text/styles.ts +9 -0
  46. package/src/components/TextInput/index.tsx +253 -0
  47. package/src/components/TextInput/mask.tsx +165 -0
  48. package/src/components/TextInput/styles.ts +15 -0
  49. package/src/components/Tooltip/index.tsx +155 -0
  50. package/src/components/Tooltip/styles.ts +9 -0
  51. package/src/components/Touchable/index.tsx +72 -0
  52. package/src/components/Touchable/styles.ts +11 -0
  53. package/src/components/View/index.tsx +94 -0
  54. package/src/components/View/styles.ts +8 -0
  55. package/src/components/components.ts +29 -0
  56. package/src/components/defaultStyles.ts +51 -0
  57. package/src/index.ts +6 -0
  58. package/src/lib/OSAlert.tsx +190 -0
  59. package/src/lib/Toast.ts +23 -0
  60. package/src/lib/hooks.ts +340 -0
  61. package/src/lib/index.ts +2 -0
  62. package/src/lib/logger.ts +13 -0
  63. package/src/lib/utils/cookies.ts +13 -0
  64. package/src/lib/utils/index.ts +4 -0
  65. package/src/lib/utils/pollyfils/scroll.ts +65 -0
  66. package/src/lib/utils/stopPropagation.ts +15 -0
  67. package/src/types/index.ts +1 -0
  68. package/src/types/utility.ts +3 -0
  69. package/dist/components/ActivityIndicator/index.d.ts +0 -12
  70. package/dist/components/ActivityIndicator/index.js +0 -57
  71. package/dist/components/ActivityIndicator/index.js.map +0 -1
  72. package/dist/components/ActivityIndicator/styles.d.ts +0 -53
  73. package/dist/components/ActivityIndicator/styles.js +0 -22
  74. package/dist/components/ActivityIndicator/styles.js.map +0 -1
  75. package/dist/components/Button.d.ts +0 -18
  76. package/dist/components/Button.js +0 -67
  77. package/dist/components/Button.js.map +0 -1
  78. package/dist/components/CenterWrapper.d.ts +0 -7
  79. package/dist/components/CenterWrapper.js +0 -28
  80. package/dist/components/CenterWrapper.js.map +0 -1
  81. package/dist/components/Checkbox/index.d.ts +0 -12
  82. package/dist/components/Checkbox/index.js +0 -58
  83. package/dist/components/Checkbox/index.js.map +0 -1
  84. package/dist/components/Checkbox/styles.d.ts +0 -53
  85. package/dist/components/Checkbox/styles.js +0 -64
  86. package/dist/components/Checkbox/styles.js.map +0 -1
  87. package/dist/components/Collapse.d.ts +0 -20
  88. package/dist/components/Collapse.js +0 -68
  89. package/dist/components/Collapse.js.map +0 -1
  90. package/dist/components/ContentView.d.ts +0 -10
  91. package/dist/components/ContentView.js +0 -52
  92. package/dist/components/ContentView.js.map +0 -1
  93. package/dist/components/Drawer.d.ts +0 -23
  94. package/dist/components/Drawer.js +0 -73
  95. package/dist/components/Drawer.js.map +0 -1
  96. package/dist/components/FileInput.d.ts +0 -8
  97. package/dist/components/FileInput.js +0 -69
  98. package/dist/components/FileInput.js.map +0 -1
  99. package/dist/components/HorizontalScroll.d.ts +0 -3
  100. package/dist/components/HorizontalScroll.js +0 -42
  101. package/dist/components/HorizontalScroll.js.map +0 -1
  102. package/dist/components/Icon.d.ts +0 -8
  103. package/dist/components/Icon.js +0 -55
  104. package/dist/components/Icon.js.map +0 -1
  105. package/dist/components/Link.d.ts +0 -7
  106. package/dist/components/Link.js +0 -63
  107. package/dist/components/Link.js.map +0 -1
  108. package/dist/components/List.d.ts +0 -18
  109. package/dist/components/List.js +0 -52
  110. package/dist/components/List.js.map +0 -1
  111. package/dist/components/Modal/index.d.ts +0 -21
  112. package/dist/components/Modal/index.js +0 -116
  113. package/dist/components/Modal/index.js.map +0 -1
  114. package/dist/components/Modal/styles.d.ts +0 -56
  115. package/dist/components/Modal/styles.js +0 -36
  116. package/dist/components/Modal/styles.js.map +0 -1
  117. package/dist/components/Overlay.d.ts +0 -10
  118. package/dist/components/Overlay.js +0 -41
  119. package/dist/components/Overlay.js.map +0 -1
  120. package/dist/components/RadioInput/index.d.ts +0 -21
  121. package/dist/components/RadioInput/index.js +0 -55
  122. package/dist/components/RadioInput/index.js.map +0 -1
  123. package/dist/components/RadioInput/styles.d.ts +0 -54
  124. package/dist/components/RadioInput/styles.js +0 -44
  125. package/dist/components/RadioInput/styles.js.map +0 -1
  126. package/dist/components/RouterPage/Menu.d.ts +0 -10
  127. package/dist/components/RouterPage/Menu.js +0 -36
  128. package/dist/components/RouterPage/Menu.js.map +0 -1
  129. package/dist/components/RouterPage/MenuItem.d.ts +0 -12
  130. package/dist/components/RouterPage/MenuItem.js +0 -42
  131. package/dist/components/RouterPage/MenuItem.js.map +0 -1
  132. package/dist/components/RouterPage/Router.d.ts +0 -8
  133. package/dist/components/RouterPage/Router.js +0 -27
  134. package/dist/components/RouterPage/Router.js.map +0 -1
  135. package/dist/components/RouterPage/index.d.ts +0 -29
  136. package/dist/components/RouterPage/index.js +0 -85
  137. package/dist/components/RouterPage/index.js.map +0 -1
  138. package/dist/components/RouterPage/styles.d.ts +0 -54
  139. package/dist/components/RouterPage/styles.js +0 -87
  140. package/dist/components/RouterPage/styles.js.map +0 -1
  141. package/dist/components/Scroll.d.ts +0 -5
  142. package/dist/components/Scroll.js +0 -24
  143. package/dist/components/Scroll.js.map +0 -1
  144. package/dist/components/Select/Custom.d.ts +0 -8
  145. package/dist/components/Select/Custom.js +0 -104
  146. package/dist/components/Select/Custom.js.map +0 -1
  147. package/dist/components/Select/Multi.d.ts +0 -3
  148. package/dist/components/Select/Multi.js +0 -105
  149. package/dist/components/Select/Multi.js.map +0 -1
  150. package/dist/components/Select/Native.d.ts +0 -17
  151. package/dist/components/Select/Native.js +0 -44
  152. package/dist/components/Select/Native.js.map +0 -1
  153. package/dist/components/Select/index.d.ts +0 -12
  154. package/dist/components/Select/index.js +0 -40
  155. package/dist/components/Select/index.js.map +0 -1
  156. package/dist/components/Select/styles.d.ts +0 -5
  157. package/dist/components/Select/styles.js +0 -57
  158. package/dist/components/Select/styles.js.map +0 -1
  159. package/dist/components/Select/types.d.ts +0 -49
  160. package/dist/components/Select/types.js +0 -3
  161. package/dist/components/Select/types.js.map +0 -1
  162. package/dist/components/Slider.d.ts +0 -5
  163. package/dist/components/Slider.js +0 -39
  164. package/dist/components/Slider.js.map +0 -1
  165. package/dist/components/Text.d.ts +0 -9
  166. package/dist/components/Text.js +0 -43
  167. package/dist/components/Text.js.map +0 -1
  168. package/dist/components/TextInput.d.ts +0 -150
  169. package/dist/components/TextInput.js +0 -125
  170. package/dist/components/TextInput.js.map +0 -1
  171. package/dist/components/Tooltip.d.ts +0 -12
  172. package/dist/components/Tooltip.js +0 -122
  173. package/dist/components/Tooltip.js.map +0 -1
  174. package/dist/components/Touchable.d.ts +0 -15
  175. package/dist/components/Touchable.js +0 -52
  176. package/dist/components/Touchable.js.map +0 -1
  177. package/dist/components/View.d.ts +0 -10
  178. package/dist/components/View.js +0 -62
  179. package/dist/components/View.js.map +0 -1
  180. package/dist/components/index.d.ts +0 -24
  181. package/dist/components/index.js +0 -37
  182. package/dist/components/index.js.map +0 -1
  183. package/dist/index.d.ts +0 -6
  184. package/dist/index.js +0 -25
  185. package/dist/index.js.map +0 -1
  186. package/dist/lib/OSAlert.d.ts +0 -21
  187. package/dist/lib/OSAlert.js +0 -140
  188. package/dist/lib/OSAlert.js.map +0 -1
  189. package/dist/lib/Toast.d.ts +0 -13
  190. package/dist/lib/Toast.js +0 -32
  191. package/dist/lib/Toast.js.map +0 -1
  192. package/dist/lib/hooks.d.ts +0 -28
  193. package/dist/lib/hooks.js +0 -183
  194. package/dist/lib/hooks.js.map +0 -1
  195. package/dist/lib/logger.d.ts +0 -2
  196. package/dist/lib/logger.js +0 -16
  197. package/dist/lib/logger.js.map +0 -1
  198. package/dist/lib/utils/cookies.d.ts +0 -6
  199. package/dist/lib/utils/cookies.js +0 -15
  200. package/dist/lib/utils/cookies.js.map +0 -1
  201. package/dist/lib/utils/index.d.ts +0 -4
  202. package/dist/lib/utils/index.js +0 -23
  203. package/dist/lib/utils/index.js.map +0 -1
  204. package/dist/lib/utils/pollyfils/scroll.d.ts +0 -1
  205. package/dist/lib/utils/pollyfils/scroll.js +0 -66
  206. package/dist/lib/utils/pollyfils/scroll.js.map +0 -1
  207. package/dist/lib/utils/stopPropagation.d.ts +0 -1
  208. package/dist/lib/utils/stopPropagation.js +0 -20
  209. package/dist/lib/utils/stopPropagation.js.map +0 -1
  210. package/dist/types/utility.d.ts +0 -2
  211. package/dist/types/utility.js +0 -3
  212. package/dist/types/utility.js.map +0 -1
@@ -0,0 +1,179 @@
1
+ import { capitalize, createDefaultVariantFactory, FormTypes, getNestedStylesByKey, includePresets, useDefaultComponentStyle } from '@codeleap/common'
2
+ import { CSSInterpolation } from '@emotion/css'
3
+ import { CSSObjectWithLabel, GroupBase, StylesConfig } from 'react-select'
4
+ import { ButtonParts as _ButtonParts } from '../Button'
5
+ import { InputBaseParts } from '../InputBase'
6
+ import { LoadingOverlayComposition } from '../LoadingOverlay'
7
+ import { SelectProps } from './types'
8
+
9
+ type ButtonParts = Exclude<_ButtonParts, `loading${Capitalize<LoadingOverlayComposition>}` | 'badgeText' | 'badgeWrapper'>
10
+
11
+ export type ItemParts = `item${Capitalize<ButtonParts>}`
12
+
13
+ export type SelectParts =
14
+ InputBaseParts
15
+ | ItemParts
16
+ | 'listPortal'
17
+ | 'listHeader'
18
+ | 'listWrapper'
19
+ | 'list'
20
+ | 'inputContainer'
21
+ | 'input'
22
+ | 'placeholder'
23
+ | 'value'
24
+ | 'valueMultiple'
25
+ | 'valueWrapper'
26
+ | 'clearIcon'
27
+ | 'dropdownIcon'
28
+
29
+ type ItemState = 'focused' | 'selected' | 'selectedFocused'
30
+
31
+ export type SelectStatefulParts =
32
+ `${ItemParts}:${ItemState}`
33
+ | 'listPlaceholder'
34
+ | 'listPlaceholderIcon'
35
+ | 'listPlaceholderText'
36
+ | 'listPlaceholderNoItems'
37
+ | 'listPlaceholderNoItemsIcon'
38
+ | 'listPlaceholderNoItemsText'
39
+ | 'itemsWrapper'
40
+ | 'loadingIndicator'
41
+ | 'innerWrapper:searchable'
42
+
43
+ export type SelectState = 'error' | 'focus' | 'disabled'
44
+
45
+ export type SelectComposition = SelectParts | `${SelectParts}:${SelectState}` | SelectStatefulParts
46
+
47
+ const createSelectStyle = createDefaultVariantFactory<SelectComposition>()
48
+
49
+ export const SelectPresets = includePresets((styles) => createSelectStyle(() => ({ wrapper: styles })))
50
+
51
+ export type ComponentState = {
52
+ error?: boolean
53
+ focused?: boolean
54
+ disabled?: boolean
55
+ }
56
+
57
+ export type OptionState = {
58
+ isSelected: boolean
59
+ isFocused: boolean
60
+ baseStyles: SelectProps['itemProps']['styles']
61
+ }
62
+
63
+ export function useSelectStyles<T, Multi extends boolean>(props: SelectProps<T, Multi>, state: ComponentState) {
64
+ const {
65
+ variants,
66
+ styles,
67
+ } = props
68
+
69
+ const {
70
+ error,
71
+ focused,
72
+ disabled,
73
+ } = state
74
+
75
+ const variantStyles = useDefaultComponentStyle<'u:Select', typeof SelectPresets>(
76
+ 'u:Select',
77
+ {
78
+ variants,
79
+ styles,
80
+ },
81
+ )
82
+
83
+ const stylesKey = (key: SelectParts | SelectStatefulParts, _styles: CSSObjectWithLabel = {}) => ({
84
+ ..._styles,
85
+ ...variantStyles[key],
86
+ ...(focused ? variantStyles[key + ':focus'] : {}),
87
+ ...(disabled ? variantStyles[key + ':disabled'] : {}),
88
+ ...(error ? variantStyles[key + ':error'] : {}),
89
+ })
90
+
91
+ const optionNestedStyles = getNestedStylesByKey('item', variantStyles)
92
+
93
+ const optionStyleKey = (
94
+ key: ButtonParts | `${ButtonParts}:${ItemState}`,
95
+ state: OptionState
96
+ ) => {
97
+ return {
98
+ ...stylesKey(`item${capitalize(key)}` as any),
99
+ ...(state?.isSelected ? optionNestedStyles[`${key}:selected`] : {}),
100
+ ...(state?.isFocused ? optionNestedStyles[`${key}:focused`] : {}),
101
+ ...(state?.isFocused && state?.isSelected ? optionNestedStyles[`${key}:selectedFocused`] : {}),
102
+ ...(state.baseStyles?.[key] as React.CSSProperties),
103
+ }
104
+ }
105
+
106
+ const optionsStyles = (state: OptionState): Record<ButtonParts, CSSInterpolation> => ({
107
+ wrapper: optionStyleKey('wrapper', state),
108
+ rightIcon: optionStyleKey('rightIcon', state),
109
+ text: optionStyleKey('text', state),
110
+ leftIcon: optionStyleKey('leftIcon', state),
111
+ icon: optionStyleKey('icon', state),
112
+ inner: optionStyleKey('inner', state),
113
+ })
114
+
115
+ const placeholderStyles = {
116
+ empty: {
117
+ wrapper: stylesKey('listPlaceholder'),
118
+ icon: stylesKey('listPlaceholderIcon'),
119
+ text: stylesKey('listPlaceholderText'),
120
+ },
121
+ noItems: {
122
+ wrapper: stylesKey('listPlaceholderNoItems'),
123
+ icon: stylesKey('listPlaceholderNoItemsIcon'),
124
+ text: stylesKey('listPlaceholderNoItemsText'),
125
+ },
126
+ }
127
+
128
+ const loadingStyles = {
129
+ wrapper: stylesKey('loadingIndicator'),
130
+ }
131
+
132
+ const inputMultiValueStyles = {
133
+ text: stylesKey('valueMultiple'),
134
+ }
135
+
136
+ const menuWrapperStyles = {
137
+ wrapper: stylesKey('itemsWrapper'),
138
+ }
139
+
140
+ const reactSelectStyles: StylesConfig<FormTypes.Option<T>, Multi, GroupBase<FormTypes.Option<T>>> = {
141
+ container: (baseStyles) => stylesKey('inputContainer', baseStyles),
142
+ control: () => stylesKey('inputContainer'),
143
+ menuPortal: (baseStyles) => stylesKey('listPortal', baseStyles),
144
+ menu: (baseStyles) => stylesKey('listWrapper', baseStyles),
145
+ menuList: (baseStyles) => stylesKey('list', baseStyles),
146
+ group: () => ({}),
147
+ indicatorSeparator: () => ({}),
148
+ groupHeading: (baseStyles) => stylesKey('listHeader', baseStyles),
149
+ clearIndicator: () => ({
150
+ ...stylesKey('iconIcon'),
151
+ ...stylesKey('clearIcon'),
152
+ }),
153
+ dropdownIndicator: () => ({
154
+ ...stylesKey('iconIcon'),
155
+ ...stylesKey('dropdownIcon'),
156
+ }),
157
+ indicatorsContainer: (baseStyles) => baseStyles,
158
+ input: (baseStyles) => stylesKey('input', baseStyles),
159
+ loadingIndicator: () => ({}),
160
+ loadingMessage: () => ({}),
161
+ multiValue: () => ({}),
162
+ multiValueLabel: () => ({}),
163
+ noOptionsMessage: () => ({}),
164
+ option: () => ({}),
165
+ placeholder: () => stylesKey('placeholder'),
166
+ singleValue: (baseStyles) => stylesKey('value', baseStyles),
167
+ valueContainer: (baseStyles) => stylesKey('valueWrapper', baseStyles),
168
+ }
169
+
170
+ return {
171
+ variantStyles,
172
+ reactSelectStyles,
173
+ optionsStyles,
174
+ placeholderStyles,
175
+ loadingStyles,
176
+ inputMultiValueStyles,
177
+ menuWrapperStyles,
178
+ }
179
+ }
@@ -0,0 +1,100 @@
1
+ import { ComponentVariants, FormTypes, StylesOf, yup } from "@codeleap/common"
2
+ import { CSSInterpolation } from '@emotion/css'
3
+ import { CSSObject } from "@emotion/react"
4
+ import { GroupBase, NoticeProps, OptionProps, Props } from "react-select"
5
+ import { AsyncProps } from "react-select/async"
6
+ import { ButtonProps } from '../Button'
7
+ import { InputBaseProps } from "../InputBase"
8
+ import { SelectPresets,SelectComposition, OptionState } from "./styles"
9
+
10
+ type SelectValue<T,Multi extends boolean> = Multi extends true ? T[] : T
11
+
12
+ type OmitWithValues<T> = Omit<T, 'options' | 'value' | 'isMulti' | 'loadOptions'|'styles'>
13
+
14
+ type DynamicSelectProps<T, Multi extends boolean> =
15
+ ({
16
+ loadOptions?: (search: string) => Promise<FormTypes.Options<T>>
17
+ defaultValues?: FormTypes.Options<T>
18
+ } & OmitWithValues<
19
+ AsyncProps<FormTypes.Option<T>, Multi, GroupBase<FormTypes.Option<T>>>
20
+ >) |
21
+ ({
22
+ loadOptions?: never
23
+ } & OmitWithValues<
24
+ Props<FormTypes.Option<T>, Multi, GroupBase<FormTypes.Option<T>>>
25
+ >)
26
+
27
+ export type ReactSelectProps<T, Multi extends boolean = false> = Omit<InputBaseProps , 'styles' | 'variants'> &{
28
+ options: FormTypes.Options<T>
29
+ value: SelectValue<T,Multi>
30
+ onValueChange?: (value: SelectValue<T,Multi>) => void
31
+ multiple?: Multi
32
+ validate?: FormTypes.ValidatorWithoutForm<SelectValue<T,Multi>> | yup.SchemaOf<SelectValue<T,Multi>>
33
+ styles?: StylesOf<SelectComposition>
34
+ } & DynamicSelectProps<T, Multi>
35
+
36
+ export type ComponentPartProps = {
37
+ focused: boolean
38
+ error: boolean
39
+ disabled: boolean
40
+ variantStyles: Record<SelectComposition, React.CSSProperties>
41
+ }
42
+
43
+ export type TCustomOption = OptionProps & ComponentPartProps & {
44
+ optionsStyles: (state: OptionState) => OptionState['baseStyles']
45
+ selectedIcon?: string
46
+ itemProps?: ButtonProps
47
+ styles?: OptionState['baseStyles']
48
+ }
49
+
50
+ type SelectPlaceholderElement = string | ((props: PlaceholderProps) => JSX.Element) | null
51
+
52
+ export type PlaceholderProps = NoticeProps & ComponentPartProps & {
53
+ text: SelectPlaceholderElement
54
+ defaultStyles: {
55
+ wrapper: CSSInterpolation
56
+ text: CSSInterpolation
57
+ icon: CSSInterpolation
58
+ }
59
+ icon: SelectPlaceholderElement
60
+ }
61
+
62
+ export type LoadingIndicatorProps = NoticeProps & {
63
+ defaultStyles: { wrapper: CSSInterpolation }
64
+ size?: number
65
+ }
66
+
67
+ export type SelectProps<T = any, Multi extends boolean = false> = React.PropsWithChildren<
68
+ {
69
+ debugName?: string
70
+ clearable?: boolean
71
+ closeOnSelect?: boolean
72
+ css?: CSSObject
73
+ focused?: boolean
74
+ _error?: string
75
+ renderItem?: (props: TCustomOption) => JSX.Element
76
+ FooterComponent?: () => JSX.Element
77
+ PlaceholderComponent?: (props: PlaceholderProps) => JSX.Element
78
+ PlaceholderNoItemsComponent?: (props: PlaceholderProps) => JSX.Element
79
+ LoadingIndicatorComponent?: (props: LoadingIndicatorProps) => JSX.Element
80
+ noItemsText?: SelectPlaceholderElement
81
+ noItemsIcon?: SelectPlaceholderElement
82
+ placeholderText?: SelectPlaceholderElement
83
+ placeholderIcon?: SelectPlaceholderElement
84
+ showDropdownIcon?: boolean
85
+ formatPlaceholderNoItems?: (props: PlaceholderProps & { text: string }) => string
86
+ selectedIcon?: string
87
+ onLoadOptionsError?: (error: any) => void
88
+ loadOptionsOnMount?: boolean
89
+ searchable?: boolean
90
+ separatorMultiValue?: string
91
+ filterItems?: Props['filterOption']
92
+ itemProps?: ButtonProps
93
+ loadingIndicatorSize?: number
94
+ limit?: number
95
+ } & Omit<
96
+ ReactSelectProps<T, Multi>,
97
+ 'isSearchable' | 'isClearable' | 'isDisabled' | 'loadingMessage' | 'filterOption' |
98
+ 'isLoading' | 'menuPortalTarget' | 'closeMenuOnSelect' | 'isMulti'>
99
+ & ComponentVariants<typeof SelectPresets>
100
+ >
@@ -0,0 +1,303 @@
1
+ import * as React from 'react'
2
+ import { ComponentVariants, onMount, PropsOf, TypeGuards, useDefaultComponentStyle, useRef } from '@codeleap/common'
3
+ import { SliderComposition } from './styles'
4
+ import { StylesOf } from '../../types'
5
+ import { View } from '../View'
6
+ import { concatStyles, InputBaseProps, InputBase, selectInputBaseProps } from '../InputBase'
7
+ import { SliderPresets } from './styles'
8
+ import { Text } from '../Text'
9
+
10
+ import {
11
+ Root as SliderContainer,
12
+ Track as SliderTrack,
13
+ Thumb as SliderThumb,
14
+ Range as SliderRange,
15
+ SliderProps as PrimitiveSliderProps
16
+ } from '@radix-ui/react-slider'
17
+
18
+ export * from './styles'
19
+
20
+ export type SliderProps = Partial<Omit<PrimitiveSliderProps, 'value' | 'onValueChange'>> & Pick<InputBaseProps, 'disabled' | 'debugName' | 'description' | 'label'> & {
21
+ debounce?: number | null
22
+ indicatorLabel?: {
23
+ order?: number[]
24
+ separator?: string
25
+ transformer?: (value: number[], defaultValue: PrimitiveSliderProps['defaultValue']) => string
26
+ }
27
+ value: number[]
28
+ onValueChange: (val: number[]) => void
29
+ variants?: ComponentVariants<typeof SliderPresets>['variants']
30
+ styles?: StylesOf<SliderComposition>
31
+ style?: PropsOf<typeof View>['style']
32
+ trackMarks?: Record<number, string>
33
+ trackMarkComponent?: React.ComponentType<TrackMarkProps>
34
+ }
35
+
36
+ export type TrackMarkProps = {
37
+ index: number
38
+ content?: string | React.ReactNode
39
+ style?: any
40
+ }
41
+
42
+ const DefaultSliderTrackMark = (props: TrackMarkProps) => {
43
+ const { index, content, style } = props
44
+
45
+ if(!TypeGuards.isString(props.content)){
46
+ return <>
47
+ {props.content}
48
+ </>
49
+ }
50
+
51
+ return <Text
52
+ text={props.content}
53
+ css={style}
54
+ />
55
+ }
56
+
57
+ export const Slider = (props: SliderProps) => {
58
+ const {
59
+ inputBaseProps,
60
+ others
61
+ } = selectInputBaseProps(props)
62
+
63
+ const {
64
+ debounce = null,
65
+ onValueChange,
66
+ value,
67
+ label,
68
+ debugName,
69
+ styles = {},
70
+ style,
71
+ disabled,
72
+ variants,
73
+ trackMarks,
74
+ trackMarkComponent = DefaultSliderTrackMark,
75
+ defaultValue: _defaultValue = [],
76
+ max = 100,
77
+ min = 0,
78
+ indicatorLabel = null,
79
+ description = null,
80
+ minStepsBetweenThumbs = 8,
81
+ step = 1,
82
+ ...sliderProps
83
+ } = others
84
+
85
+ const defaultValueRef = useRef<PrimitiveSliderProps['defaultValue']>(_defaultValue)
86
+ const defaultValue = defaultValueRef.current
87
+
88
+ onMount(() => {
89
+ if (_defaultValue?.length <= 0) {
90
+ defaultValueRef.current = value
91
+ }
92
+ })
93
+
94
+ const SliderTrackMark = trackMarkComponent
95
+
96
+ const currentThumbRef = useRef(null)
97
+
98
+ const handleChange: SliderProps['onValueChange'] = (newValue) => {
99
+ if (newValue.length <= 1) {
100
+ onValueChange(newValue)
101
+ return
102
+ }
103
+
104
+ const copyValue = [...value]
105
+
106
+ const i = currentThumbRef.current
107
+ const _newValue = newValue[currentThumbRef.current]
108
+
109
+ const hasLeftThumb = i !== 0
110
+ const hasRightThumb = i + 1 < newValue.length
111
+
112
+ const previousThumbValue = hasLeftThumb ? (copyValue[i -1] + minStepsBetweenThumbs) : null
113
+ const nextThumbValue = hasRightThumb ? (copyValue[i + 1] - minStepsBetweenThumbs) : null
114
+
115
+ if (previousThumbValue && _newValue <= previousThumbValue) {
116
+ copyValue[i] = previousThumbValue
117
+ } else if (nextThumbValue && _newValue >= nextThumbValue) {
118
+ copyValue[i] = nextThumbValue
119
+ } else {
120
+ copyValue[i] = _newValue
121
+ }
122
+
123
+ onValueChange(copyValue)
124
+ }
125
+
126
+ const variantStyles = useDefaultComponentStyle<'u:Slider', typeof SliderPresets>('u:Slider', {
127
+ variants,
128
+ styles,
129
+ })
130
+
131
+ const thumbStyle = React.useMemo(() => {
132
+ return concatStyles([
133
+ variantStyles.thumb,
134
+ disabled && variantStyles['thumb:disabled'],
135
+ ])
136
+ }, [])
137
+
138
+ const trackStyle = React.useMemo(() => {
139
+ return concatStyles([
140
+ variantStyles.track,
141
+ disabled && variantStyles['track:disabled'],
142
+ variantStyles.unselectedTrack,
143
+ disabled && variantStyles['unselectedTrack:disabled'],
144
+ ])
145
+ }, [disabled])
146
+
147
+ const selectedTrackStyle = React.useMemo(() => {
148
+ return concatStyles([
149
+ variantStyles.selectedTrack,
150
+ disabled && variantStyles['selectedTrack:disabled'],
151
+ ])
152
+ }, [disabled])
153
+
154
+ const containerStyle = React.useMemo(() => {
155
+ return concatStyles([
156
+ variantStyles.sliderContainer,
157
+ disabled && variantStyles['sliderContainer:disabled'],
158
+ ])
159
+ }, [disabled])
160
+
161
+ const trackMarksHaveContent = !(TypeGuards.isArray(trackMarks) || TypeGuards.isNil(trackMarks))
162
+
163
+ const trackMarksProp = React.useMemo(() => {
164
+ if(!trackMarksHaveContent){
165
+ return trackMarks
166
+ }
167
+ return Object.keys(trackMarks).map((key) => Number(key))
168
+ },[trackMarksHaveContent])
169
+
170
+ const trackMarkStyle = React.useMemo(() => {
171
+ return concatStyles([
172
+ variantStyles.trackMark,
173
+ disabled && variantStyles['trackMark:disabled'],
174
+ ])
175
+ }, [disabled])
176
+
177
+ const trackMarkWrapperStyle = React.useMemo(() => {
178
+ return [
179
+ variantStyles.trackMarkWrapper,
180
+ disabled && variantStyles['trackMarkWrapper:disabled'],
181
+ ]
182
+ }, [disabled])
183
+
184
+ const sliderLabel = React.useMemo(() => {
185
+ if (TypeGuards.isNil(indicatorLabel) || !indicatorLabel?.order && !indicatorLabel?.transformer) {
186
+ return description
187
+ }
188
+
189
+ if (TypeGuards.isFunction(indicatorLabel?.transformer)) {
190
+ const str = indicatorLabel?.transformer(value, defaultValue)
191
+ return str
192
+ } else if (TypeGuards.isArray(indicatorLabel?.order)) {
193
+ let str = ''
194
+
195
+ indicatorLabel?.order?.forEach((idx, i) => {
196
+ const valueLabel = value[idx]
197
+
198
+ if (TypeGuards.isNil(valueLabel)) return
199
+
200
+ str = i === 0 ? String(valueLabel) : `${str}${indicatorLabel?.separator ?? ' - '}${valueLabel}`
201
+ })
202
+
203
+ return str
204
+ } else {
205
+ return description
206
+ }
207
+ }, [value])
208
+
209
+ return (
210
+ <InputBase
211
+ {...inputBaseProps}
212
+ disabled={disabled}
213
+ styles={{
214
+ ...variantStyles,
215
+ innerWrapper: [
216
+ variantStyles.innerWrapper,
217
+ ],
218
+ }}
219
+ labelAsRow
220
+ description={sliderLabel}
221
+ >
222
+ <SliderContainer
223
+ {...sliderProps}
224
+ step={step}
225
+ minStepsBetweenThumbs={minStepsBetweenThumbs}
226
+ style={containerStyle}
227
+ defaultValue={defaultValue}
228
+ value={value}
229
+ onValueCommit={() => null}
230
+ onValueChange={handleChange}
231
+ min={min}
232
+ max={max}
233
+ disabled={disabled}
234
+ >
235
+ <SliderTrack style={trackStyle}>
236
+ <SliderRange style={selectedTrackStyle} />
237
+ </SliderTrack>
238
+
239
+ {defaultValue.map((_thumbValue, i) => (
240
+ <SliderThumb
241
+ key={i}
242
+ style={thumbStyle}
243
+ onClick={() => currentThumbRef.current = i}
244
+ onMouseEnter={() => currentThumbRef.current = i}
245
+ />
246
+ ))}
247
+ </SliderContainer>
248
+
249
+ {trackMarksProp ?
250
+ <View css={trackMarkWrapperStyle}>
251
+ {
252
+ trackMarksProp.map((_, idx) => {
253
+ let idxStyle = {}
254
+
255
+ const relativeValue = Number(trackMarksProp[idx])
256
+
257
+ if(idx === 0){
258
+ idxStyle = variantStyles.firstTrackMark
259
+ } else if(idx === trackMarksProp.length - 1){
260
+ idxStyle = variantStyles.lastTrackMark
261
+ } else {
262
+ const markerPosition = (relativeValue / max) * 100
263
+
264
+ idxStyle = {
265
+ position: 'absolute',
266
+ left: `${markerPosition}%`,
267
+ top: '50%',
268
+ transform: `translate(-50%, -50%)`,
269
+ }
270
+ }
271
+
272
+ const style = [
273
+ trackMarkStyle,
274
+ idxStyle,
275
+ ]
276
+
277
+ if (!trackMarksHaveContent){
278
+ return <SliderTrackMark
279
+ index={idx}
280
+ style={style}
281
+ key={idx}
282
+ />
283
+ }
284
+
285
+ const content = trackMarks[relativeValue]
286
+
287
+ return (
288
+ <SliderTrackMark
289
+ index={idx}
290
+ content={content}
291
+ style={style}
292
+ key={idx}
293
+ />
294
+ )
295
+ })
296
+ }
297
+ </View>
298
+ : null}
299
+ </InputBase>
300
+ )
301
+ }
302
+
303
+ export * from './styles'
@@ -0,0 +1,11 @@
1
+ import { createDefaultVariantFactory, includePresets } from '@codeleap/common'
2
+ import { IconLessInputBaseParts, InputBaseParts, InputBaseStates } from '../InputBase'
3
+
4
+ type SliderParts = InputBaseParts | 'thumb' | 'track' | 'selectedTrack' | 'unselectedTrack' | 'trackMark' | 'firstTrackMark' | 'lastTrackMark' | 'trackMarkWrapper' | 'sliderContainer' | IconLessInputBaseParts
5
+ type SliderStates = Exclude<InputBaseStates, 'focus'>
6
+
7
+ export type SliderComposition = `${SliderParts}:${SliderStates}` | SliderParts
8
+
9
+ const createSliderStyle = createDefaultVariantFactory<SliderComposition>()
10
+
11
+ export const SliderPresets = includePresets((styles) => createSliderStyle(() => ({ wrapper: styles })))