@appdirect/design-system 0.0.23 → 0.0.25
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/dist/Combobox/ADMultiselect.js +91 -0
- package/dist/Combobox/ADMultiselect.tsx +237 -0
- package/dist/Combobox/AutocompleteClearable.js +33 -35
- package/dist/Combobox/AutocompleteClearable.tsx +56 -121
- package/dist/Combobox/SearchableSelect.js +23 -42
- package/dist/Combobox/SearchableSelect.tsx +33 -129
- package/dist/Combobox/Select.js +13 -19
- package/dist/Combobox/Select.tsx +15 -44
- package/dist/Combobox/index.js +1 -1
- package/dist/Combobox/index.ts +2 -2
- package/dist/ComplexComponents/Utilities/CopyButton.js +15 -27
- package/dist/ComplexComponents/Utilities/CopyButton.tsx +35 -49
- package/dist/DESIGN.md +2 -2
- package/dist/DataDisplay/Avatar.js +26 -81
- package/dist/DataDisplay/Avatar.tsx +35 -93
- package/dist/DataDisplay/Pill.js +21 -79
- package/dist/DataDisplay/Pill.tsx +34 -116
- package/dist/Inputs/ColorInput.js +12 -18
- package/dist/Inputs/ColorInput.tsx +13 -41
- package/dist/Inputs/NumberInput.js +12 -36
- package/dist/Inputs/NumberInput.tsx +13 -72
- package/dist/Inputs/TextArea.js +12 -18
- package/dist/Inputs/TextArea.tsx +13 -41
- package/dist/Inputs/TextInput.js +12 -18
- package/dist/Inputs/TextInput.tsx +13 -41
- package/dist/Inputs/inputLabel.js +35 -0
- package/dist/Inputs/inputLabel.tsx +83 -0
- package/dist/Layout/Inline.js +5 -42
- package/dist/Layout/Inline.tsx +6 -79
- package/dist/Overlays/Tooltip.js +2 -2
- package/dist/Overlays/Tooltip.tsx +4 -1
- package/dist/contracts/index.json +1 -1
- package/dist/css/mantine.css +193 -29
- package/dist/css/tokens.css +58 -18
- package/dist/index.js +3 -3
- package/dist/index.ts +4 -4
- package/dist/stories/Combobox/{Multiselect.stories.tsx → ADMultiselect.stories.tsx} +159 -53
- package/dist/stories/Combobox/SearchableSelect.stories.tsx +3 -3
- package/dist/stories/Combobox/Select.stories.tsx +1 -1
- package/dist/stories/ComplexComponents/Utilities/CopyButton.stories.tsx +4 -14
- package/dist/stories/Inputs/ColorInput.stories.tsx +12 -0
- package/dist/stories/Inputs/NumberInput.stories.tsx +12 -0
- package/dist/stories/Inputs/TextArea.stories.tsx +12 -0
- package/dist/stories/Inputs/TextInput.stories.tsx +12 -0
- package/dist/stories/index.json +42 -35
- package/package.json +2 -2
- package/dist/Combobox/Multiselect.js +0 -31
- package/dist/Combobox/Multiselect.tsx +0 -158
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import React, { forwardRef, useMemo } from 'react';
|
|
4
|
+
import { MultiSelect as MantineMultiSelect, } from '@mantine/core';
|
|
5
|
+
import { useUncontrolled } from '@mantine/hooks';
|
|
6
|
+
import { IconSelector } from '@tabler/icons-react';
|
|
7
|
+
import { Checkbox } from '../Inputs/Checkbox.js';
|
|
8
|
+
import { Inline } from '../Layout/index.js';
|
|
9
|
+
import { Pill } from '../DataDisplay/Pill.js';
|
|
10
|
+
import { getInputLabelProps } from '../Inputs/inputLabel.js';
|
|
11
|
+
import { Tooltip } from '../Overlays/Tooltip.js';
|
|
12
|
+
const MAX_DISPLAYED_VALUES = 2;
|
|
13
|
+
// ========================== HELPERS ==========================
|
|
14
|
+
/** Flatten ComboboxData (strings, items and groups) into a flat item list. */
|
|
15
|
+
function flattenData(data) {
|
|
16
|
+
const items = [];
|
|
17
|
+
const push = (item) => items.push(typeof item === 'string'
|
|
18
|
+
? { value: item, label: item }
|
|
19
|
+
: { value: item.value, label: item.label ?? item.value });
|
|
20
|
+
data?.forEach((entry) => {
|
|
21
|
+
if (typeof entry !== 'string' && 'group' in entry)
|
|
22
|
+
entry.items.forEach(push);
|
|
23
|
+
else
|
|
24
|
+
push(entry);
|
|
25
|
+
});
|
|
26
|
+
return items;
|
|
27
|
+
}
|
|
28
|
+
// ========================== COMPONENT ==========================
|
|
29
|
+
/**
|
|
30
|
+
* AppDirect multi-value select.
|
|
31
|
+
*
|
|
32
|
+
* Mantine's `MultiSelect` with two DS customizations, everything else
|
|
33
|
+
* (value/onChange, search, clearable, hidden form input, ARIA, keyboard
|
|
34
|
+
* handling, ...) is core behavior and every core prop is supported:
|
|
35
|
+
*
|
|
36
|
+
* - **"+N more" pill:** only `maxDisplayedValues` pill slots are shown; the
|
|
37
|
+
* remaining values collapse into a "+N more" pill whose tooltip lists them.
|
|
38
|
+
* - **Checkbox options:** options render with a checkbox instead of a check icon.
|
|
39
|
+
*
|
|
40
|
+
* Design hand-off: there is intentionally no Figma component. It looks the same as
|
|
41
|
+
* `Combobox`, so designers use that and engineering swaps this in for multi-value fields.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* <ADMultiselect
|
|
46
|
+
* label="Technologies"
|
|
47
|
+
* placeholder="Pick one or more"
|
|
48
|
+
* data={['React', 'Vue', 'Angular']}
|
|
49
|
+
* value={value}
|
|
50
|
+
* onChange={setValue}
|
|
51
|
+
* maxDisplayedValues={2}
|
|
52
|
+
* />
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export const ADMultiselect = forwardRef(({ size = 'md', label, required, showOptional = false, hasHelpIcon = false, helpIconLabel, onHelpIconClick, error, errorCaption, maxDisplayedValues = MAX_DISPLAYED_VALUES, value, defaultValue, onChange, data, placeholder, readOnly, withCheckIcon = false, renderOption, variant, ...props }, ref) => {
|
|
56
|
+
const [selected, setSelected] = useUncontrolled({
|
|
57
|
+
value,
|
|
58
|
+
defaultValue,
|
|
59
|
+
finalValue: [],
|
|
60
|
+
onChange,
|
|
61
|
+
});
|
|
62
|
+
const effectiveError = error === true ? (errorCaption ?? true) : error;
|
|
63
|
+
const labelProps = getInputLabelProps({
|
|
64
|
+
label,
|
|
65
|
+
required,
|
|
66
|
+
showOptional,
|
|
67
|
+
hasHelpIcon,
|
|
68
|
+
helpIconLabel,
|
|
69
|
+
onHelpIconClick,
|
|
70
|
+
});
|
|
71
|
+
const labelsByValue = useMemo(() => new Map(flattenData(data).map((item) => [item.value, item.label])), [data]);
|
|
72
|
+
// The "+N more" pill occupies the last visible slot once values overflow.
|
|
73
|
+
const hasOverflow = selected.length > maxDisplayedValues;
|
|
74
|
+
const visibleCount = hasOverflow ? Math.max(maxDisplayedValues - 1, 0) : selected.length;
|
|
75
|
+
const hiddenValues = selected.slice(visibleCount);
|
|
76
|
+
const renderPill = ({ option, value: pillValue, onRemove, disabled, }) => {
|
|
77
|
+
const index = pillValue === undefined ? -1 : selected.indexOf(pillValue);
|
|
78
|
+
if (index < visibleCount) {
|
|
79
|
+
return (_jsx(Pill, { size: size, disabled: disabled, withRemoveButton: !readOnly && !option?.disabled, onRemove: onRemove, children: option?.label ?? pillValue }));
|
|
80
|
+
}
|
|
81
|
+
if (index === visibleCount) {
|
|
82
|
+
return (_jsx(Tooltip, { position: "top", maxWidth: 420, label: _jsx(Inline, { gap: 4, children: hiddenValues.map((hidden, i) => (_jsxs("span", { children: [labelsByValue.get(hidden) ?? hidden, i < hiddenValues.length - 1 ? ',' : ''] }, hidden))) }), children: _jsxs(Pill, { size: size, disabled: disabled, children: ["+", hiddenValues.length, " more"] }) }));
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
};
|
|
86
|
+
const renderCheckboxOption = ({ option, checked }) => (_jsxs(Inline, { gap: "sm", wrap: "nowrap", children: [_jsx(Checkbox, { checked: !!checked, disabled: option.disabled, onChange: () => { }, "aria-hidden": true, tabIndex: -1, style: { pointerEvents: 'none' } }), _jsx("span", { children: option.label })] }));
|
|
87
|
+
return (_jsx(MantineMultiSelect, { ...props, ref: ref, size: size, radius: "sm", variant: variant ?? 'default', ...labelProps, required: required, data: data,
|
|
88
|
+
// Core keeps the placeholder visible next to the pills; the DS shows it only when empty.
|
|
89
|
+
placeholder: selected.length > 0 ? undefined : placeholder, value: selected, onChange: setSelected, readOnly: readOnly, withCheckIcon: withCheckIcon, renderOption: renderOption ?? renderCheckboxOption, renderPill: renderPill, rightSection: _jsx(IconSelector, { size: 14, "aria-hidden": true }), rightSectionPointerEvents: props.clearable ? 'auto' : 'none', error: effectiveError }));
|
|
90
|
+
});
|
|
91
|
+
ADMultiselect.displayName = 'ADMultiselect';
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, { forwardRef, useMemo } from 'react';
|
|
4
|
+
import {
|
|
5
|
+
MultiSelect as MantineMultiSelect,
|
|
6
|
+
MultiSelectProps as MantineMultiSelectProps,
|
|
7
|
+
ComboboxData,
|
|
8
|
+
ComboboxItem,
|
|
9
|
+
} from '@mantine/core';
|
|
10
|
+
import { useUncontrolled } from '@mantine/hooks';
|
|
11
|
+
import { IconSelector } from '@tabler/icons-react';
|
|
12
|
+
import { ComponentSize } from '../config';
|
|
13
|
+
import { Checkbox } from '../Inputs/Checkbox';
|
|
14
|
+
import { Inline } from '../Layout';
|
|
15
|
+
import { Pill } from '../DataDisplay/Pill';
|
|
16
|
+
import { getInputLabelProps } from '../Inputs/inputLabel';
|
|
17
|
+
import { Tooltip } from '../Overlays/Tooltip';
|
|
18
|
+
|
|
19
|
+
const MAX_DISPLAYED_VALUES = 2;
|
|
20
|
+
|
|
21
|
+
// ========================== TYPES ==========================
|
|
22
|
+
|
|
23
|
+
export interface ADMultiselectProps
|
|
24
|
+
extends Omit<MantineMultiSelectProps, 'size' | 'radius' | 'rightSection' | 'renderPill'> {
|
|
25
|
+
/** Input size from design system scale */
|
|
26
|
+
size?: ComponentSize;
|
|
27
|
+
/** Whether to show "(Optional)" text after the label */
|
|
28
|
+
showOptional?: boolean;
|
|
29
|
+
/** Whether to show a (?) help icon with a tooltip to the right of the label */
|
|
30
|
+
hasHelpIcon?: boolean;
|
|
31
|
+
/** Tooltip label shown when hovering the help icon. Also used as aria-label for a11y */
|
|
32
|
+
helpIconLabel?: string;
|
|
33
|
+
/** Optional click handler for the help icon */
|
|
34
|
+
onHelpIconClick?: React.MouseEventHandler<HTMLButtonElement>;
|
|
35
|
+
/**
|
|
36
|
+
* Optional error message text shown below the input when in error state.
|
|
37
|
+
* Used with error={true} for red border + message; omit for red border only.
|
|
38
|
+
*/
|
|
39
|
+
errorCaption?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Maximum number of pills shown before the rest collapse into a "+N more"
|
|
42
|
+
* pill. The "+N more" pill takes the last slot, so `2` shows one value pill
|
|
43
|
+
* plus "+N more" once more than two values are selected. Hovering it lists
|
|
44
|
+
* the hidden values.
|
|
45
|
+
*/
|
|
46
|
+
maxDisplayedValues?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ========================== HELPERS ==========================
|
|
50
|
+
|
|
51
|
+
/** Flatten ComboboxData (strings, items and groups) into a flat item list. */
|
|
52
|
+
function flattenData(data: ComboboxData | undefined): ComboboxItem[] {
|
|
53
|
+
const items: ComboboxItem[] = [];
|
|
54
|
+
const push = (item: string | { value: string; label?: string }) =>
|
|
55
|
+
items.push(
|
|
56
|
+
typeof item === 'string'
|
|
57
|
+
? { value: item, label: item }
|
|
58
|
+
: { value: item.value, label: item.label ?? item.value }
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
data?.forEach((entry) => {
|
|
62
|
+
if (typeof entry !== 'string' && 'group' in entry) entry.items.forEach(push);
|
|
63
|
+
else push(entry as string | { value: string; label?: string });
|
|
64
|
+
});
|
|
65
|
+
return items;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ========================== COMPONENT ==========================
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* AppDirect multi-value select.
|
|
72
|
+
*
|
|
73
|
+
* Mantine's `MultiSelect` with two DS customizations, everything else
|
|
74
|
+
* (value/onChange, search, clearable, hidden form input, ARIA, keyboard
|
|
75
|
+
* handling, ...) is core behavior and every core prop is supported:
|
|
76
|
+
*
|
|
77
|
+
* - **"+N more" pill:** only `maxDisplayedValues` pill slots are shown; the
|
|
78
|
+
* remaining values collapse into a "+N more" pill whose tooltip lists them.
|
|
79
|
+
* - **Checkbox options:** options render with a checkbox instead of a check icon.
|
|
80
|
+
*
|
|
81
|
+
* Design hand-off: there is intentionally no Figma component. It looks the same as
|
|
82
|
+
* `Combobox`, so designers use that and engineering swaps this in for multi-value fields.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```tsx
|
|
86
|
+
* <ADMultiselect
|
|
87
|
+
* label="Technologies"
|
|
88
|
+
* placeholder="Pick one or more"
|
|
89
|
+
* data={['React', 'Vue', 'Angular']}
|
|
90
|
+
* value={value}
|
|
91
|
+
* onChange={setValue}
|
|
92
|
+
* maxDisplayedValues={2}
|
|
93
|
+
* />
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export const ADMultiselect = forwardRef<HTMLInputElement, ADMultiselectProps>(
|
|
97
|
+
(
|
|
98
|
+
{
|
|
99
|
+
size = 'md',
|
|
100
|
+
label,
|
|
101
|
+
required,
|
|
102
|
+
showOptional = false,
|
|
103
|
+
hasHelpIcon = false,
|
|
104
|
+
helpIconLabel,
|
|
105
|
+
onHelpIconClick,
|
|
106
|
+
error,
|
|
107
|
+
errorCaption,
|
|
108
|
+
maxDisplayedValues = MAX_DISPLAYED_VALUES,
|
|
109
|
+
value,
|
|
110
|
+
defaultValue,
|
|
111
|
+
onChange,
|
|
112
|
+
data,
|
|
113
|
+
placeholder,
|
|
114
|
+
readOnly,
|
|
115
|
+
withCheckIcon = false,
|
|
116
|
+
renderOption,
|
|
117
|
+
variant,
|
|
118
|
+
...props
|
|
119
|
+
},
|
|
120
|
+
ref
|
|
121
|
+
) => {
|
|
122
|
+
const [selected, setSelected] = useUncontrolled<string[]>({
|
|
123
|
+
value,
|
|
124
|
+
defaultValue,
|
|
125
|
+
finalValue: [],
|
|
126
|
+
onChange,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const effectiveError = error === true ? (errorCaption ?? true) : error;
|
|
130
|
+
|
|
131
|
+
const labelProps = getInputLabelProps({
|
|
132
|
+
label,
|
|
133
|
+
required,
|
|
134
|
+
showOptional,
|
|
135
|
+
hasHelpIcon,
|
|
136
|
+
helpIconLabel,
|
|
137
|
+
onHelpIconClick,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const labelsByValue = useMemo(
|
|
141
|
+
() => new Map(flattenData(data).map((item) => [item.value, item.label])),
|
|
142
|
+
[data]
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// The "+N more" pill occupies the last visible slot once values overflow.
|
|
146
|
+
const hasOverflow = selected.length > maxDisplayedValues;
|
|
147
|
+
const visibleCount = hasOverflow ? Math.max(maxDisplayedValues - 1, 0) : selected.length;
|
|
148
|
+
const hiddenValues = selected.slice(visibleCount);
|
|
149
|
+
|
|
150
|
+
const renderPill: MantineMultiSelectProps['renderPill'] = ({
|
|
151
|
+
option,
|
|
152
|
+
value: pillValue,
|
|
153
|
+
onRemove,
|
|
154
|
+
disabled,
|
|
155
|
+
}) => {
|
|
156
|
+
const index = pillValue === undefined ? -1 : selected.indexOf(pillValue);
|
|
157
|
+
|
|
158
|
+
if (index < visibleCount) {
|
|
159
|
+
return (
|
|
160
|
+
<Pill
|
|
161
|
+
size={size}
|
|
162
|
+
disabled={disabled}
|
|
163
|
+
withRemoveButton={!readOnly && !option?.disabled}
|
|
164
|
+
onRemove={onRemove}
|
|
165
|
+
>
|
|
166
|
+
{option?.label ?? pillValue}
|
|
167
|
+
</Pill>
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (index === visibleCount) {
|
|
172
|
+
return (
|
|
173
|
+
<Tooltip
|
|
174
|
+
position="top"
|
|
175
|
+
maxWidth={420}
|
|
176
|
+
label={
|
|
177
|
+
<Inline gap={4}>
|
|
178
|
+
{hiddenValues.map((hidden, i) => (
|
|
179
|
+
<span key={hidden}>
|
|
180
|
+
{labelsByValue.get(hidden) ?? hidden}
|
|
181
|
+
{i < hiddenValues.length - 1 ? ',' : ''}
|
|
182
|
+
</span>
|
|
183
|
+
))}
|
|
184
|
+
</Inline>
|
|
185
|
+
}
|
|
186
|
+
>
|
|
187
|
+
<Pill size={size} disabled={disabled}>
|
|
188
|
+
+{hiddenValues.length} more
|
|
189
|
+
</Pill>
|
|
190
|
+
</Tooltip>
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return null;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const renderCheckboxOption: MantineMultiSelectProps['renderOption'] = ({ option, checked }) => (
|
|
198
|
+
<Inline gap="sm" wrap="nowrap">
|
|
199
|
+
<Checkbox
|
|
200
|
+
checked={!!checked}
|
|
201
|
+
disabled={option.disabled}
|
|
202
|
+
onChange={() => {}} // Selection is handled by the option click
|
|
203
|
+
aria-hidden
|
|
204
|
+
tabIndex={-1}
|
|
205
|
+
style={{ pointerEvents: 'none' }}
|
|
206
|
+
/>
|
|
207
|
+
<span>{option.label}</span>
|
|
208
|
+
</Inline>
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<MantineMultiSelect
|
|
213
|
+
{...props}
|
|
214
|
+
ref={ref}
|
|
215
|
+
size={size}
|
|
216
|
+
radius="sm"
|
|
217
|
+
variant={variant ?? 'default'}
|
|
218
|
+
{...labelProps}
|
|
219
|
+
required={required}
|
|
220
|
+
data={data}
|
|
221
|
+
// Core keeps the placeholder visible next to the pills; the DS shows it only when empty.
|
|
222
|
+
placeholder={selected.length > 0 ? undefined : placeholder}
|
|
223
|
+
value={selected}
|
|
224
|
+
onChange={setSelected}
|
|
225
|
+
readOnly={readOnly}
|
|
226
|
+
withCheckIcon={withCheckIcon}
|
|
227
|
+
renderOption={renderOption ?? renderCheckboxOption}
|
|
228
|
+
renderPill={renderPill}
|
|
229
|
+
rightSection={<IconSelector size={14} aria-hidden />}
|
|
230
|
+
rightSectionPointerEvents={props.clearable ? 'auto' : 'none'}
|
|
231
|
+
error={effectiveError}
|
|
232
|
+
/>
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
ADMultiselect.displayName = 'ADMultiselect';
|
|
@@ -1,36 +1,34 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}), rightSection: value !== '' && !disabled && (_jsx(CloseButton, { size: "sm", onMouseDown: (event) => event.preventDefault(), onClick: () => setValue(''), "aria-label": "Clear value" })) }) }), _jsx(Combobox.Dropdown, { children: _jsx(Combobox.Options, { children: options.length === 0 ? _jsx(Combobox.Empty, { children: "Nothing found" }) : options }) })] }) }));
|
|
36
|
-
}
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import React, { forwardRef, useMemo } from 'react';
|
|
4
|
+
import { Autocomplete as MantineAutocomplete } from '@mantine/core';
|
|
5
|
+
import { Text } from '../Typography/Text.js';
|
|
6
|
+
// ========================== COMPONENT ==========================
|
|
7
|
+
/**
|
|
8
|
+
* AppDirect Design System AutocompleteClearable
|
|
9
|
+
*
|
|
10
|
+
* Mantine's `Autocomplete` with the clear button on by default and DS sizing, radius
|
|
11
|
+
* and "(Optional)" label. Filtering, controlled and uncontrolled value, keyboard
|
|
12
|
+
* handling and every other `Autocomplete` prop are Mantine core behavior.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* <AutocompleteClearable
|
|
17
|
+
* label="Country"
|
|
18
|
+
* placeholder="Pick a country or type anything"
|
|
19
|
+
* data={['United States', 'Canada', 'Germany']}
|
|
20
|
+
* />
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export const AutocompleteClearable = forwardRef(({ size = 'md', clearable = true, showOptional = false, required, label, ...props }, ref) => {
|
|
24
|
+
const enhancedLabel = useMemo(() => {
|
|
25
|
+
if (!label)
|
|
26
|
+
return label;
|
|
27
|
+
if (showOptional && !required) {
|
|
28
|
+
return (_jsxs(_Fragment, { children: [label, " ", _jsx(Text, { span: true, c: "dimmed", fw: "normal", children: "(Optional)" })] }));
|
|
29
|
+
}
|
|
30
|
+
return label;
|
|
31
|
+
}, [label, showOptional, required]);
|
|
32
|
+
return (_jsx(MantineAutocomplete, { ref: ref, size: size, radius: "sm", clearable: clearable, required: required, label: enhancedLabel, ...props }));
|
|
33
|
+
});
|
|
34
|
+
AutocompleteClearable.displayName = 'AutocompleteClearable';
|
|
@@ -1,128 +1,63 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import React, { forwardRef, useMemo } from 'react';
|
|
4
|
+
import { Autocomplete as MantineAutocomplete, AutocompleteProps as MantineAutocompleteProps } from '@mantine/core';
|
|
5
|
+
import { ComponentSize } from '../config';
|
|
6
|
+
import { Text } from '../Typography/Text';
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
'United States',
|
|
8
|
-
'Canada',
|
|
9
|
-
'United Kingdom',
|
|
10
|
-
'Germany',
|
|
11
|
-
'France',
|
|
12
|
-
'Australia',
|
|
13
|
-
'Japan',
|
|
14
|
-
'Singapore',
|
|
15
|
-
];
|
|
8
|
+
// ========================== TYPES ==========================
|
|
16
9
|
|
|
17
|
-
export interface DSAutocompleteClearableProps {
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
placeholder?: string;
|
|
22
|
-
/** Data array for options - can be strings or objects with value/label */
|
|
23
|
-
data?: string[] | { value: string; label: string }[];
|
|
24
|
-
/** Description text displayed below the input */
|
|
25
|
-
description?: string;
|
|
26
|
-
/** Error message */
|
|
27
|
-
error?: string;
|
|
28
|
-
/** Whether the field is required */
|
|
29
|
-
required?: boolean;
|
|
30
|
-
/** Whether to show "(optional)" next to the label */
|
|
10
|
+
export interface DSAutocompleteClearableProps extends Omit<MantineAutocompleteProps, 'size' | 'radius'> {
|
|
11
|
+
/** Input size from design system scale */
|
|
12
|
+
size?: ComponentSize;
|
|
13
|
+
/** Whether to show "(Optional)" text after the label */
|
|
31
14
|
showOptional?: boolean;
|
|
32
|
-
/** Whether the input is disabled */
|
|
33
|
-
disabled?: boolean;
|
|
34
|
-
/** Input size */
|
|
35
|
-
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
36
|
-
/** Style object for the component */
|
|
37
|
-
style?: React.CSSProperties;
|
|
38
15
|
}
|
|
39
16
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
label={label}
|
|
88
|
-
description={description}
|
|
89
|
-
error={error}
|
|
90
|
-
required={required}
|
|
91
|
-
size={size}
|
|
92
|
-
disabled={disabled}
|
|
93
|
-
placeholder={placeholder}
|
|
94
|
-
value={value}
|
|
95
|
-
onChange={(event) => {
|
|
96
|
-
setValue(event.currentTarget.value);
|
|
97
|
-
combobox.openDropdown();
|
|
98
|
-
combobox.updateSelectedOptionIndex();
|
|
99
|
-
}}
|
|
100
|
-
onClick={() => combobox.openDropdown()}
|
|
101
|
-
onFocus={() => combobox.openDropdown()}
|
|
102
|
-
onBlur={() => combobox.closeDropdown()}
|
|
103
|
-
withAsterisk={required && !showOptional}
|
|
104
|
-
{...(showOptional && !required && {
|
|
105
|
-
label: label ? `${label} (optional)` : undefined
|
|
106
|
-
})}
|
|
107
|
-
rightSection={
|
|
108
|
-
value !== '' && !disabled && (
|
|
109
|
-
<CloseButton
|
|
110
|
-
size="sm"
|
|
111
|
-
onMouseDown={(event) => event.preventDefault()}
|
|
112
|
-
onClick={() => setValue('')}
|
|
113
|
-
aria-label="Clear value"
|
|
114
|
-
/>
|
|
115
|
-
)
|
|
116
|
-
}
|
|
117
|
-
/>
|
|
118
|
-
</Combobox.Target>
|
|
119
|
-
|
|
120
|
-
<Combobox.Dropdown>
|
|
121
|
-
<Combobox.Options>
|
|
122
|
-
{options.length === 0 ? <Combobox.Empty>Nothing found</Combobox.Empty> : options}
|
|
123
|
-
</Combobox.Options>
|
|
124
|
-
</Combobox.Dropdown>
|
|
125
|
-
</Combobox>
|
|
126
|
-
</div>
|
|
127
|
-
);
|
|
128
|
-
}
|
|
17
|
+
// ========================== COMPONENT ==========================
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* AppDirect Design System AutocompleteClearable
|
|
21
|
+
*
|
|
22
|
+
* Mantine's `Autocomplete` with the clear button on by default and DS sizing, radius
|
|
23
|
+
* and "(Optional)" label. Filtering, controlled and uncontrolled value, keyboard
|
|
24
|
+
* handling and every other `Autocomplete` prop are Mantine core behavior.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* <AutocompleteClearable
|
|
29
|
+
* label="Country"
|
|
30
|
+
* placeholder="Pick a country or type anything"
|
|
31
|
+
* data={['United States', 'Canada', 'Germany']}
|
|
32
|
+
* />
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export const AutocompleteClearable = forwardRef<HTMLInputElement, DSAutocompleteClearableProps>(
|
|
36
|
+
({ size = 'md', clearable = true, showOptional = false, required, label, ...props }, ref) => {
|
|
37
|
+
const enhancedLabel = useMemo(() => {
|
|
38
|
+
if (!label) return label;
|
|
39
|
+
if (showOptional && !required) {
|
|
40
|
+
return (
|
|
41
|
+
<>
|
|
42
|
+
{label} <Text span c="dimmed" fw="normal">(Optional)</Text>
|
|
43
|
+
</>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return label;
|
|
47
|
+
}, [label, showOptional, required]);
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<MantineAutocomplete
|
|
51
|
+
ref={ref}
|
|
52
|
+
size={size}
|
|
53
|
+
radius="sm"
|
|
54
|
+
clearable={clearable}
|
|
55
|
+
required={required}
|
|
56
|
+
label={enhancedLabel}
|
|
57
|
+
{...props}
|
|
58
|
+
/>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
AutocompleteClearable.displayName = 'AutocompleteClearable';
|
|
@@ -1,43 +1,24 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx as _jsx
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
? normalizedData.filter((item) => item.label.toLowerCase().includes(search.toLowerCase().trim()) ||
|
|
26
|
-
item.value.toLowerCase().includes(search.toLowerCase().trim()))
|
|
27
|
-
: normalizedData;
|
|
28
|
-
const options = filteredOptions.map((item) => (_jsx(Combobox.Option, { value: item.value, children: item.label }, item.value)));
|
|
29
|
-
return (_jsx("div", { style: style, children: _jsxs(Combobox, { store: combobox, withinPortal: false, onOptionSubmit: (val) => {
|
|
30
|
-
setValue(val);
|
|
31
|
-
setSearch(val);
|
|
32
|
-
combobox.closeDropdown();
|
|
33
|
-
}, children: [_jsx(Combobox.Target, { children: _jsx(InputBase, { label: label, description: description, error: error, required: required, size: size, disabled: disabled, rightSection: _jsx(Combobox.Chevron, {}), value: search, onChange: (event) => {
|
|
34
|
-
combobox.openDropdown();
|
|
35
|
-
combobox.updateSelectedOptionIndex();
|
|
36
|
-
setSearch(event.currentTarget.value);
|
|
37
|
-
}, onClick: () => combobox.openDropdown(), onFocus: () => combobox.openDropdown(), onBlur: () => {
|
|
38
|
-
combobox.closeDropdown();
|
|
39
|
-
setSearch(value || '');
|
|
40
|
-
}, placeholder: placeholder, rightSectionPointerEvents: "none", withAsterisk: required && !showOptional, ...(showOptional && !required && {
|
|
41
|
-
label: label ? `${label} (optional)` : undefined
|
|
42
|
-
}) }) }), _jsx(Combobox.Dropdown, { children: _jsx(Combobox.Options, { children: options.length > 0 ? options : _jsx(Combobox.Empty, { children: "Nothing found" }) }) })] }) }));
|
|
43
|
-
}
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import React, { forwardRef } from 'react';
|
|
4
|
+
import { Select } from './Select.js';
|
|
5
|
+
// ========================== COMPONENT ==========================
|
|
6
|
+
/**
|
|
7
|
+
* AppDirect Design System SearchableSelect
|
|
8
|
+
*
|
|
9
|
+
* The DS `Select` (Mantine's `Select`) with `searchable` on by default, so the
|
|
10
|
+
* field is typeable and filters its options. Search, filtering, controlled and
|
|
11
|
+
* uncontrolled value, clearable, grouping and every other `Select` prop are Mantine
|
|
12
|
+
* core behavior.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* <SearchableSelect
|
|
17
|
+
* label="Language"
|
|
18
|
+
* placeholder="Search languages"
|
|
19
|
+
* data={['JavaScript', 'TypeScript', 'Python']}
|
|
20
|
+
* />
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export const SearchableSelect = forwardRef(({ searchable = true, size = 'md', ...props }, ref) => (_jsx(Select, { ref: ref, searchable: searchable, size: size, ...props })));
|
|
24
|
+
SearchableSelect.displayName = 'SearchableSelect';
|