@fibery/ui-kit 1.40.0 → 1.40.1

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.
@@ -0,0 +1,288 @@
1
+ /* eslint-disable no-use-before-define */
2
+ import {useComposedRefs} from "@fibery/react/src/use-composed-refs";
3
+ import {ForwardedRef, forwardRef, RefObject, useCallback, useMemo, useRef, useState} from "react";
4
+ import BaseSelect, {
5
+ ActionMeta,
6
+ components as reactSelectComponents,
7
+ createFilter,
8
+ MenuListProps,
9
+ MenuProps,
10
+ ControlProps,
11
+ mergeStyles,
12
+ MultiValue,
13
+ MultiValueProps,
14
+ OnChangeValue,
15
+ OptionProps,
16
+ OptionsOrGroups,
17
+ Props as BaseSelectProps,
18
+ PropsValue,
19
+ SelectInstance,
20
+ SingleValue,
21
+ SingleValueProps,
22
+ StylesConfig,
23
+ } from "react-select";
24
+ import BaseCreatableSelect, {CreatableProps} from "react-select/creatable";
25
+ import {ClearIndicator, NoClearIndicator} from "./components/clear-indicator";
26
+ import {DropdownIndicator} from "./components/drop-down-indicator";
27
+ import {GroupHeading} from "./components/group-heading";
28
+ import {Menu} from "./components/menu";
29
+ import {MenuListVirtualized} from "./components/menu-list-virtualized";
30
+ import {NoOptionsMessage} from "./components/no-option-message";
31
+ import {Option, OptionSlow} from "./components/option";
32
+ import {useSelectControlSettings} from "./select-control-settings-context";
33
+ import {makeComponentsStyles, makeNonVirtualizedStyles, makeVirtualizedStyles, makeZIndexStyles} from "./styles";
34
+ import {countOptions, GroupBase, ReactSelectRefContext} from "./util";
35
+ import _ from "lodash";
36
+
37
+ export type {
38
+ ActionMeta,
39
+ GroupBase,
40
+ MenuListProps,
41
+ MenuProps,
42
+ ControlProps,
43
+ MultiValue,
44
+ MultiValueProps,
45
+ OnChangeValue,
46
+ OptionProps,
47
+ OptionsOrGroups,
48
+ PropsValue,
49
+ SingleValue,
50
+ SingleValueProps,
51
+ StylesConfig,
52
+ SelectInstance,
53
+ BaseSelectProps,
54
+ };
55
+
56
+ export {reactSelectComponents as components, MenuListVirtualized, createFilter};
57
+
58
+ function GroupVirtualized() {
59
+ return null;
60
+ }
61
+
62
+ export function combineStyles<T, U extends boolean, V extends GroupBase<T>>(styles: StylesConfig<T, U, V>[]) {
63
+ return styles.reduce(mergeStyles, {});
64
+ }
65
+
66
+ export type SelectProps<T = unknown, U extends boolean = boolean, V extends GroupBase<T> = GroupBase<T>> = Omit<
67
+ Omit<Omit<BaseSelectProps<T, U, V>, "isMulti">, "backspaceRemovesValue">,
68
+ "isDisabled"
69
+ > & {
70
+ virtualized?: boolean;
71
+ title?: string;
72
+ isCollectionMode?: U;
73
+ disabled?: boolean;
74
+ forbidValuesClearInCollnMode?: boolean;
75
+ };
76
+
77
+ const FAST_OPTION_USAGE_THRESHOLD = 200;
78
+
79
+ function useSelectComponents<T, U extends boolean, V extends GroupBase<T>>({
80
+ components,
81
+ optionsCount,
82
+ virtualized,
83
+ isCollectionMode,
84
+ forbidValuesClearInCollnMode,
85
+ }: {
86
+ components: SelectProps<T, U, V>["components"];
87
+ optionsCount: number;
88
+ virtualized: boolean;
89
+ isCollectionMode?: U;
90
+ forbidValuesClearInCollnMode?: boolean;
91
+ }) {
92
+ const shouldUseFastOption = optionsCount > FAST_OPTION_USAGE_THRESHOLD;
93
+ const shouldHideMultiValueRemove = isCollectionMode && forbidValuesClearInCollnMode;
94
+
95
+ return {
96
+ Menu,
97
+ DropdownIndicator,
98
+ ClearIndicator,
99
+ MultiValueRemove: shouldHideMultiValueRemove ? NoClearIndicator : ClearIndicator,
100
+ NoOptionsMessage,
101
+ GroupHeading,
102
+ Option: shouldUseFastOption ? Option : OptionSlow,
103
+ ...components,
104
+ Group: virtualized ? GroupVirtualized : components?.Group ? components.Group : reactSelectComponents.Group,
105
+ MenuList: virtualized
106
+ ? MenuListVirtualized
107
+ : components?.MenuList
108
+ ? components.MenuList
109
+ : reactSelectComponents.MenuList,
110
+ };
111
+ }
112
+
113
+ function useSelectStyles<T, U extends boolean, V extends GroupBase<T>>({
114
+ virtualized,
115
+ zIndex,
116
+ styles,
117
+ }: {
118
+ virtualized: boolean;
119
+ zIndex?: number;
120
+ styles?: SelectProps<T, U, V>["styles"];
121
+ }) {
122
+ return useMemo(() => {
123
+ return combineStyles<T, U, V>(
124
+ _.compact([
125
+ makeComponentsStyles<T, U, V>(),
126
+ virtualized ? makeVirtualizedStyles<T, U, V>() : makeNonVirtualizedStyles<T, U, V>(),
127
+ zIndex ? makeZIndexStyles<T, U, V>(zIndex) : null,
128
+ styles,
129
+ ])
130
+ );
131
+ }, [styles, virtualized, zIndex]);
132
+ }
133
+
134
+ export const SelectComponent = forwardRef(function Select<
135
+ T = unknown,
136
+ U extends boolean = boolean,
137
+ V extends GroupBase<T> = GroupBase<T>
138
+ >(
139
+ {
140
+ components,
141
+ isCollectionMode,
142
+ menuPortalTarget,
143
+ onKeyDown,
144
+ styles,
145
+ virtualized = true,
146
+ onMenuOpen,
147
+ onMenuClose,
148
+ onChange,
149
+ forbidValuesClearInCollnMode,
150
+ ...rest
151
+ }: SelectProps<T, U, V>,
152
+ forwardedRef: ForwardedRef<SelectInstance<T, U, V>>
153
+ ) {
154
+ const {getPopupContainerElement, zIndex} = useSelectControlSettings();
155
+ const [menuOpenState, setMenuOpenState] = useState(rest.defaultMenuIsOpen);
156
+ const optionsCount = useMemo(() => countOptions(rest.options), [rest.options]);
157
+
158
+ const onMenuOpenWrapped = useCallback(() => {
159
+ setMenuOpenState(true);
160
+ onMenuOpen && onMenuOpen();
161
+ }, [onMenuOpen]);
162
+
163
+ const onMenuCloseWrapped = useCallback(() => {
164
+ setMenuOpenState(false);
165
+ onMenuClose && onMenuClose();
166
+ }, [onMenuClose]);
167
+
168
+ const handleKeyDown: React.KeyboardEventHandler<HTMLInputElement> = useCallback(
169
+ (e) => {
170
+ switch (e.key) {
171
+ case "Escape":
172
+ // we can't rely on "menuIsOpen" prop here, as user may not pass this prop and rely on internal component behavior instead.
173
+ if (menuOpenState) {
174
+ e.stopPropagation();
175
+ }
176
+ break;
177
+ case "Home":
178
+ e.preventDefault();
179
+ if (e.shiftKey) {
180
+ (e.target as HTMLInputElement).selectionStart = 0;
181
+ } else {
182
+ (e.target as HTMLInputElement).setSelectionRange(0, 0);
183
+ }
184
+ break;
185
+ case "End": {
186
+ e.preventDefault();
187
+ const len = (e.target as HTMLInputElement).value.length;
188
+ if (e.shiftKey) {
189
+ (e.target as HTMLInputElement).selectionEnd = len;
190
+ } else {
191
+ (e.target as HTMLInputElement).setSelectionRange(len, len);
192
+ }
193
+ break;
194
+ }
195
+ default:
196
+ }
197
+ onKeyDown && onKeyDown(e);
198
+ },
199
+ [onKeyDown, menuOpenState]
200
+ );
201
+
202
+ const selectRef = useRef<SelectInstance<T, U, V> | null>(null);
203
+ const composedRef = useComposedRefs(forwardedRef, selectRef);
204
+
205
+ const selectComponents = useSelectComponents({
206
+ components,
207
+ optionsCount,
208
+ virtualized,
209
+ isCollectionMode,
210
+ forbidValuesClearInCollnMode,
211
+ });
212
+
213
+ const combinedStyles = useSelectStyles({virtualized, zIndex, styles});
214
+
215
+ return (
216
+ <ReactSelectRefContext.Provider value={selectRef as RefObject<SelectInstance>}>
217
+ <BaseSelect<T, U, V>
218
+ ref={composedRef}
219
+ // There are places, where it is useful to override menuPortalTarget globally for all selects.
220
+ // When you are already in some popup. e.g. user input in buttons, relation filter popover.
221
+ menuPortalTarget={getPopupContainerElement ? getPopupContainerElement() : menuPortalTarget}
222
+ menuPlacement={"auto"}
223
+ styles={combinedStyles}
224
+ isMulti={isCollectionMode}
225
+ backspaceRemovesValue={isCollectionMode && !forbidValuesClearInCollnMode}
226
+ tabSelectsValue={false}
227
+ components={selectComponents}
228
+ {...rest}
229
+ onKeyDown={handleKeyDown}
230
+ onMenuOpen={onMenuOpenWrapped}
231
+ onMenuClose={onMenuCloseWrapped}
232
+ onChange={rest.disabled ? undefined : onChange}
233
+ isDisabled={rest.disabled}
234
+ />
235
+ </ReactSelectRefContext.Provider>
236
+ );
237
+ }) as <Option = unknown, IsMulti extends boolean = boolean, Group extends GroupBase<Option> = GroupBase<Option>>(
238
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
239
+ props: SelectProps<Option, IsMulti, Group> & {ref?: any}
240
+ ) => JSX.Element;
241
+
242
+ function CreatableSelectInner<T = unknown, U extends boolean = boolean, V extends GroupBase<T> = GroupBase<T>>(
243
+ {
244
+ components,
245
+ isCollectionMode,
246
+ menuPortalTarget,
247
+ styles = {},
248
+ virtualized = true,
249
+ forbidValuesClearInCollnMode,
250
+ ...rest
251
+ }: CreatableProps<T, U, V> & SelectProps<T, U, V>,
252
+ ref: ForwardedRef<SelectInstance<T, U, V>>
253
+ ) {
254
+ const optionsCount = useMemo(() => countOptions(rest.options), [rest.options]);
255
+
256
+ const selectComponents = useSelectComponents({
257
+ components,
258
+ optionsCount,
259
+ virtualized,
260
+ isCollectionMode,
261
+ forbidValuesClearInCollnMode,
262
+ });
263
+
264
+ const combinedStyles = useSelectStyles({virtualized, styles});
265
+
266
+ return (
267
+ <BaseCreatableSelect
268
+ ref={ref}
269
+ menuPortalTarget={menuPortalTarget}
270
+ menuPlacement={"auto"}
271
+ styles={combinedStyles}
272
+ isMulti={isCollectionMode}
273
+ backspaceRemovesValue={isCollectionMode}
274
+ tabSelectsValue={false}
275
+ components={selectComponents}
276
+ {...rest}
277
+ isDisabled={rest.disabled}
278
+ />
279
+ );
280
+ }
281
+
282
+ export const CreatableSelect = forwardRef(CreatableSelectInner) as <
283
+ T = unknown,
284
+ U extends boolean = boolean,
285
+ V extends GroupBase<T> = GroupBase<T>
286
+ >(
287
+ props: CreatableProps<T, U, V> & SelectProps<T, U, V>
288
+ ) => ReturnType<typeof CreatableSelectInner>;
@@ -13,8 +13,6 @@ export const toggleButtonCss = css`
13
13
  box-sizing: border-box;
14
14
  display: flex;
15
15
  align-items: center;
16
- min-height: 24px;
17
- font-size: ${textStyles.small.fontSize}px;
18
16
  border-radius: ${border.radius8}px;
19
17
 
20
18
  transition-property: border, background-color, color, opacity, box-shadow;
@@ -23,8 +21,6 @@ export const toggleButtonCss = css`
23
21
  color: ${themeVars.textColor};
