@true-engineering/true-react-common-ui-kit 3.5.0 → 3.6.0
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.
- package/README.md +4 -0
- package/dist/components/Select/CustomSelect.stories.d.ts +14 -0
- package/dist/components/Select/Select.d.ts +1 -1
- package/dist/components/Select/components/SelectList/SelectList.d.ts +1 -1
- package/dist/true-react-common-ui-kit.js +2 -2
- package/dist/true-react-common-ui-kit.umd.cjs +2 -2
- package/package.json +1 -1
- package/src/components/Select/CustomSelect.stories.tsx +217 -0
- package/src/components/Select/Select.tsx +3 -2
- package/src/components/Select/components/SelectList/SelectList.tsx +2 -2
package/README.md
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { type ComponentMeta, type ComponentStory } from '@storybook/react';
|
|
3
|
+
import { type ISelectProps } from './Select';
|
|
4
|
+
interface ISelectWithCustomProps<T> extends ISelectProps<T> {
|
|
5
|
+
shouldUsePopper?: boolean;
|
|
6
|
+
shouldRenderInBody?: boolean;
|
|
7
|
+
shouldHideOnScroll?: boolean;
|
|
8
|
+
canBeFlipped?: boolean;
|
|
9
|
+
scrollParent?: 'document' | 'auto';
|
|
10
|
+
}
|
|
11
|
+
declare function SelectWithCustomProps({ noMatchesLabel, shouldUsePopper, shouldRenderInBody, shouldHideOnScroll, canBeFlipped, scrollParent, ...restProps }: ISelectWithCustomProps<string>): JSX.Element;
|
|
12
|
+
declare const _default: ComponentMeta<typeof SelectWithCustomProps>;
|
|
13
|
+
export default _default;
|
|
14
|
+
export declare const CustomSelect: ComponentStory<typeof SelectWithCustomProps>;
|
|
@@ -6,7 +6,7 @@ import { ISearchInputProps } from '../SearchInput';
|
|
|
6
6
|
import { IMultipleSelectValue } from './types';
|
|
7
7
|
import { ISelectStyles } from './Select.styles';
|
|
8
8
|
export interface ISelectProps<Value> extends Omit<IInputProps, 'value' | 'onChange' | 'onBlur' | 'type' | 'tweakStyles'>, ICommonProps<ISelectStyles> {
|
|
9
|
-
defaultOptionLabel?:
|
|
9
|
+
defaultOptionLabel?: ReactNode;
|
|
10
10
|
allOptionsLabel?: string;
|
|
11
11
|
noMatchesLabel?: string;
|
|
12
12
|
loadingLabel?: ReactNode;
|
|
@@ -8,7 +8,7 @@ export interface ISelectListProps<Value> extends ICommonProps<ISelectListStyles>
|
|
|
8
8
|
noMatchesLabel?: string;
|
|
9
9
|
isLoading?: boolean;
|
|
10
10
|
loadingLabel?: ReactNode;
|
|
11
|
-
defaultOptionLabel?:
|
|
11
|
+
defaultOptionLabel?: ReactNode;
|
|
12
12
|
allOptionsLabel?: string;
|
|
13
13
|
areAllOptionsSelected?: boolean;
|
|
14
14
|
shouldScrollToList?: boolean;
|
|
@@ -10962,7 +10962,7 @@ function SelectList(param) {
|
|
|
10962
10962
|
children: loadingLabel
|
|
10963
10963
|
}) : /* @__PURE__ */ jsxs(Fragment, {
|
|
10964
10964
|
children: [
|
|
10965
|
-
|
|
10965
|
+
isReactNodeNotEmpty(defaultOptionLabel) && /* @__PURE__ */ jsx(ScrollIntoViewIfNeeded, {
|
|
10966
10966
|
active: focusedIndex === DEFAULT_OPTION_INDEX,
|
|
10967
10967
|
options: {
|
|
10968
10968
|
block: "nearest"
|
|
@@ -11481,7 +11481,7 @@ function Select(props) {
|
|
|
11481
11481
|
var isMounted = useIsMounted();
|
|
11482
11482
|
var _useState = _sliced_to_array$k(useState(false), 2), isListOpen = _useState[0], setIsListOpen = _useState[1];
|
|
11483
11483
|
var _useState1 = _sliced_to_array$k(useState(false), 2), areOptionsLoading = _useState1[0], setAreOptionsLoading = _useState1[1];
|
|
11484
|
-
var hasDefaultOption =
|
|
11484
|
+
var hasDefaultOption = isReactNodeNotEmpty(defaultOptionLabel);
|
|
11485
11485
|
var _useState2 = _sliced_to_array$k(useState(DEFAULT_OPTION_INDEX), 2), focusedListCellIndex = _useState2[0], setFocusedListCellIndex = _useState2[1];
|
|
11486
11486
|
var _useState3 = _sliced_to_array$k(useState(""), 2), searchValue = _useState3[0], setSearchValue = _useState3[1];
|
|
11487
11487
|
var _useState4 = _sliced_to_array$k(useState(true), 2), shouldShowDefaultOption = _useState4[0], setShouldShowDefaultOption = _useState4[1];
|
|
@@ -10952,7 +10952,7 @@
|
|
|
10952
10952
|
children: loadingLabel
|
|
10953
10953
|
}) : /* @__PURE__ */ jsxs(Fragment, {
|
|
10954
10954
|
children: [
|
|
10955
|
-
trueReactPlatformHelpers.
|
|
10955
|
+
trueReactPlatformHelpers.isReactNodeNotEmpty(defaultOptionLabel) && /* @__PURE__ */ jsx(ScrollIntoViewIfNeeded, {
|
|
10956
10956
|
active: focusedIndex === DEFAULT_OPTION_INDEX,
|
|
10957
10957
|
options: {
|
|
10958
10958
|
block: "nearest"
|
|
@@ -11471,7 +11471,7 @@
|
|
|
11471
11471
|
var isMounted = useIsMounted();
|
|
11472
11472
|
var _useState = _sliced_to_array$k(React.useState(false), 2), isListOpen = _useState[0], setIsListOpen = _useState[1];
|
|
11473
11473
|
var _useState1 = _sliced_to_array$k(React.useState(false), 2), areOptionsLoading = _useState1[0], setAreOptionsLoading = _useState1[1];
|
|
11474
|
-
var hasDefaultOption = trueReactPlatformHelpers.
|
|
11474
|
+
var hasDefaultOption = trueReactPlatformHelpers.isReactNodeNotEmpty(defaultOptionLabel);
|
|
11475
11475
|
var _useState2 = _sliced_to_array$k(React.useState(DEFAULT_OPTION_INDEX), 2), focusedListCellIndex = _useState2[0], setFocusedListCellIndex = _useState2[1];
|
|
11476
11476
|
var _useState3 = _sliced_to_array$k(React.useState(""), 2), searchValue = _useState3[0], setSearchValue = _useState3[1];
|
|
11477
11477
|
var _useState4 = _sliced_to_array$k(React.useState(true), 2), shouldShowDefaultOption = _useState4[0], setShouldShowDefaultOption = _useState4[1];
|
package/package.json
CHANGED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { createUseStyles } from 'react-jss';
|
|
3
|
+
import { isStringNotEmpty } from '@true-engineering/true-react-platform-helpers';
|
|
4
|
+
import { type ComponentMeta, type ComponentStory } from '@storybook/react';
|
|
5
|
+
import { Button, type IButtonStyles } from '../Button';
|
|
6
|
+
import { Input, type IInputStyles } from '../Input';
|
|
7
|
+
import { TextButton } from '../TextButton';
|
|
8
|
+
import { Select, type ISelectProps } from './Select';
|
|
9
|
+
|
|
10
|
+
interface ISelectWithCustomProps<T> extends ISelectProps<T> {
|
|
11
|
+
shouldUsePopper?: boolean;
|
|
12
|
+
shouldRenderInBody?: boolean;
|
|
13
|
+
shouldHideOnScroll?: boolean;
|
|
14
|
+
canBeFlipped?: boolean;
|
|
15
|
+
scrollParent?: 'document' | 'auto';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const useSelectStyles = createUseStyles({
|
|
19
|
+
option: {
|
|
20
|
+
width: '100%',
|
|
21
|
+
gap: 10,
|
|
22
|
+
display: 'grid',
|
|
23
|
+
gridTemplateColumns: 'minmax(0, 1fr) max-content',
|
|
24
|
+
alignItems: 'center',
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const useDefaultOptionStyles = createUseStyles({
|
|
29
|
+
customDefaultOption: {
|
|
30
|
+
width: '100%',
|
|
31
|
+
padding: [10, 20],
|
|
32
|
+
justifySelf: 'stretch',
|
|
33
|
+
alignSelf: 'stretch',
|
|
34
|
+
backgroundColor: '#ffffff',
|
|
35
|
+
cursor: 'default',
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
defaultView: {
|
|
39
|
+
display: 'grid',
|
|
40
|
+
placeItems: 'center',
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
inputView: {
|
|
44
|
+
gap: 10,
|
|
45
|
+
display: 'grid',
|
|
46
|
+
gridTemplateColumns: '1fr max-content',
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const inputTweakStyles: IInputStyles = {
|
|
51
|
+
inputWrapper: {
|
|
52
|
+
height: 24,
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
input: {
|
|
56
|
+
padding: [0, 4],
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const buttonTweakStyles: IButtonStyles = {
|
|
61
|
+
s: {
|
|
62
|
+
height: 24,
|
|
63
|
+
|
|
64
|
+
'&$onlyIcon': {
|
|
65
|
+
width: 24,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
interface ICustomDefaultOptionProps {
|
|
71
|
+
onAdd?: (option?: string) => void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function CustomDefaultOption({ onAdd }: ICustomDefaultOptionProps) {
|
|
75
|
+
const classes = useDefaultOptionStyles();
|
|
76
|
+
|
|
77
|
+
const [isAdding, setIsAdding] = useState(false);
|
|
78
|
+
const [inputValue, setInputValue] = useState('');
|
|
79
|
+
|
|
80
|
+
const onAddClick = () => {
|
|
81
|
+
onAdd?.(inputValue);
|
|
82
|
+
setIsAdding(false);
|
|
83
|
+
setInputValue('');
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<div className={classes.customDefaultOption} onClick={(event) => event.stopPropagation()}>
|
|
88
|
+
{!isAdding && (
|
|
89
|
+
<div className={classes.defaultView}>
|
|
90
|
+
<TextButton icon="plus" hasCircleUnderIcon isBold onClick={() => setIsAdding(true)}>
|
|
91
|
+
Add Option
|
|
92
|
+
</TextButton>
|
|
93
|
+
</div>
|
|
94
|
+
)}
|
|
95
|
+
{isAdding && (
|
|
96
|
+
<div className={classes.inputView}>
|
|
97
|
+
<Input
|
|
98
|
+
value={inputValue}
|
|
99
|
+
border="bottom"
|
|
100
|
+
placeholder="Option Value"
|
|
101
|
+
tweakStyles={inputTweakStyles}
|
|
102
|
+
shouldFocusOnMount
|
|
103
|
+
onChange={setInputValue}
|
|
104
|
+
/>
|
|
105
|
+
<Button size="s" icon="plus" tweakStyles={buttonTweakStyles} onClick={onAddClick} />
|
|
106
|
+
</div>
|
|
107
|
+
)}
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function SelectWithCustomProps({
|
|
113
|
+
noMatchesLabel,
|
|
114
|
+
shouldUsePopper,
|
|
115
|
+
shouldRenderInBody,
|
|
116
|
+
shouldHideOnScroll,
|
|
117
|
+
canBeFlipped,
|
|
118
|
+
scrollParent,
|
|
119
|
+
...restProps
|
|
120
|
+
}: ISelectWithCustomProps<string>) {
|
|
121
|
+
const classes = useSelectStyles();
|
|
122
|
+
const [inputValue, setInputValue] = useState<string>();
|
|
123
|
+
const [options, setOptions] = useState<string[]>([]);
|
|
124
|
+
|
|
125
|
+
const onAdd = (option?: string) => {
|
|
126
|
+
if (isStringNotEmpty(option)) {
|
|
127
|
+
setOptions((prevOptions) => [...prevOptions, option]);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const onRemove = (option: string) => {
|
|
132
|
+
setOptions((prevOptions) => prevOptions.filter((entry) => entry !== option));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const renderOption = (value: string) => (
|
|
136
|
+
<div className={classes.option}>
|
|
137
|
+
<span>{value}</span>
|
|
138
|
+
<Button
|
|
139
|
+
size="s"
|
|
140
|
+
icon="trash-can"
|
|
141
|
+
tweakStyles={buttonTweakStyles}
|
|
142
|
+
onClick={(event) => {
|
|
143
|
+
event.stopPropagation();
|
|
144
|
+
|
|
145
|
+
onRemove(value);
|
|
146
|
+
}}
|
|
147
|
+
/>
|
|
148
|
+
</div>
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const props = {
|
|
152
|
+
...restProps,
|
|
153
|
+
onChange: setInputValue,
|
|
154
|
+
value: inputValue,
|
|
155
|
+
options,
|
|
156
|
+
convertValueToReactNode: renderOption,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<Select
|
|
161
|
+
{...props}
|
|
162
|
+
defaultOptionLabel={<CustomDefaultOption onAdd={onAdd} />}
|
|
163
|
+
noMatchesLabel={isStringNotEmpty(noMatchesLabel) ? noMatchesLabel : undefined}
|
|
164
|
+
tweakStyles={{
|
|
165
|
+
tweakSelectList: {
|
|
166
|
+
defaultCell: {
|
|
167
|
+
padding: 0,
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
}}
|
|
171
|
+
dropdownOptions={{
|
|
172
|
+
shouldUsePopper,
|
|
173
|
+
shouldRenderInBody,
|
|
174
|
+
shouldHideOnScroll,
|
|
175
|
+
canBeFlipped,
|
|
176
|
+
scrollParent,
|
|
177
|
+
}}
|
|
178
|
+
/>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export default {
|
|
183
|
+
title: 'Select',
|
|
184
|
+
component: SelectWithCustomProps,
|
|
185
|
+
argTypes: {
|
|
186
|
+
scrollParent: {
|
|
187
|
+
options: ['document', 'auto'],
|
|
188
|
+
control: { type: 'select' },
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
} as ComponentMeta<typeof SelectWithCustomProps>;
|
|
192
|
+
|
|
193
|
+
const Template: ComponentStory<typeof SelectWithCustomProps> = (args) => (
|
|
194
|
+
<SelectWithCustomProps {...args} />
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
export const CustomSelect = Template.bind({});
|
|
198
|
+
|
|
199
|
+
CustomSelect.args = {
|
|
200
|
+
label: 'Dropdown',
|
|
201
|
+
noMatchesLabel: 'No matches',
|
|
202
|
+
isInvalid: false,
|
|
203
|
+
errorMessage: '',
|
|
204
|
+
errorPosition: 'bottom',
|
|
205
|
+
hasFloatingLabel: true,
|
|
206
|
+
hasRequiredLabel: false,
|
|
207
|
+
isDisabled: false,
|
|
208
|
+
isRequired: false,
|
|
209
|
+
isClearable: false,
|
|
210
|
+
|
|
211
|
+
shouldUsePopper: false,
|
|
212
|
+
shouldRenderInBody: false,
|
|
213
|
+
shouldHideOnScroll: false,
|
|
214
|
+
shouldScrollToList: true,
|
|
215
|
+
canBeFlipped: false,
|
|
216
|
+
scrollParent: 'document',
|
|
217
|
+
};
|
|
@@ -17,6 +17,7 @@ import { debounce } from 'ts-debounce';
|
|
|
17
17
|
import {
|
|
18
18
|
getTestId,
|
|
19
19
|
isNotEmpty,
|
|
20
|
+
isReactNodeNotEmpty,
|
|
20
21
|
isStringNotEmpty,
|
|
21
22
|
createFilter,
|
|
22
23
|
} from '@true-engineering/true-react-platform-helpers';
|
|
@@ -41,7 +42,7 @@ import { useStyles, ISelectStyles, searchInputStyles, getInputStyles } from './S
|
|
|
41
42
|
export interface ISelectProps<Value>
|
|
42
43
|
extends Omit<IInputProps, 'value' | 'onChange' | 'onBlur' | 'type' | 'tweakStyles'>,
|
|
43
44
|
ICommonProps<ISelectStyles> {
|
|
44
|
-
defaultOptionLabel?:
|
|
45
|
+
defaultOptionLabel?: ReactNode;
|
|
45
46
|
allOptionsLabel?: string;
|
|
46
47
|
noMatchesLabel?: string;
|
|
47
48
|
loadingLabel?: ReactNode;
|
|
@@ -150,7 +151,7 @@ export function Select<Value>(
|
|
|
150
151
|
const isMounted = useIsMounted();
|
|
151
152
|
const [isListOpen, setIsListOpen] = useState(false);
|
|
152
153
|
const [areOptionsLoading, setAreOptionsLoading] = useState(false);
|
|
153
|
-
const hasDefaultOption =
|
|
154
|
+
const hasDefaultOption = isReactNodeNotEmpty(defaultOptionLabel);
|
|
154
155
|
|
|
155
156
|
const [focusedListCellIndex, setFocusedListCellIndex] = useState(DEFAULT_OPTION_INDEX);
|
|
156
157
|
const [searchValue, setSearchValue] = useState('');
|
|
@@ -20,7 +20,7 @@ export interface ISelectListProps<Value> extends ICommonProps<ISelectListStyles>
|
|
|
20
20
|
noMatchesLabel?: string;
|
|
21
21
|
isLoading?: boolean;
|
|
22
22
|
loadingLabel?: ReactNode;
|
|
23
|
-
defaultOptionLabel?:
|
|
23
|
+
defaultOptionLabel?: ReactNode;
|
|
24
24
|
allOptionsLabel?: string;
|
|
25
25
|
areAllOptionsSelected?: boolean;
|
|
26
26
|
shouldScrollToList?: boolean;
|
|
@@ -97,7 +97,7 @@ export function SelectList<Value>({
|
|
|
97
97
|
<div className={clsx(classes.cell, classes.loading)}>{loadingLabel}</div>
|
|
98
98
|
) : (
|
|
99
99
|
<>
|
|
100
|
-
{
|
|
100
|
+
{isReactNodeNotEmpty(defaultOptionLabel) && (
|
|
101
101
|
<ScrollIntoViewIfNeeded
|
|
102
102
|
active={focusedIndex === DEFAULT_OPTION_INDEX}
|
|
103
103
|
options={{ block: 'nearest' }}
|