@soyfri/shared-library 2.0.0-beta.2 → 2.0.0-beta.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.
- package/.dockerignore +8 -0
- package/.github/workflows/publish.yml +107 -0
- package/.prettierrc +3 -0
- package/.storybook/main.ts +19 -0
- package/.storybook/preview.ts +14 -0
- package/.storybook/vitest.setup.ts +9 -0
- package/Dockerfile +37 -0
- package/build.js +102 -0
- package/chromatic.config.json +5 -0
- package/cleanDirectories.js +40 -0
- package/dist/README.md +243 -0
- package/dist/components/Icon/Icon.js +1 -1
- package/dist/components/Table/Table.js +1 -1
- package/dist/index.cjs +24 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +7 -1
- package/dist/mui.d.ts +1 -0
- package/dist/package.json +197 -0
- package/package.json +4 -32
- package/rollup.config.cjs +87 -0
- package/src/components/ActionMenu/ActionMenu.stories.tsx +230 -0
- package/src/components/ActionMenu/ActionMenu.tsx +174 -0
- package/src/components/ActionMenu/index.ts +2 -0
- package/src/components/AppBar/AppBar.stories.tsx +272 -0
- package/src/components/AppBar/AppBar.sx.ts +32 -0
- package/src/components/AppBar/AppBar.tsx +123 -0
- package/src/components/AppBar/AppBarBrand.tsx +120 -0
- package/src/components/AppBar/AppBarContext.ts +25 -0
- package/src/components/AppBar/AppBarMenuToggle.tsx +90 -0
- package/src/components/AppBar/AppBarUserMenu.tsx +217 -0
- package/src/components/AppBar/index.ts +25 -0
- package/src/components/Autocomplete/Autocomplete.definitions.ts +477 -0
- package/src/components/Autocomplete/Autocomplete.helpers.ts +60 -0
- package/src/components/Autocomplete/Autocomplete.stories.tsx +748 -0
- package/src/components/Autocomplete/Autocomplete.sx.ts +30 -0
- package/src/components/Autocomplete/Autocomplete.tsx +361 -0
- package/src/components/Autocomplete/Autocomplete.types.ts +13 -0
- package/src/components/Autocomplete/_parts/AutocompleteChips.tsx +55 -0
- package/src/components/Autocomplete/_parts/AutocompleteLoader.tsx +17 -0
- package/src/components/Autocomplete/_parts/AutocompleteOption.tsx +31 -0
- package/src/components/Autocomplete/index.ts +12 -0
- package/src/components/Avatar/Avatar.definitions.ts +162 -0
- package/src/components/Avatar/Avatar.stories.tsx +258 -0
- package/src/components/Avatar/Avatar.tsx +206 -0
- package/src/components/Avatar/index.ts +1 -0
- package/src/components/Button/Button.definition.ts +97 -0
- package/src/components/Button/Button.stories.tsx +285 -0
- package/src/components/Button/Button.tsx +67 -0
- package/src/components/Button/index.ts +1 -0
- package/src/components/Card/Card.definition.ts +5 -0
- package/src/components/Card/Card.stories.tsx +221 -0
- package/src/components/Card/Card.sx.ts +104 -0
- package/src/components/Card/Card.tsx +200 -0
- package/src/components/Card/index.ts +9 -0
- package/src/components/Chip/Chip.definitions.ts +167 -0
- package/src/components/Chip/Chip.stories.tsx +265 -0
- package/src/components/Chip/Chip.tsx +61 -0
- package/src/components/Chip/index.ts +1 -0
- package/src/components/Column/Column.tsx +29 -0
- package/src/components/Column/index.ts +1 -0
- package/src/components/DatePicker/DatePicker.definitions.ts +228 -0
- package/src/components/DatePicker/DatePicker.helpers.ts +24 -0
- package/src/components/DatePicker/DatePicker.stories.tsx +309 -0
- package/src/components/DatePicker/DatePicker.sx.ts +33 -0
- package/src/components/DatePicker/DatePicker.tsx +189 -0
- package/src/components/DatePicker/DatePicker.types.ts +10 -0
- package/src/components/DatePicker/index.ts +9 -0
- package/src/components/DateRangePicker/DateRangePicker.definitions.ts +191 -0
- package/src/components/DateRangePicker/DateRangePicker.stories.tsx +252 -0
- package/src/components/DateRangePicker/DateRangePicker.tsx +56 -0
- package/src/components/DateRangePicker/index.ts +1 -0
- package/src/components/DateTimePicker/DateTimePicker.definitions.ts +256 -0
- package/src/components/DateTimePicker/DateTimePicker.helpers.ts +38 -0
- package/src/components/DateTimePicker/DateTimePicker.stories.tsx +418 -0
- package/src/components/DateTimePicker/DateTimePicker.sx.ts +30 -0
- package/src/components/DateTimePicker/DateTimePicker.tsx +225 -0
- package/src/components/DateTimePicker/DateTimePicker.types.ts +10 -0
- package/src/components/DateTimePicker/index.ts +9 -0
- package/src/components/Drawer/Drawer.stories.tsx +270 -0
- package/src/components/Drawer/Drawer.sx.ts +106 -0
- package/src/components/Drawer/Drawer.tsx +214 -0
- package/src/components/Drawer/DrawerContext.ts +26 -0
- package/src/components/Drawer/DrawerItem.tsx +110 -0
- package/src/components/Drawer/index.ts +10 -0
- package/src/components/Flyout/Flyout.stories.tsx +282 -0
- package/src/components/Flyout/Flyout.tsx +122 -0
- package/src/components/Flyout/index.ts +1 -0
- package/src/components/Gallery/Gallery.definition.tsx +37 -0
- package/src/components/Gallery/Gallery.stories.tsx +82 -0
- package/src/components/Gallery/Gallery.tsx +118 -0
- package/src/components/Gallery/GalleryLightbox.tsx +170 -0
- package/src/components/Gallery/GalleryMain.tsx +84 -0
- package/src/components/Gallery/GalleryThumbnails.tsx +106 -0
- package/src/components/Gallery/index.ts +1 -0
- package/src/components/Icon/Icon.stories.tsx +121 -0
- package/src/components/Icon/Icon.tsx +175 -0
- package/src/components/Icon/index.ts +2 -0
- package/src/components/Input/Input.definitions.ts +324 -0
- package/src/components/Input/Input.helpers.ts +49 -0
- package/src/components/Input/Input.stories.tsx +499 -0
- package/src/components/Input/Input.sx.ts +42 -0
- package/src/components/Input/Input.tsx +141 -0
- package/src/components/Input/Input.types.ts +10 -0
- package/src/components/Input/index.ts +9 -0
- package/src/components/InputGroup/InputGroup.definitions.ts +158 -0
- package/src/components/InputGroup/InputGroup.stories.tsx +267 -0
- package/src/components/InputGroup/InputGroup.tsx +179 -0
- package/src/components/InputGroup/index.ts +1 -0
- package/src/components/MenuButton/MenuButton.stories.tsx +197 -0
- package/src/components/MenuButton/MenuButton.tsx +100 -0
- package/src/components/MenuButton/index.ts +1 -0
- package/src/components/Modal/Modal.stories.tsx +721 -0
- package/src/components/Modal/Modal.tsx +355 -0
- package/src/components/Modal/ModalBody.tsx +16 -0
- package/src/components/Modal/ModalFooter.tsx +71 -0
- package/src/components/Modal/ModalHeader.tsx +18 -0
- package/src/components/Modal/index.ts +6 -0
- package/src/components/PageLoader/PageLoader.stories.tsx +217 -0
- package/src/components/PageLoader/PageLoader.tsx +96 -0
- package/src/components/PageLoader/index.ts +2 -0
- package/src/components/ScrollTopButton/ScrollTopButton.stories.tsx +158 -0
- package/src/components/ScrollTopButton/ScrollTopButton.tsx +135 -0
- package/src/components/ScrollTopButton/index.ts +8 -0
- package/src/components/ScrollTopButton/scrollToTop.ts +37 -0
- package/src/components/Select/Select.definitions.ts +602 -0
- package/src/components/Select/Select.helpers.ts +71 -0
- package/src/components/Select/Select.stories.tsx +687 -0
- package/src/components/Select/Select.sx.ts +14 -0
- package/src/components/Select/Select.tsx +429 -0
- package/src/components/Select/Select.types.ts +15 -0
- package/src/components/Select/_parts/SelectMenuItem.tsx +40 -0
- package/src/components/Select/_parts/SelectSearchHeader.tsx +51 -0
- package/src/components/Select/_parts/SelectValue.tsx +96 -0
- package/src/components/Select/index.ts +14 -0
- package/src/components/Stat/Stat.stories.tsx +85 -0
- package/src/components/Stat/Stat.tsx +117 -0
- package/src/components/Stat/index.ts +2 -0
- package/src/components/StatusMessage/StatusMessage.stories.tsx +130 -0
- package/src/components/StatusMessage/StatusMessage.tsx +162 -0
- package/src/components/StatusMessage/index.ts +2 -0
- package/src/components/Stepper/Step.tsx +21 -0
- package/src/components/Stepper/Stepper.definition.ts +75 -0
- package/src/components/Stepper/Stepper.stories.tsx +122 -0
- package/src/components/Stepper/Stepper.tsx +75 -0
- package/src/components/Stepper/index.ts +2 -0
- package/src/components/Table/EmptyTable.png +0 -0
- package/src/components/Table/Table.definition.ts +580 -0
- package/src/components/Table/Table.stories.tsx +853 -0
- package/src/components/Table/Table.tsx +495 -0
- package/src/components/Table/data.ts +134 -0
- package/src/components/Table/exportsUtils.ts +195 -0
- package/src/components/Table/index.ts +3 -0
- package/src/components/Table/types.ts +34 -0
- package/src/components/Tabs/Tab.definition.ts +53 -0
- package/src/components/Tabs/Tab.tsx +19 -0
- package/src/components/Tabs/Tabs.stories.tsx +118 -0
- package/src/components/Tabs/Tabs.tsx +99 -0
- package/src/components/Tabs/_tabUtils.tsx +4 -0
- package/src/components/Tabs/index.ts +2 -0
- package/src/components/Timeline/Timeline.definition.ts +43 -0
- package/src/components/Timeline/Timeline.stories.tsx +108 -0
- package/src/components/Timeline/Timeline.tsx +49 -0
- package/src/components/Timeline/TimelineItem.tsx +31 -0
- package/src/components/Timeline/index.ts +2 -0
- package/src/components/Tooltip/Tooltip.stories.tsx +129 -0
- package/src/components/Tooltip/Tooltip.tsx +58 -0
- package/src/components/Tooltip/index.ts +1 -0
- package/src/components/_shared/formField.sx.ts +118 -0
- package/src/components/_shared/resolvePreset.ts +35 -0
- package/src/hooks/ClipBoard/ClipBoard.stories.tsx +168 -0
- package/src/hooks/ClipBoard/ClipBoard.tsx +131 -0
- package/src/hooks/ClipBoard/ClipboardUnifiedDemo.tsx +111 -0
- package/src/hooks/ClipBoard/index.ts +1 -0
- package/src/hooks/Wizard/Wizard.stories.tsx +301 -0
- package/src/hooks/Wizard/WizardContext.tsx +166 -0
- package/src/hooks/Wizard/index.ts +6 -0
- package/src/hooks/Wizard/useWizard.ts +13 -0
- package/src/index.ts +17 -0
- package/src/mui.ts +54 -0
- package/src/styles.css +3 -0
- package/src/theme/componentStyles.ts +47 -0
- package/src/theme/tokens.ts +43 -0
- package/tailwind.config.js +10 -0
- package/tsconfig.json +48 -0
- package/tsup.config.js +41 -0
- package/vite.config.js +132 -0
- package/vitest.config.ts +35 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { SxProps, Theme } from '@mui/material/styles';
|
|
2
|
+
|
|
3
|
+
import { buildFormFieldSx } from '../_shared/formField.sx';
|
|
4
|
+
import type { LabelPosition } from './Autocomplete';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Builder del sx del Autocomplete. Reutiliza `buildFormFieldSx` y añade
|
|
8
|
+
* los paddings propios del Autocomplete (chips + input interno).
|
|
9
|
+
*/
|
|
10
|
+
export const buildAutocompleteSx = (
|
|
11
|
+
borderRadius: number | string,
|
|
12
|
+
labelPosition: LabelPosition,
|
|
13
|
+
): SxProps<Theme> =>
|
|
14
|
+
buildFormFieldSx({
|
|
15
|
+
borderRadius,
|
|
16
|
+
labelPosition,
|
|
17
|
+
extraOutsideSx: {
|
|
18
|
+
// El input interno del Autocomplete.
|
|
19
|
+
'& .MuiAutocomplete-input': {
|
|
20
|
+
paddingTop: '4px',
|
|
21
|
+
paddingBottom: '4px',
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
// Contenedor del input (donde viven los chips en modo multiple).
|
|
25
|
+
'& .MuiAutocomplete-inputRoot': {
|
|
26
|
+
paddingTop: '4px',
|
|
27
|
+
paddingBottom: '4px',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useMemo,
|
|
3
|
+
useRef,
|
|
4
|
+
useState,
|
|
5
|
+
type ReactNode,
|
|
6
|
+
type SyntheticEvent,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import {
|
|
9
|
+
Autocomplete as MuiAutocomplete,
|
|
10
|
+
TextField,
|
|
11
|
+
type TextFieldProps,
|
|
12
|
+
} from '@mui/material';
|
|
13
|
+
import { useTheme, type SxProps, type Theme } from '@mui/material/styles';
|
|
14
|
+
import { Controller, type Control, type RegisterOptions } from 'react-hook-form';
|
|
15
|
+
|
|
16
|
+
import { buildAutocompleteSx } from './Autocomplete.sx';
|
|
17
|
+
import {
|
|
18
|
+
areResolvedValuesEqual,
|
|
19
|
+
isResolvedValueEmpty,
|
|
20
|
+
resolveMultipleValue,
|
|
21
|
+
resolveSingleValue,
|
|
22
|
+
} from './Autocomplete.helpers';
|
|
23
|
+
import { resolvePreset } from '../_shared/resolvePreset';
|
|
24
|
+
import { AutocompleteOption } from './_parts/AutocompleteOption';
|
|
25
|
+
import { AutocompleteChips } from './_parts/AutocompleteChips';
|
|
26
|
+
import { AutocompleteLoader } from './_parts/AutocompleteLoader';
|
|
27
|
+
|
|
28
|
+
// ── Tipos de dominio ─────────────────────────────────────────────────────
|
|
29
|
+
export interface SelectOption {
|
|
30
|
+
value: string | number;
|
|
31
|
+
label: string;
|
|
32
|
+
img?: string;
|
|
33
|
+
disabled?: boolean;
|
|
34
|
+
group?: string;
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type LabelPosition = 'outside' | 'floating';
|
|
39
|
+
export type AutocompleteSize = 'small' | 'medium';
|
|
40
|
+
|
|
41
|
+
// ── Render slots ─────────────────────────────────────────────────────────
|
|
42
|
+
export type RenderOptionItem = (item: SelectOption) => ReactNode;
|
|
43
|
+
export type RenderChipLabel = (item: SelectOption) => ReactNode;
|
|
44
|
+
|
|
45
|
+
// ── Props base ───────────────────────────────────────────────────────────
|
|
46
|
+
export type BaseAutocompleteProps<
|
|
47
|
+
_TValue extends SelectOption['value'] = SelectOption['value'],
|
|
48
|
+
> = {
|
|
49
|
+
label?: string;
|
|
50
|
+
options: SelectOption[];
|
|
51
|
+
placeholder?: string;
|
|
52
|
+
multiple?: boolean;
|
|
53
|
+
disabled?: boolean;
|
|
54
|
+
readOnly?: boolean;
|
|
55
|
+
loading?: boolean;
|
|
56
|
+
error?: boolean;
|
|
57
|
+
helperText?: string;
|
|
58
|
+
maxChipsToShow?: number;
|
|
59
|
+
renderOptionItem?: RenderOptionItem;
|
|
60
|
+
renderChipLabel?: RenderChipLabel;
|
|
61
|
+
/** Border radius del input. Default: 10. */
|
|
62
|
+
borderRadius?: number | string;
|
|
63
|
+
/** "outside" (default) = label arriba del input; "floating" = comportamiento nativo MUI. */
|
|
64
|
+
labelPosition?: LabelPosition;
|
|
65
|
+
/** Tamaño del TextField. Default: 'small'. */
|
|
66
|
+
size?: AutocompleteSize;
|
|
67
|
+
/** Texto cuando no hay coincidencias. */
|
|
68
|
+
noOptionsText?: ReactNode;
|
|
69
|
+
/** Texto cuando está cargando. */
|
|
70
|
+
loadingText?: ReactNode;
|
|
71
|
+
sx?: SxProps<Theme>;
|
|
72
|
+
className?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Nombre del preset de estilo registrado en `theme.styles.Autocomplete`.
|
|
75
|
+
* - `"default"` (o ausente) = estilo built-in del paquete.
|
|
76
|
+
* - Cualquier otro string = mergea el preset encima del estilo built-in.
|
|
77
|
+
*/
|
|
78
|
+
preset?: string;
|
|
79
|
+
/** Props para el TextField subyacente. */
|
|
80
|
+
textFieldProps?: Partial<TextFieldProps>;
|
|
81
|
+
|
|
82
|
+
// ── Búsqueda asíncrona / remota ──────────────────────────────────────
|
|
83
|
+
/** Se dispara cada vez que el usuario escribe. Útil para llamadas a servicios
|
|
84
|
+
* (debounce recomendado en el consumer). */
|
|
85
|
+
onInputChange?: (
|
|
86
|
+
event: SyntheticEvent,
|
|
87
|
+
value: string,
|
|
88
|
+
reason: 'input' | 'reset' | 'clear' | 'blur' | 'selectOption' | 'removeOption',
|
|
89
|
+
) => void;
|
|
90
|
+
/** Valor controlado del texto de búsqueda. */
|
|
91
|
+
inputValue?: string;
|
|
92
|
+
/** Filtro custom. Para búsqueda remota pasar `(x) => x` y confiar sólo en el servicio. */
|
|
93
|
+
filterOptions?: (options: SelectOption[], state: any) => SelectOption[];
|
|
94
|
+
/** Permite valores arbitrarios (no solo de la lista). */
|
|
95
|
+
freeSolo?: boolean;
|
|
96
|
+
open?: boolean;
|
|
97
|
+
onOpen?: (event: SyntheticEvent) => void;
|
|
98
|
+
onClose?: (event: SyntheticEvent, reason: string) => void;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// ── Variantes discriminadas (RHF vs controlado) ──────────────────────────
|
|
102
|
+
export interface ControlledAutocompleteProps<
|
|
103
|
+
TValue extends SelectOption['value'] = SelectOption['value'],
|
|
104
|
+
> extends BaseAutocompleteProps<TValue> {
|
|
105
|
+
value: TValue | TValue[] | null;
|
|
106
|
+
onChange: (val: TValue | TValue[] | null) => void;
|
|
107
|
+
name?: string;
|
|
108
|
+
control?: never;
|
|
109
|
+
validation?: never;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface RHFAutocompleteProps<
|
|
113
|
+
TValue extends SelectOption['value'] = SelectOption['value'],
|
|
114
|
+
> extends BaseAutocompleteProps<TValue> {
|
|
115
|
+
name: string;
|
|
116
|
+
control: Control<any>;
|
|
117
|
+
validation?: RegisterOptions;
|
|
118
|
+
value?: never;
|
|
119
|
+
onChange?: never;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ── API pública final ────────────────────────────────────────────────────
|
|
123
|
+
export type AutocompleteProps<
|
|
124
|
+
TValue extends SelectOption['value'] = SelectOption['value'],
|
|
125
|
+
> = ControlledAutocompleteProps<TValue> | RHFAutocompleteProps<TValue>;
|
|
126
|
+
|
|
127
|
+
export function Autocomplete<
|
|
128
|
+
TValue extends SelectOption['value'] = SelectOption['value'],
|
|
129
|
+
>(props: AutocompleteProps<TValue>) {
|
|
130
|
+
const {
|
|
131
|
+
label,
|
|
132
|
+
options,
|
|
133
|
+
placeholder,
|
|
134
|
+
multiple = false,
|
|
135
|
+
disabled = false,
|
|
136
|
+
readOnly = false,
|
|
137
|
+
loading = false,
|
|
138
|
+
error: errorProp = false,
|
|
139
|
+
helperText,
|
|
140
|
+
maxChipsToShow = 3,
|
|
141
|
+
renderOptionItem,
|
|
142
|
+
renderChipLabel,
|
|
143
|
+
borderRadius = 10,
|
|
144
|
+
labelPosition = 'outside',
|
|
145
|
+
size = 'small',
|
|
146
|
+
noOptionsText = 'No hay opciones',
|
|
147
|
+
loadingText,
|
|
148
|
+
sx,
|
|
149
|
+
className,
|
|
150
|
+
preset,
|
|
151
|
+
textFieldProps,
|
|
152
|
+
onInputChange,
|
|
153
|
+
inputValue,
|
|
154
|
+
filterOptions,
|
|
155
|
+
freeSolo,
|
|
156
|
+
open,
|
|
157
|
+
onOpen,
|
|
158
|
+
onClose,
|
|
159
|
+
} = props as BaseAutocompleteProps<TValue>;
|
|
160
|
+
|
|
161
|
+
// Focus tracking para mostrar placeholder solo cuando el label sube.
|
|
162
|
+
const [isFocused, setIsFocused] = useState(false);
|
|
163
|
+
|
|
164
|
+
// Cache interno de opciones vistas (indexado por value). Crítico para async
|
|
165
|
+
// multiple: cuando `options` cambian por un nuevo search, los items ya
|
|
166
|
+
// seleccionados no desaparecen de los chips porque los recordamos acá.
|
|
167
|
+
const seenOptionsRef = useRef<Map<SelectOption['value'], SelectOption>>(
|
|
168
|
+
new Map(),
|
|
169
|
+
);
|
|
170
|
+
for (const opt of options) {
|
|
171
|
+
seenOptionsRef.current.set(opt.value, opt);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Pool de opciones para resolver values (merge de options + vistas).
|
|
175
|
+
const resolvePool = useMemo<SelectOption[]>(() => {
|
|
176
|
+
const merged = new Map(seenOptionsRef.current);
|
|
177
|
+
for (const opt of options) merged.set(opt.value, opt);
|
|
178
|
+
return Array.from(merged.values());
|
|
179
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
180
|
+
}, [options]);
|
|
181
|
+
|
|
182
|
+
// Estabiliza la referencia del resolvedValue que pasamos a MUI Autocomplete.
|
|
183
|
+
// MUI compara value con `!==`: una referencia nueva con mismo contenido
|
|
184
|
+
// dispara resetInputValue() y borra el texto que el usuario está escribiendo.
|
|
185
|
+
const prevResolvedRef = useRef<SelectOption[] | SelectOption | null>(
|
|
186
|
+
multiple ? [] : null,
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
const stabilizeResolved = (
|
|
190
|
+
newVal: SelectOption[] | SelectOption | null,
|
|
191
|
+
): any => {
|
|
192
|
+
if (areResolvedValuesEqual(prevResolvedRef.current, newVal, multiple)) {
|
|
193
|
+
return prevResolvedRef.current;
|
|
194
|
+
}
|
|
195
|
+
prevResolvedRef.current = newVal;
|
|
196
|
+
return newVal;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const theme = useTheme();
|
|
200
|
+
const presetSx = resolvePreset('Autocomplete', preset, theme);
|
|
201
|
+
|
|
202
|
+
const mergedSx = [
|
|
203
|
+
buildAutocompleteSx(borderRadius, labelPosition),
|
|
204
|
+
...(presetSx ? [presetSx] : []),
|
|
205
|
+
...(Array.isArray(sx) ? sx : sx ? [sx] : []),
|
|
206
|
+
];
|
|
207
|
+
|
|
208
|
+
const renderAutocomplete = (
|
|
209
|
+
resolvedValue: any,
|
|
210
|
+
handleChange: (event: any, newValue: any) => void,
|
|
211
|
+
onBlur?: () => void,
|
|
212
|
+
inputRef?: React.Ref<any>,
|
|
213
|
+
rhfError?: boolean,
|
|
214
|
+
rhfHelperText?: string,
|
|
215
|
+
) => {
|
|
216
|
+
const finalError = !!rhfError || !!errorProp;
|
|
217
|
+
const finalHelperText = rhfHelperText || helperText;
|
|
218
|
+
|
|
219
|
+
const isEmpty = isResolvedValueEmpty(resolvedValue, multiple);
|
|
220
|
+
const showPlaceholder = isEmpty && isFocused && !!placeholder;
|
|
221
|
+
|
|
222
|
+
return (
|
|
223
|
+
<MuiAutocomplete
|
|
224
|
+
multiple={multiple}
|
|
225
|
+
options={options}
|
|
226
|
+
value={resolvedValue}
|
|
227
|
+
disabled={disabled}
|
|
228
|
+
readOnly={readOnly}
|
|
229
|
+
loading={loading}
|
|
230
|
+
className={className}
|
|
231
|
+
sx={mergedSx}
|
|
232
|
+
freeSolo={freeSolo as any}
|
|
233
|
+
open={open}
|
|
234
|
+
onOpen={onOpen}
|
|
235
|
+
onClose={onClose}
|
|
236
|
+
inputValue={inputValue}
|
|
237
|
+
onInputChange={onInputChange}
|
|
238
|
+
filterOptions={filterOptions as any}
|
|
239
|
+
getOptionLabel={(opt) =>
|
|
240
|
+
typeof opt === 'string' ? opt : (opt?.label ?? '')
|
|
241
|
+
}
|
|
242
|
+
isOptionEqualToValue={(a, b) => a?.value === b?.value}
|
|
243
|
+
getOptionDisabled={(opt) => !!opt?.disabled}
|
|
244
|
+
onChange={handleChange}
|
|
245
|
+
onFocus={() => setIsFocused(true)}
|
|
246
|
+
onBlur={() => {
|
|
247
|
+
setIsFocused(false);
|
|
248
|
+
onBlur?.();
|
|
249
|
+
}}
|
|
250
|
+
renderOption={(liProps, option) => (
|
|
251
|
+
<AutocompleteOption
|
|
252
|
+
liProps={liProps as any}
|
|
253
|
+
option={option}
|
|
254
|
+
customRender={renderOptionItem}
|
|
255
|
+
/>
|
|
256
|
+
)}
|
|
257
|
+
renderTags={
|
|
258
|
+
multiple
|
|
259
|
+
? (value, getTagProps) => (
|
|
260
|
+
<AutocompleteChips
|
|
261
|
+
value={value}
|
|
262
|
+
getTagProps={getTagProps}
|
|
263
|
+
size={size}
|
|
264
|
+
maxChipsToShow={maxChipsToShow}
|
|
265
|
+
renderChipLabel={renderChipLabel}
|
|
266
|
+
/>
|
|
267
|
+
)
|
|
268
|
+
: undefined
|
|
269
|
+
}
|
|
270
|
+
noOptionsText={noOptionsText}
|
|
271
|
+
loadingText={loadingText ?? <AutocompleteLoader />}
|
|
272
|
+
renderInput={(params) => (
|
|
273
|
+
<TextField
|
|
274
|
+
{...params}
|
|
275
|
+
label={label}
|
|
276
|
+
size={size}
|
|
277
|
+
variant="outlined"
|
|
278
|
+
placeholder={showPlaceholder ? placeholder : undefined}
|
|
279
|
+
error={finalError}
|
|
280
|
+
helperText={finalHelperText}
|
|
281
|
+
inputRef={inputRef}
|
|
282
|
+
{...textFieldProps}
|
|
283
|
+
InputLabelProps={{
|
|
284
|
+
...(params.InputLabelProps as Record<string, any>),
|
|
285
|
+
...(textFieldProps?.InputLabelProps as Record<string, any> | undefined),
|
|
286
|
+
shrink:
|
|
287
|
+
labelPosition === 'outside'
|
|
288
|
+
? !isEmpty || isFocused
|
|
289
|
+
: (params.InputLabelProps as any)?.shrink,
|
|
290
|
+
}}
|
|
291
|
+
/>
|
|
292
|
+
)}
|
|
293
|
+
/>
|
|
294
|
+
);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const handleControlledChange = (_event: any, newValue: any) => {
|
|
298
|
+
const onChange = (props as ControlledAutocompleteProps<TValue>).onChange;
|
|
299
|
+
if (multiple) {
|
|
300
|
+
const values = (newValue ?? []).map((opt: SelectOption) => opt.value as TValue);
|
|
301
|
+
onChange(values);
|
|
302
|
+
} else {
|
|
303
|
+
onChange((newValue?.value ?? null) as TValue | null);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// --- RHF mode ---
|
|
308
|
+
if ('control' in props && props.control) {
|
|
309
|
+
const { name, control, validation } = props as RHFAutocompleteProps<TValue>;
|
|
310
|
+
return (
|
|
311
|
+
<Controller
|
|
312
|
+
name={name}
|
|
313
|
+
control={control}
|
|
314
|
+
rules={validation}
|
|
315
|
+
render={({ field, fieldState: { error: fieldError } }) => {
|
|
316
|
+
const resolvedValue = stabilizeResolved(
|
|
317
|
+
multiple
|
|
318
|
+
? resolveMultipleValue(resolvePool, field.value)
|
|
319
|
+
: resolveSingleValue(resolvePool, field.value),
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
const handleChange = (_event: any, newValue: any) => {
|
|
323
|
+
if (multiple) {
|
|
324
|
+
field.onChange(
|
|
325
|
+
(newValue ?? []).map((opt: SelectOption) => opt.value),
|
|
326
|
+
);
|
|
327
|
+
} else {
|
|
328
|
+
field.onChange(newValue?.value ?? null);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
return renderAutocomplete(
|
|
333
|
+
resolvedValue,
|
|
334
|
+
handleChange,
|
|
335
|
+
field.onBlur,
|
|
336
|
+
field.ref,
|
|
337
|
+
!!fieldError,
|
|
338
|
+
fieldError?.message,
|
|
339
|
+
);
|
|
340
|
+
}}
|
|
341
|
+
/>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// --- Controlled mode ---
|
|
346
|
+
const controlledValue = (props as ControlledAutocompleteProps<TValue>).value;
|
|
347
|
+
const resolvedValue = stabilizeResolved(
|
|
348
|
+
multiple
|
|
349
|
+
? resolveMultipleValue(resolvePool, controlledValue as SelectOption['value'][])
|
|
350
|
+
: resolveSingleValue(resolvePool, controlledValue as SelectOption['value']),
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
return renderAutocomplete(resolvedValue, handleControlledChange);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @deprecated Usar `Autocomplete` en su lugar. Alias mantenido para retro-compatibilidad.
|
|
358
|
+
*/
|
|
359
|
+
export const AutocompleteSelect = Autocomplete;
|
|
360
|
+
|
|
361
|
+
export default Autocomplete;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Re-export barrel para compatibilidad con imports antiguos.
|
|
2
|
+
// Los tipos ahora viven dentro de Autocomplete.tsx.
|
|
3
|
+
export type {
|
|
4
|
+
SelectOption,
|
|
5
|
+
LabelPosition,
|
|
6
|
+
AutocompleteSize,
|
|
7
|
+
RenderOptionItem,
|
|
8
|
+
RenderChipLabel,
|
|
9
|
+
BaseAutocompleteProps,
|
|
10
|
+
ControlledAutocompleteProps,
|
|
11
|
+
RHFAutocompleteProps,
|
|
12
|
+
AutocompleteProps,
|
|
13
|
+
} from './Autocomplete';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Avatar, Chip } from '@mui/material';
|
|
3
|
+
import ClearIcon from '@mui/icons-material/Clear';
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
SelectOption,
|
|
7
|
+
RenderChipLabel,
|
|
8
|
+
AutocompleteSize,
|
|
9
|
+
} from '../Autocomplete';
|
|
10
|
+
|
|
11
|
+
interface AutocompleteChipsProps {
|
|
12
|
+
value: SelectOption[];
|
|
13
|
+
getTagProps: (args: { index: number }) => Record<string, any>;
|
|
14
|
+
size: AutocompleteSize;
|
|
15
|
+
maxChipsToShow: number;
|
|
16
|
+
renderChipLabel?: RenderChipLabel;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Render de los chips del Autocomplete en modo multiple.
|
|
21
|
+
* Trunca después de `maxChipsToShow` con un chip "+N más".
|
|
22
|
+
* El `getTagProps({ index })` viene de MUI y contiene el handler de delete,
|
|
23
|
+
* la key, etc. — se spreadea tal cual al Chip.
|
|
24
|
+
*/
|
|
25
|
+
export const AutocompleteChips: React.FC<AutocompleteChipsProps> = ({
|
|
26
|
+
value,
|
|
27
|
+
getTagProps,
|
|
28
|
+
size,
|
|
29
|
+
maxChipsToShow,
|
|
30
|
+
renderChipLabel,
|
|
31
|
+
}) => {
|
|
32
|
+
const displayed = value.slice(0, maxChipsToShow);
|
|
33
|
+
const hidden = value.length - maxChipsToShow;
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<>
|
|
37
|
+
{displayed.map((option, index) => {
|
|
38
|
+
const tagProps = getTagProps({ index });
|
|
39
|
+
return (
|
|
40
|
+
<Chip
|
|
41
|
+
{...tagProps}
|
|
42
|
+
key={option.value}
|
|
43
|
+
size={size}
|
|
44
|
+
label={renderChipLabel ? renderChipLabel(option) : option.label}
|
|
45
|
+
avatar={option.img ? <Avatar src={option.img} /> : undefined}
|
|
46
|
+
deleteIcon={<ClearIcon />}
|
|
47
|
+
/>
|
|
48
|
+
);
|
|
49
|
+
})}
|
|
50
|
+
{hidden > 0 && <Chip size={size} label={`+${hidden} más`} />}
|
|
51
|
+
</>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default AutocompleteChips;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Box, CircularProgress, Typography } from '@mui/material';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Loader por defecto para el `loadingText` del Autocomplete.
|
|
6
|
+
* El consumer puede sobrescribirlo pasando `loadingText` al componente padre.
|
|
7
|
+
*/
|
|
8
|
+
export const AutocompleteLoader: React.FC<{ text?: string }> = ({
|
|
9
|
+
text = 'Cargando...',
|
|
10
|
+
}) => (
|
|
11
|
+
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
|
|
12
|
+
<CircularProgress size={16} />
|
|
13
|
+
<Typography variant="body2">{text}</Typography>
|
|
14
|
+
</Box>
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
export default AutocompleteLoader;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { SelectOption, RenderOptionItem } from '../Autocomplete';
|
|
3
|
+
|
|
4
|
+
interface AutocompleteOptionProps {
|
|
5
|
+
liProps: React.HTMLAttributes<HTMLLIElement> & { key?: React.Key };
|
|
6
|
+
option: SelectOption;
|
|
7
|
+
customRender?: RenderOptionItem;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Render por defecto de un <li> del dropdown del Autocomplete.
|
|
12
|
+
* Se expone como componente para testear y extender fácilmente, pero MUI
|
|
13
|
+
* lo llama desde `renderOption={(props, option) => ...}` en el padre.
|
|
14
|
+
*
|
|
15
|
+
* El `key` lo manejamos nosotros (no spreadeamos junto al resto) porque MUI
|
|
16
|
+
* lo inyecta como parte de `propsLi` y React exige que sea prop directo.
|
|
17
|
+
*/
|
|
18
|
+
export const AutocompleteOption: React.FC<AutocompleteOptionProps> = ({
|
|
19
|
+
liProps,
|
|
20
|
+
option,
|
|
21
|
+
customRender,
|
|
22
|
+
}) => {
|
|
23
|
+
const { key: _ignored, ...liRest } = liProps;
|
|
24
|
+
return (
|
|
25
|
+
<li key={option.value} {...liRest}>
|
|
26
|
+
{customRender ? customRender(option) : option.label}
|
|
27
|
+
</li>
|
|
28
|
+
);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default AutocompleteOption;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { Autocomplete, AutocompleteSelect, default } from './Autocomplete';
|
|
2
|
+
export type {
|
|
3
|
+
AutocompleteProps,
|
|
4
|
+
SelectOption,
|
|
5
|
+
BaseAutocompleteProps,
|
|
6
|
+
ControlledAutocompleteProps,
|
|
7
|
+
RHFAutocompleteProps,
|
|
8
|
+
RenderOptionItem,
|
|
9
|
+
RenderChipLabel,
|
|
10
|
+
LabelPosition,
|
|
11
|
+
AutocompleteSize,
|
|
12
|
+
} from './Autocomplete.types';
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
export const DefaultAvatarDefinition = `
|
|
2
|
+
import { Avatar } from './Avatar';
|
|
3
|
+
|
|
4
|
+
export const DefaultAvatarExample = () => (
|
|
5
|
+
<Avatar
|
|
6
|
+
items={[
|
|
7
|
+
{ text: 'User One', imageUrl: 'https://i.pravatar.cc/150?img=1' },
|
|
8
|
+
{ text: 'User Two', imageUrl: 'https://i.pravatar.cc/150?img=2' },
|
|
9
|
+
]}
|
|
10
|
+
/>
|
|
11
|
+
);
|
|
12
|
+
`;
|
|
13
|
+
|
|
14
|
+
export const WithTypeAndTextDefinition = `
|
|
15
|
+
import { Avatar } from './Avatar';
|
|
16
|
+
|
|
17
|
+
export const WithTypeAndTextExample = () => (
|
|
18
|
+
<Avatar
|
|
19
|
+
type="Owner"
|
|
20
|
+
items={[{ text: 'Maria', imageUrl: 'https://i.pravatar.cc/150?img=5' }]}
|
|
21
|
+
showText
|
|
22
|
+
/>
|
|
23
|
+
);
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
export const WithBadgesAndTooltipDefinition = `
|
|
27
|
+
import { Avatar } from './Avatar';
|
|
28
|
+
|
|
29
|
+
export const WithBadgesAndTooltipExample = () => (
|
|
30
|
+
<Avatar
|
|
31
|
+
showTooltip
|
|
32
|
+
items={[
|
|
33
|
+
{ text: 'John D.', badge: 'JD', backgroundColor: '#EF5350', color: '#fff' },
|
|
34
|
+
{ text: 'Alice B.', badge: 'AB', backgroundColor: '#AB47BC', color: '#fff' },
|
|
35
|
+
]}
|
|
36
|
+
/>
|
|
37
|
+
);
|
|
38
|
+
`;
|
|
39
|
+
|
|
40
|
+
export const SizeSmallDefinition = `
|
|
41
|
+
import { Avatar } from './Avatar';
|
|
42
|
+
|
|
43
|
+
export const SizeSmallExample = () => (
|
|
44
|
+
<Avatar
|
|
45
|
+
size="sm"
|
|
46
|
+
items={[
|
|
47
|
+
{ text: 'User One', imageUrl: 'https://i.pravatar.cc/150?img=11' },
|
|
48
|
+
{ text: 'User Two', imageUrl: 'https://i.pravatar.cc/150?img=12' },
|
|
49
|
+
{ text: 'User Three', imageUrl: 'https://i.pravatar.cc/150?img=13' },
|
|
50
|
+
]}
|
|
51
|
+
/>
|
|
52
|
+
);
|
|
53
|
+
`;
|
|
54
|
+
|
|
55
|
+
export const SizeMediumDefinition = `
|
|
56
|
+
import { Avatar } from './Avatar';
|
|
57
|
+
|
|
58
|
+
export const SizeMediumExample = () => (
|
|
59
|
+
<Avatar
|
|
60
|
+
size="md"
|
|
61
|
+
items={[
|
|
62
|
+
{ text: 'User One', imageUrl: 'https://i.pravatar.cc/150?img=21' },
|
|
63
|
+
{ text: 'User Two', imageUrl: 'https://i.pravatar.cc/150?img=22' },
|
|
64
|
+
{ text: 'User Three', imageUrl: 'https://i.pravatar.cc/150?img=23' },
|
|
65
|
+
]}
|
|
66
|
+
/>
|
|
67
|
+
);
|
|
68
|
+
`;
|
|
69
|
+
|
|
70
|
+
export const SizeLargeDefinition = `
|
|
71
|
+
import { Avatar } from './Avatar';
|
|
72
|
+
|
|
73
|
+
export const SizeLargeExample = () => (
|
|
74
|
+
<Avatar
|
|
75
|
+
size="lg"
|
|
76
|
+
items={[
|
|
77
|
+
{ text: 'User One', imageUrl: 'https://i.pravatar.cc/150?img=31' },
|
|
78
|
+
{ text: 'User Two', imageUrl: 'https://i.pravatar.cc/150?img=32' },
|
|
79
|
+
]}
|
|
80
|
+
/>
|
|
81
|
+
);
|
|
82
|
+
`;
|
|
83
|
+
|
|
84
|
+
export const SizeExtraLargeDefinition = `
|
|
85
|
+
import { Avatar } from './Avatar';
|
|
86
|
+
|
|
87
|
+
export const SizeExtraLargeExample = () => (
|
|
88
|
+
<Avatar
|
|
89
|
+
size="xl"
|
|
90
|
+
items={[{ text: 'Maria', imageUrl: 'https://i.pravatar.cc/150?img=5' }]}
|
|
91
|
+
showText={false}
|
|
92
|
+
/>
|
|
93
|
+
);
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
export const NumericSizeDefinition = `
|
|
97
|
+
import { Avatar } from './Avatar';
|
|
98
|
+
|
|
99
|
+
export const NumericSizeExample = () => (
|
|
100
|
+
<Avatar
|
|
101
|
+
size={72}
|
|
102
|
+
items={[
|
|
103
|
+
{ text: 'User One', imageUrl: 'https://i.pravatar.cc/150?img=41' },
|
|
104
|
+
{ text: 'User Two', imageUrl: 'https://i.pravatar.cc/150?img=42' },
|
|
105
|
+
]}
|
|
106
|
+
showText={false}
|
|
107
|
+
/>
|
|
108
|
+
);
|
|
109
|
+
`;
|
|
110
|
+
|
|
111
|
+
export const FallbackIconDefinition = `
|
|
112
|
+
import { Avatar } from './Avatar';
|
|
113
|
+
|
|
114
|
+
export const FallbackIconExample = () => (
|
|
115
|
+
<Avatar
|
|
116
|
+
items={[
|
|
117
|
+
{ text: 'Without image' },
|
|
118
|
+
{ text: 'Broken image', imageUrl: 'https://example.invalid/broken.png' },
|
|
119
|
+
]}
|
|
120
|
+
showTooltip
|
|
121
|
+
/>
|
|
122
|
+
);
|
|
123
|
+
`;
|
|
124
|
+
|
|
125
|
+
export const DisplayedAvatarsLimitDefinition = `
|
|
126
|
+
import { Avatar } from './Avatar';
|
|
127
|
+
|
|
128
|
+
export const DisplayedAvatarsLimitExample = () => (
|
|
129
|
+
<Avatar
|
|
130
|
+
displayedAvatars={3}
|
|
131
|
+
items={[
|
|
132
|
+
{ text: 'User 1', imageUrl: 'https://i.pravatar.cc/150?img=51' },
|
|
133
|
+
{ text: 'User 2', imageUrl: 'https://i.pravatar.cc/150?img=52' },
|
|
134
|
+
{ text: 'User 3', imageUrl: 'https://i.pravatar.cc/150?img=53' },
|
|
135
|
+
{ text: 'User 4', imageUrl: 'https://i.pravatar.cc/150?img=54' },
|
|
136
|
+
{ text: 'User 5', imageUrl: 'https://i.pravatar.cc/150?img=55' },
|
|
137
|
+
]}
|
|
138
|
+
/>
|
|
139
|
+
);
|
|
140
|
+
`;
|
|
141
|
+
|
|
142
|
+
export const CustomStylingDefinition = `
|
|
143
|
+
import { Avatar } from './Avatar';
|
|
144
|
+
|
|
145
|
+
export const CustomStylingExample = () => (
|
|
146
|
+
<Avatar
|
|
147
|
+
size="md"
|
|
148
|
+
items={[
|
|
149
|
+
{ text: 'User One', imageUrl: 'https://i.pravatar.cc/150?img=61' },
|
|
150
|
+
{ text: 'User Two', imageUrl: 'https://i.pravatar.cc/150?img=62' },
|
|
151
|
+
]}
|
|
152
|
+
sx={{
|
|
153
|
+
p: 1,
|
|
154
|
+
borderRadius: 2,
|
|
155
|
+
bgcolor: 'action.hover',
|
|
156
|
+
}}
|
|
157
|
+
avatarSx={{
|
|
158
|
+
border: (theme) => '3px solid ' + theme.palette.primary.main,
|
|
159
|
+
}}
|
|
160
|
+
/>
|
|
161
|
+
);
|
|
162
|
+
`;
|