24
22
  ${iconColorVar}: ${themeVars.colorIconButtonGhostNeutral};
25
23
 
26
- ${iconSizeVar}: 16px;
27
-
28
24
  border: 1.5px solid ${themeVars.colorBorderReactionsHover};
29
25
  background-color: ${themeVars.colorBgReactionsDefault};
30
26
 
@@ -37,13 +33,29 @@ export const toggleButtonCss = css`
37
33
  line-height: 16px;
38
34
  user-select: none;
39
35
 
36
+ &[data-size="small"] {
37
+ font-size: ${textStyles.small.fontSize}px;
38
+ line-height: 16px;
39
+ min-height: 24px;
40
+
41
+ ${iconSizeVar}: 16px;
42
+ }
43
+
40
44
  &[data-size="medium"] {
45
+ font-size: ${textStyles.regular.fontSize}px;
46
+ line-height: 20px;
41
47
  min-height: 28px;
48
+
49
+ ${iconSizeVar}: 18px;
42
50
  }
43
51
 
44
52
  &[data-size="large"] {
53
+ font-size: ${textStyles.regular.fontSize}px;
54
+ line-height: 20px;
45
55
  min-height: 32px;
46
56
  padding-inline: 7px;
57
+
58
+ ${iconSizeVar}: 18px;
47
59
  }
48
60
 
49
61
  &:not(:disabled, [aria-readonly="true"]) {
@@ -3,6 +3,7 @@ import {FC, ComponentProps, useMemo} from "react";
3
3
  import {ThemeColors} from "./design-system";
4
4
  import {useTheme} from "./theme-provider";
5
5
  import {Toggle} from "./toggle";
6
+ import {useIsPhone} from "./use-is-phone";
6
7
 
7
8
  const getBackgroundColors = (accentColor: string | undefined, theme: ThemeColors) => {
8
9
  return accentColor && chroma.valid(accentColor)
@@ -21,9 +22,17 @@ interface ToggleOnOffProps extends Omit<ComponentProps<typeof Toggle>, "backgrou
21
22
  }
22
23
 
23
24
  export const ToggleOnOff: FC<ToggleOnOffProps> = ({value, color, ...rest}) => {
25
+ const isPhone = useIsPhone();
24
26
  const theme = useTheme();
25
27
  const {enabledColor, disabledColor} = useMemo(() => getBackgroundColors(color, theme), [color, theme]);
26
- return <Toggle value={Boolean(value)} backgroundColor={value ? enabledColor : disabledColor} {...rest} />;
28
+ return (
29
+ <Toggle
30
+ value={Boolean(value)}
31
+ size={isPhone ? "large" : "medium"}
32
+ backgroundColor={value ? enabledColor : disabledColor}
33
+ {...rest}
34
+ />
35
+ );
27
36
  };
28
37
 
29
38
  interface ReduxFormToggleOnOffProps extends ToggleOnOffProps {