@bluemarble/bm-components 0.0.20 → 0.0.23
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/esm/index.js +445 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/components/Autocomplete/index.d.ts +12 -7
- package/dist/esm/types/components/EditableGrid/GridCell.d.ts +24 -0
- package/dist/esm/types/components/EditableGrid/GridInput/index.d.ts +16 -0
- package/dist/esm/types/components/EditableGrid/GridInputAutocomplete/index.d.ts +11 -0
- package/dist/esm/types/components/EditableGrid/GridInputText/index.d.ts +3 -0
- package/dist/esm/types/components/EditableGrid/GridSelect/index.d.ts +10 -0
- package/dist/esm/types/components/EditableGrid/NewRow/index.d.ts +13 -0
- package/dist/esm/types/components/EditableGrid/index.d.ts +52 -0
- package/dist/esm/types/components/hooks/useEditableGrid.d.ts +23 -0
- package/dist/esm/types/components/hooks/useEvent.d.ts +1 -0
- package/dist/esm/types/components/index.d.ts +3 -0
- package/dist/index.d.ts +99 -8
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import React__default, { memo, useState, useRef, useMemo, useEffect, useCallback } from 'react';
|
|
3
|
-
import { TableHead, TableRow, TableCell, TableSortLabel, Chip, Tooltip, IconButton, Paper, LinearProgress, TableContainer, Table, TableBody, TablePagination, Popover, Box as Box$2, Typography, Autocomplete as Autocomplete$1, TextField, Button, FormControl, Select as Select$1, InputLabel, FormControlLabel, Checkbox as Checkbox$1, Switch as Switch$1, FormLabel, RadioGroup, Radio as Radio$1, CircularProgress } from '@mui/material';
|
|
3
|
+
import { TableHead, TableRow, TableCell, TableSortLabel, Chip, Tooltip, IconButton, Paper, LinearProgress, TableContainer, Table, TableBody, TablePagination, Popover, Box as Box$2, Typography, Autocomplete as Autocomplete$1, TextField, Button, FormControl, Select as Select$1, InputLabel, FormControlLabel, Checkbox as Checkbox$1, Switch as Switch$1, FormLabel, RadioGroup, Radio as Radio$1, CircularProgress, MenuItem, ClickAwayListener } from '@mui/material';
|
|
4
4
|
import emStyled from '@emotion/styled';
|
|
5
5
|
import '@emotion/react';
|
|
6
6
|
import { Field, useField } from 'formik';
|
|
7
|
+
import { red } from '@mui/material/colors';
|
|
7
8
|
|
|
8
9
|
const GridHeader = ({ setOrder, setOrderBy, order, orderBy, titles = [], }) => {
|
|
9
10
|
function onRequestSort(title) {
|
|
@@ -3833,6 +3834,16 @@ function __rest(s, e) {
|
|
|
3833
3834
|
t[p[i]] = s[p[i]];
|
|
3834
3835
|
}
|
|
3835
3836
|
return t;
|
|
3837
|
+
}
|
|
3838
|
+
|
|
3839
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
3840
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3841
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
3842
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
3843
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
3844
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
3845
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
3846
|
+
});
|
|
3836
3847
|
}
|
|
3837
3848
|
|
|
3838
3849
|
const CustomTableCell = (_a) => {
|
|
@@ -3922,7 +3933,7 @@ const Grid = (props) => {
|
|
|
3922
3933
|
function handleFilterBySearch(text) {
|
|
3923
3934
|
const newArray = defaultData.filter((item) => {
|
|
3924
3935
|
for (const [, value] of Object.entries(item)) {
|
|
3925
|
-
if (`${value}`.includes(text))
|
|
3936
|
+
if (`${value}`.toLocaleLowerCase().includes(text.toLocaleLowerCase()))
|
|
3926
3937
|
return true;
|
|
3927
3938
|
}
|
|
3928
3939
|
return false;
|
|
@@ -14849,7 +14860,7 @@ const FormikSelect = (_a) => {
|
|
|
14849
14860
|
|
|
14850
14861
|
function Autocomplete(_a) {
|
|
14851
14862
|
var { withFormik = true, name, getOptionValue } = _a, rest = __rest(_a, ["withFormik", "name", "getOptionValue"]);
|
|
14852
|
-
if (withFormik
|
|
14863
|
+
if (withFormik) {
|
|
14853
14864
|
return (React__default.createElement(FormikAutocomplete, Object.assign({ name: name, getOptionValue: getOptionValue }, rest)));
|
|
14854
14865
|
}
|
|
14855
14866
|
else
|
|
@@ -14859,11 +14870,17 @@ function BaseInput(props) {
|
|
|
14859
14870
|
return (React__default.createElement(Autocomplete$1, Object.assign({ fullWidth: true, renderInput: (params) => React__default.createElement(TextField, Object.assign({ label: props.label }, params)) }, props)));
|
|
14860
14871
|
}
|
|
14861
14872
|
function FormikAutocomplete(_a) {
|
|
14862
|
-
var { getOptionValue } = _a, props = __rest(_a, ["getOptionValue"]);
|
|
14873
|
+
var { getOptionValue, option } = _a, props = __rest(_a, ["getOptionValue", "option"]);
|
|
14863
14874
|
const [_b, , { setValue }] = useField({
|
|
14864
14875
|
name: props.name,
|
|
14865
14876
|
}), { value: defaultValue } = _b, field = __rest(_b, ["value"]);
|
|
14866
|
-
const defaultOption =
|
|
14877
|
+
const defaultOption = useMemo(() => {
|
|
14878
|
+
const key = option === null || option === void 0 ? void 0 : option.key;
|
|
14879
|
+
if (key)
|
|
14880
|
+
return props.options.find((option) => option[key] === defaultValue);
|
|
14881
|
+
else
|
|
14882
|
+
return props.options.find((option) => Object.values(option)[0] === defaultValue);
|
|
14883
|
+
}, []);
|
|
14867
14884
|
const onChange = (_, newValue) => {
|
|
14868
14885
|
if (getOptionValue) {
|
|
14869
14886
|
setValue(getOptionValue(newValue));
|
|
@@ -14871,11 +14888,22 @@ function FormikAutocomplete(_a) {
|
|
|
14871
14888
|
else
|
|
14872
14889
|
setValue(newValue);
|
|
14873
14890
|
};
|
|
14874
|
-
|
|
14875
|
-
if (
|
|
14876
|
-
|
|
14877
|
-
|
|
14878
|
-
|
|
14891
|
+
const getOptionLabel = (item) => {
|
|
14892
|
+
if (props === null || props === void 0 ? void 0 : props.getOptionLabel)
|
|
14893
|
+
return props.getOptionLabel(item);
|
|
14894
|
+
else if (option === null || option === void 0 ? void 0 : option.label)
|
|
14895
|
+
return String(item[option.label]);
|
|
14896
|
+
return "[getOptionLabel] error";
|
|
14897
|
+
};
|
|
14898
|
+
const isOptionEqualToValue = (a, b) => {
|
|
14899
|
+
const key = option === null || option === void 0 ? void 0 : option.key;
|
|
14900
|
+
if (props === null || props === void 0 ? void 0 : props.isOptionEqualToValue)
|
|
14901
|
+
return props.isOptionEqualToValue(a, b);
|
|
14902
|
+
else if (key)
|
|
14903
|
+
return a[key] === b[key];
|
|
14904
|
+
return Object.values(a)[0] === Object.values(b)[0];
|
|
14905
|
+
};
|
|
14906
|
+
return (React__default.createElement(Autocomplete$1, Object.assign({ getOptionLabel: getOptionLabel, renderInput: (params) => (React__default.createElement(TextField, Object.assign({}, params, field, { label: props.label }))), defaultValue: defaultOption, onChange: onChange, isOptionEqualToValue: isOptionEqualToValue }, props)));
|
|
14879
14907
|
}
|
|
14880
14908
|
|
|
14881
14909
|
const Checkbox = (_a) => {
|
|
@@ -14950,5 +14978,411 @@ const LargeButton = (_a) => {
|
|
|
14950
14978
|
return (React__default.createElement(Button, Object.assign({ variant: "contained", fullWidth: true, sx: Object.assign({ color: "#fff", fontWeight: "bold" }, rest.sx), disabled: rest.disabled || loading }, rest), loading ? (React__default.createElement(CircularProgress, Object.assign({ size: 25, color: "inherit" }, CircularProgressProps))) : (children)));
|
|
14951
14979
|
};
|
|
14952
14980
|
|
|
14953
|
-
|
|
14981
|
+
/* eslint no-undef: "off" */
|
|
14982
|
+
function useEvent(event, handler, passive = false) {
|
|
14983
|
+
useEffect(() => {
|
|
14984
|
+
window.addEventListener(event, handler, passive);
|
|
14985
|
+
return function cleanup() {
|
|
14986
|
+
window.removeEventListener(event, handler);
|
|
14987
|
+
};
|
|
14988
|
+
});
|
|
14989
|
+
}
|
|
14990
|
+
|
|
14991
|
+
const InputAutocompleteCell = ({ options, label, value, onInputChange, currentValue, defaultValue: ExternalDefaultValue, }) => {
|
|
14992
|
+
const defaultValue = options.find((option) => String(option[value]) ===
|
|
14993
|
+
(String(currentValue) || String(ExternalDefaultValue)));
|
|
14994
|
+
const onChange = (_, newValue, reason) => {
|
|
14995
|
+
if (reason === "clear")
|
|
14996
|
+
return;
|
|
14997
|
+
onInputChange(newValue[value]);
|
|
14998
|
+
};
|
|
14999
|
+
return (React__default.createElement(Autocomplete$1, { defaultValue: defaultValue, options: options, onChange: onChange, getOptionLabel: (option) => option[label], renderInput: (params) => React__default.createElement(RenderInput, Object.assign({}, params)), isOptionEqualToValue: (a, b) => (a === null || a === void 0 ? void 0 : a[value]) === (b === null || b === void 0 ? void 0 : b[value]) }));
|
|
15000
|
+
};
|
|
15001
|
+
const RenderInput = (params) => {
|
|
15002
|
+
return (React__default.createElement(TextField, Object.assign({}, params, { autoFocus: true, focused: true, sx: {
|
|
15003
|
+
width: "100%",
|
|
15004
|
+
height: "100%",
|
|
15005
|
+
position: "absolute",
|
|
15006
|
+
left: 0,
|
|
15007
|
+
top: 0,
|
|
15008
|
+
borderRadius: 0,
|
|
15009
|
+
color: "primary.main",
|
|
15010
|
+
p: 0,
|
|
15011
|
+
}, inputProps: Object.assign(Object.assign({}, params.inputProps), { sx: Object.assign(Object.assign({}, params.inputProps.sx), { width: "100%", height: "100%", p: "0!important", pl: 2 }) }), InputProps: Object.assign(Object.assign({}, params.InputProps), { sx: Object.assign(Object.assign({}, params.inputProps.sx), { height: "100%", borderRadius: 0 }) }) })));
|
|
15012
|
+
};
|
|
15013
|
+
|
|
15014
|
+
const InputTextCell = ({ defaultValue, currentValue, inputProps = {}, onInputChange, }) => {
|
|
15015
|
+
const { onDefaultValue } = inputProps, rest = __rest(inputProps, ["onDefaultValue"]);
|
|
15016
|
+
const formattedDefaultValue = onDefaultValue
|
|
15017
|
+
? onDefaultValue(currentValue || defaultValue)
|
|
15018
|
+
: currentValue || defaultValue;
|
|
15019
|
+
return (React__default.createElement(TextField, Object.assign({ focused: true, autoFocus: true, onChange: ({ target }) => onInputChange(target.value), sx: {
|
|
15020
|
+
width: "100%",
|
|
15021
|
+
height: "100%",
|
|
15022
|
+
position: "absolute",
|
|
15023
|
+
left: 0,
|
|
15024
|
+
top: 0,
|
|
15025
|
+
borderRadius: 0,
|
|
15026
|
+
color: "primary.main",
|
|
15027
|
+
}, InputProps: { sx: { width: "100%", height: "100%", borderRadius: 0 } }, inputProps: {
|
|
15028
|
+
sx: { width: "100%", height: "100%", pl: 1, borderRadius: 0 },
|
|
15029
|
+
}, defaultValue: formattedDefaultValue }, rest)));
|
|
15030
|
+
};
|
|
15031
|
+
|
|
15032
|
+
const InputSelectCell = ({ options, label, value, setCanSave, onInputChange, currentValue, }) => {
|
|
15033
|
+
var _a;
|
|
15034
|
+
const defaultValue = (_a = options.find((option) => option[value] === currentValue)) === null || _a === void 0 ? void 0 : _a[value];
|
|
15035
|
+
const onChange = (_, { props }) => {
|
|
15036
|
+
onInputChange(props.value);
|
|
15037
|
+
};
|
|
15038
|
+
return (React__default.createElement(TextField, { defaultValue: defaultValue, autoFocus: true, focused: true, onChange: onChange, sx: {
|
|
15039
|
+
width: "100%",
|
|
15040
|
+
height: "100%",
|
|
15041
|
+
position: "absolute",
|
|
15042
|
+
left: 0,
|
|
15043
|
+
top: 0,
|
|
15044
|
+
borderRadius: 0,
|
|
15045
|
+
color: "primary.main",
|
|
15046
|
+
}, inputProps: {
|
|
15047
|
+
sx: { width: "100%", height: "100%", p: 0, pl: 2 },
|
|
15048
|
+
}, InputProps: {
|
|
15049
|
+
sx: { height: "100%", borderRadius: 0 },
|
|
15050
|
+
}, SelectProps: {
|
|
15051
|
+
// onOpen: () => setCanSave(false),
|
|
15052
|
+
// onClose: () => setCanSave(true)
|
|
15053
|
+
}, select: true }, options.map((option) => (React__default.createElement(MenuItem, { key: option[value], value: option[value] }, option[label])))));
|
|
15054
|
+
};
|
|
15055
|
+
|
|
15056
|
+
const InputCell = (props) => {
|
|
15057
|
+
if (props.type === "select" && props.select)
|
|
15058
|
+
return (React__default.createElement(InputSelectCell, Object.assign({}, props.select, { setCanSave: props.setCanSave, onInputChange: props.onInputChange, currentValue: props.currentValue })));
|
|
15059
|
+
else if (props.type === "autocomplete")
|
|
15060
|
+
return (React__default.createElement(InputAutocompleteCell, Object.assign({}, props.select, { setCanSave: props.setCanSave, onInputChange: props.onInputChange, defaultValue: props.defaultValue, currentValue: props.currentValue })));
|
|
15061
|
+
else {
|
|
15062
|
+
return React__default.createElement(InputTextCell, Object.assign({}, props));
|
|
15063
|
+
}
|
|
15064
|
+
};
|
|
15065
|
+
|
|
15066
|
+
const GridCell = ({ value, focusedIndex, canEditTable, columnIndex, rowIndex, isEditing, onInputChange, bordered, onClick, onDoubleClick, setCanSave, tableCellProps, validationErrors, column, }) => {
|
|
15067
|
+
var _a;
|
|
15068
|
+
const formattedValue = (column === null || column === void 0 ? void 0 : column.formatLabelValue)
|
|
15069
|
+
? column === null || column === void 0 ? void 0 : column.formatLabelValue(value)
|
|
15070
|
+
: value;
|
|
15071
|
+
const isFocused = focusedIndex.y === rowIndex && focusedIndex.x === columnIndex;
|
|
15072
|
+
const canEdit = isEditing && isFocused;
|
|
15073
|
+
const withOthersStyles = () => {
|
|
15074
|
+
let result = {};
|
|
15075
|
+
if (bordered) {
|
|
15076
|
+
result = {
|
|
15077
|
+
border: "1px solid",
|
|
15078
|
+
borderColor: "divider",
|
|
15079
|
+
};
|
|
15080
|
+
}
|
|
15081
|
+
if (isFocused && !canEdit && canEditTable) {
|
|
15082
|
+
result = Object.assign(Object.assign({}, result), { outline: "2px solid", color: "primary.main", outlineColor: "inherit" });
|
|
15083
|
+
}
|
|
15084
|
+
if (canEdit) {
|
|
15085
|
+
result = Object.assign(Object.assign({}, result), { p: 0 });
|
|
15086
|
+
}
|
|
15087
|
+
if (validationErrors === null || validationErrors === void 0 ? void 0 : validationErrors.includes(column === null || column === void 0 ? void 0 : column.name)) {
|
|
15088
|
+
result = {
|
|
15089
|
+
bgcolor: red[100],
|
|
15090
|
+
};
|
|
15091
|
+
}
|
|
15092
|
+
return result;
|
|
15093
|
+
};
|
|
15094
|
+
return (React__default.createElement(TableCell, Object.assign({ onClick: onClick, onDoubleClick: onDoubleClick }, tableCellProps, { sx: Object.assign(Object.assign({ position: "relative" }, tableCellProps === null || tableCellProps === void 0 ? void 0 : tableCellProps.sx), withOthersStyles()) }), canEdit ? (React__default.createElement(InputCell, { select: column === null || column === void 0 ? void 0 : column.select, type: column === null || column === void 0 ? void 0 : column.type, inputProps: column === null || column === void 0 ? void 0 : column.inputProps, defaultValue: (_a = column === null || column === void 0 ? void 0 : column.newRow) === null || _a === void 0 ? void 0 : _a.defaultValue, currentValue: value, onInputChange: onInputChange, setCanSave: setCanSave })) : (React__default.createElement(Typography, { variant: "body2", color: "initial" }, formattedValue))));
|
|
15095
|
+
};
|
|
15096
|
+
|
|
15097
|
+
const NewRow = ({ columns, hasCheckbox, maxPositions, onSaveNewRow, }) => {
|
|
15098
|
+
const [focusedIndex, setFocusedIndex] = useState({ x: 0, y: 0 });
|
|
15099
|
+
const newRowValues = useRef({});
|
|
15100
|
+
const [validationError, setValidationError] = useState([]);
|
|
15101
|
+
const onInputChange = (prop, value) => {
|
|
15102
|
+
newRowValues.current = Object.assign(Object.assign({}, newRowValues.current), { [prop]: value });
|
|
15103
|
+
};
|
|
15104
|
+
const getDefaultRowValues = () => {
|
|
15105
|
+
const defaultNewRow = {};
|
|
15106
|
+
columns.forEach((column) => {
|
|
15107
|
+
var _a;
|
|
15108
|
+
defaultNewRow[column.name] =
|
|
15109
|
+
((_a = column === null || column === void 0 ? void 0 : column.newRow) === null || _a === void 0 ? void 0 : _a.defaultValue) || undefined;
|
|
15110
|
+
});
|
|
15111
|
+
return defaultNewRow;
|
|
15112
|
+
};
|
|
15113
|
+
const saveRow = () => {
|
|
15114
|
+
const requiredColumns = columns.filter((column) => column.required);
|
|
15115
|
+
let isRequiredsFilled = true;
|
|
15116
|
+
requiredColumns.forEach((column) => {
|
|
15117
|
+
var _a, _b, _c;
|
|
15118
|
+
if (!((_a = newRowValues.current) === null || _a === void 0 ? void 0 : _a[column.name]) ||
|
|
15119
|
+
((_c = (_b = newRowValues.current) === null || _b === void 0 ? void 0 : _b[column.name]) === null || _c === void 0 ? void 0 : _c.trim()) === "")
|
|
15120
|
+
isRequiredsFilled = false;
|
|
15121
|
+
});
|
|
15122
|
+
if (!isRequiredsFilled) {
|
|
15123
|
+
setValidationError(requiredColumns.map((column) => column === null || column === void 0 ? void 0 : column.name));
|
|
15124
|
+
return;
|
|
15125
|
+
}
|
|
15126
|
+
if (onSaveNewRow)
|
|
15127
|
+
onSaveNewRow(newRowValues.current);
|
|
15128
|
+
newRowValues.current = getDefaultRowValues();
|
|
15129
|
+
setFocusedIndex({ x: 0, y: 0 });
|
|
15130
|
+
setValidationError([]);
|
|
15131
|
+
};
|
|
15132
|
+
const onKeyDown = (event) => {
|
|
15133
|
+
const canMoveRight = focusedIndex.x + 1 <= maxPositions.x;
|
|
15134
|
+
const canMoveLeft = focusedIndex.x - 1 >= 0;
|
|
15135
|
+
const { key, shiftKey } = event;
|
|
15136
|
+
if (key === "Enter") {
|
|
15137
|
+
event.preventDefault();
|
|
15138
|
+
saveRow();
|
|
15139
|
+
}
|
|
15140
|
+
else if (shiftKey && key === "Tab") {
|
|
15141
|
+
event.preventDefault();
|
|
15142
|
+
setFocusedIndex({
|
|
15143
|
+
y: 0,
|
|
15144
|
+
x: canMoveLeft ? focusedIndex.x - 1 : focusedIndex.x,
|
|
15145
|
+
});
|
|
15146
|
+
}
|
|
15147
|
+
else if (key === "Tab") {
|
|
15148
|
+
event.preventDefault();
|
|
15149
|
+
setFocusedIndex({
|
|
15150
|
+
y: 0,
|
|
15151
|
+
x: canMoveRight ? focusedIndex.x + 1 : focusedIndex.x,
|
|
15152
|
+
});
|
|
15153
|
+
}
|
|
15154
|
+
};
|
|
15155
|
+
useEffect(() => {
|
|
15156
|
+
newRowValues.current = getDefaultRowValues();
|
|
15157
|
+
}, []);
|
|
15158
|
+
return (React__default.createElement(TableRow, { onKeyDown: onKeyDown },
|
|
15159
|
+
hasCheckbox && React__default.createElement(TableCell, { padding: "checkbox" }),
|
|
15160
|
+
columns
|
|
15161
|
+
.filter((columns) => columns.visible !== false)
|
|
15162
|
+
.map((column, columnIndex) => {
|
|
15163
|
+
var _a, _b;
|
|
15164
|
+
const key = String(column.name || column.label || columnIndex);
|
|
15165
|
+
return (React__default.createElement(GridCell, { canEditTable: true, bordered: true, key: key, isEditing: true, focusedIndex: focusedIndex, rowIndex: 0, columnIndex: columnIndex, value: ((_a = newRowValues.current) === null || _a === void 0 ? void 0 : _a[column.name]) ||
|
|
15166
|
+
((_b = column === null || column === void 0 ? void 0 : column.newRow) === null || _b === void 0 ? void 0 : _b.defaultValue), onInputChange: (value) => onInputChange(column.name, value), column: column, onClick: () => setFocusedIndex({ x: columnIndex, y: 0 }), onDoubleClick: () => { }, setCanSave: () => { }, tableCellProps: { sx: { height: 31 } }, validationErrors: validationError }));
|
|
15167
|
+
})));
|
|
15168
|
+
};
|
|
15169
|
+
|
|
15170
|
+
function EditableGrid({ columns, data, primaryKey, onSave, setFocusedIndex, setIsEditing, focusedIndex, isEditing, bordered, canEditTable, setCanEditTable, selectedRows, setSelectedRows, checkbox = false, checkMultiple = false, onSaveNewRow, canInsertNewRow, }) {
|
|
15171
|
+
var _a;
|
|
15172
|
+
const [canSave, setCanSave] = useState(true);
|
|
15173
|
+
const checkboxOffset = checkbox ? 1 : 0;
|
|
15174
|
+
const inputValue = useRef("");
|
|
15175
|
+
const maxPositions = {
|
|
15176
|
+
x: columns.length - 1 - checkboxOffset,
|
|
15177
|
+
y: data.length - 1,
|
|
15178
|
+
};
|
|
15179
|
+
const focusedRow = data === null || data === void 0 ? void 0 : data[focusedIndex === null || focusedIndex === void 0 ? void 0 : focusedIndex.y];
|
|
15180
|
+
const focusedProp = (_a = columns === null || columns === void 0 ? void 0 : columns[(focusedIndex === null || focusedIndex === void 0 ? void 0 : focusedIndex.x) + checkboxOffset]) === null || _a === void 0 ? void 0 : _a.name;
|
|
15181
|
+
const gridRef = useRef(null);
|
|
15182
|
+
const [isShiftPressed, setIsShiftPressed] = useState(false);
|
|
15183
|
+
const onInputChange = (value) => {
|
|
15184
|
+
inputValue.current = value;
|
|
15185
|
+
};
|
|
15186
|
+
const moveIndexPosition = (x = 0, y = 0, ignoreEditing) => {
|
|
15187
|
+
let xPosition = focusedIndex.x + x;
|
|
15188
|
+
let yPosition = focusedIndex.y + y;
|
|
15189
|
+
if (isEditing && !ignoreEditing)
|
|
15190
|
+
return;
|
|
15191
|
+
if (xPosition > maxPositions.x && yPosition < maxPositions.y) {
|
|
15192
|
+
xPosition = 0;
|
|
15193
|
+
yPosition += 1;
|
|
15194
|
+
}
|
|
15195
|
+
else if (xPosition < 0 && yPosition > 0) {
|
|
15196
|
+
xPosition = maxPositions.x;
|
|
15197
|
+
yPosition -= 1;
|
|
15198
|
+
}
|
|
15199
|
+
else if (xPosition < 0) {
|
|
15200
|
+
xPosition = 0;
|
|
15201
|
+
}
|
|
15202
|
+
else if (yPosition < 0) {
|
|
15203
|
+
yPosition = 0;
|
|
15204
|
+
}
|
|
15205
|
+
else if (xPosition > maxPositions.x) {
|
|
15206
|
+
xPosition = maxPositions.x;
|
|
15207
|
+
}
|
|
15208
|
+
else if (yPosition > maxPositions.y) {
|
|
15209
|
+
yPosition = maxPositions.y;
|
|
15210
|
+
}
|
|
15211
|
+
setFocusedIndex({
|
|
15212
|
+
x: xPosition,
|
|
15213
|
+
y: yPosition,
|
|
15214
|
+
});
|
|
15215
|
+
};
|
|
15216
|
+
const handleSaveChanges = () => __awaiter(this, void 0, void 0, function* () {
|
|
15217
|
+
const previousValue = focusedRow[focusedProp];
|
|
15218
|
+
if (previousValue !== inputValue.current)
|
|
15219
|
+
onSave(updateRowData(), {
|
|
15220
|
+
prop: focusedProp,
|
|
15221
|
+
newValue: inputValue.current,
|
|
15222
|
+
index: focusedIndex.y,
|
|
15223
|
+
});
|
|
15224
|
+
});
|
|
15225
|
+
const updateRowData = () => {
|
|
15226
|
+
const updatedRow = Object.assign(Object.assign({}, focusedRow), { [focusedProp]: inputValue.current });
|
|
15227
|
+
return updatedRow;
|
|
15228
|
+
};
|
|
15229
|
+
const startEditOrSave = () => {
|
|
15230
|
+
if (!canSave)
|
|
15231
|
+
return;
|
|
15232
|
+
if (!isEditing) {
|
|
15233
|
+
setIsEditing(true);
|
|
15234
|
+
}
|
|
15235
|
+
else {
|
|
15236
|
+
handleSaveChanges();
|
|
15237
|
+
setIsEditing(false);
|
|
15238
|
+
}
|
|
15239
|
+
};
|
|
15240
|
+
const saveAndMove = (event) => {
|
|
15241
|
+
event.preventDefault();
|
|
15242
|
+
if (isEditing)
|
|
15243
|
+
handleSaveChanges();
|
|
15244
|
+
moveIndexPosition(1, 0, true);
|
|
15245
|
+
};
|
|
15246
|
+
const keyMaps = {
|
|
15247
|
+
ArrowRight: (event) => moveIndexPosition(1, 0),
|
|
15248
|
+
ArrowLeft: (event) => moveIndexPosition(-1, 0),
|
|
15249
|
+
ArrowDown: (event) => moveIndexPosition(0, 1),
|
|
15250
|
+
ArrowUp: (event) => moveIndexPosition(0, -1),
|
|
15251
|
+
Enter: (event) => startEditOrSave(),
|
|
15252
|
+
Tab: (event) => saveAndMove(event),
|
|
15253
|
+
Escape: (event) => {
|
|
15254
|
+
event.preventDefault();
|
|
15255
|
+
setIsEditing(false);
|
|
15256
|
+
if (!isEditing)
|
|
15257
|
+
setCanEditTable(false);
|
|
15258
|
+
},
|
|
15259
|
+
};
|
|
15260
|
+
const handleKeyDown = (event) => {
|
|
15261
|
+
if (event.key === "Shift")
|
|
15262
|
+
setIsShiftPressed(true);
|
|
15263
|
+
if (!canEditTable)
|
|
15264
|
+
return;
|
|
15265
|
+
const movement = keyMaps[event.key];
|
|
15266
|
+
if (event.shiftKey && event.key === "Tab") {
|
|
15267
|
+
event.preventDefault();
|
|
15268
|
+
if (isEditing)
|
|
15269
|
+
handleSaveChanges();
|
|
15270
|
+
moveIndexPosition(-1, 0, true);
|
|
15271
|
+
}
|
|
15272
|
+
else if (movement) {
|
|
15273
|
+
movement(event);
|
|
15274
|
+
}
|
|
15275
|
+
};
|
|
15276
|
+
const handleKeyUp = (event) => {
|
|
15277
|
+
if (event.key === "Shift")
|
|
15278
|
+
setIsShiftPressed(false);
|
|
15279
|
+
};
|
|
15280
|
+
const onClick = (x, y) => {
|
|
15281
|
+
if (!canEditTable)
|
|
15282
|
+
setCanEditTable(true);
|
|
15283
|
+
if (focusedIndex.x === x && focusedIndex.y === y && canEditTable)
|
|
15284
|
+
setIsEditing(true);
|
|
15285
|
+
else {
|
|
15286
|
+
if (isEditing)
|
|
15287
|
+
handleSaveChanges();
|
|
15288
|
+
setFocusedIndex({ x, y });
|
|
15289
|
+
}
|
|
15290
|
+
};
|
|
15291
|
+
const onDoubleClickCell = (x, y) => {
|
|
15292
|
+
if (!canEditTable)
|
|
15293
|
+
return;
|
|
15294
|
+
setIsEditing(true);
|
|
15295
|
+
setFocusedIndex({ x, y });
|
|
15296
|
+
};
|
|
15297
|
+
const onClickAwayTable = () => {
|
|
15298
|
+
if (!canSave)
|
|
15299
|
+
return;
|
|
15300
|
+
if (isEditing)
|
|
15301
|
+
handleSaveChanges();
|
|
15302
|
+
setCanEditTable(false);
|
|
15303
|
+
setIsEditing(false);
|
|
15304
|
+
};
|
|
15305
|
+
const isSelectedRow = (row) => selectedRows.includes(row);
|
|
15306
|
+
const onChangeSelectRow = (row) => {
|
|
15307
|
+
if (isSelectedRow(row))
|
|
15308
|
+
setSelectedRows(selectedRows.filter((selectedRow) => selectedRow !== row));
|
|
15309
|
+
else if (selectedRows.length > 0 && checkMultiple && isShiftPressed) {
|
|
15310
|
+
const lastRowIndex = data.findIndex((each) => each[primaryKey] === selectedRows[selectedRows.length - 1]);
|
|
15311
|
+
const currentRowIndex = data.findIndex((each) => each[primaryKey] === row);
|
|
15312
|
+
const sortedIndexes = [lastRowIndex, currentRowIndex].sort((a, b) => a - b);
|
|
15313
|
+
const filteredRows = data.filter((_, index) => index >= sortedIndexes[0] && index <= sortedIndexes[1]);
|
|
15314
|
+
const filteredRowsId = filteredRows.map((data) => data[primaryKey]);
|
|
15315
|
+
setSelectedRows([...selectedRows, ...filteredRowsId]);
|
|
15316
|
+
}
|
|
15317
|
+
else {
|
|
15318
|
+
if (!checkMultiple)
|
|
15319
|
+
setSelectedRows([row]);
|
|
15320
|
+
else
|
|
15321
|
+
setSelectedRows([...selectedRows, row]);
|
|
15322
|
+
}
|
|
15323
|
+
};
|
|
15324
|
+
useEvent("keydown", handleKeyDown);
|
|
15325
|
+
useEvent("keyup", handleKeyUp);
|
|
15326
|
+
useEffect(() => {
|
|
15327
|
+
if (isEditing) {
|
|
15328
|
+
inputValue.current = focusedRow[focusedProp];
|
|
15329
|
+
}
|
|
15330
|
+
}, [isEditing, focusedIndex]);
|
|
15331
|
+
return (React__default.createElement(ClickAwayListener, { onClickAway: onClickAwayTable },
|
|
15332
|
+
React__default.createElement(Paper, null,
|
|
15333
|
+
React__default.createElement(Table, { ref: gridRef, size: "small" },
|
|
15334
|
+
React__default.createElement(TableHead, null,
|
|
15335
|
+
React__default.createElement(TableRow, null, columns.map((column, index) => {
|
|
15336
|
+
var _a;
|
|
15337
|
+
const key = column.name === ""
|
|
15338
|
+
? column.id || column.label || index
|
|
15339
|
+
: String(column.name);
|
|
15340
|
+
return (React__default.createElement(TableCell, Object.assign({ key: key }, column === null || column === void 0 ? void 0 : column.TableCellProps, { sx: Object.assign({ border: "1px solid", borderColor: "divider" }, (_a = column === null || column === void 0 ? void 0 : column.TableCellProps) === null || _a === void 0 ? void 0 : _a.sx) }),
|
|
15341
|
+
React__default.createElement(Typography, { variant: "body2", fontWeight: "bold", fontSize: "inherit" }, column.label)));
|
|
15342
|
+
}))),
|
|
15343
|
+
React__default.createElement(TableBody, null,
|
|
15344
|
+
canInsertNewRow && (React__default.createElement(NewRow, { columns: columns, hasCheckbox: checkbox, maxPositions: maxPositions, onSaveNewRow: onSaveNewRow })),
|
|
15345
|
+
data.map((row, rowIndex) => {
|
|
15346
|
+
return (React__default.createElement(TableRow, { key: row[primaryKey] },
|
|
15347
|
+
checkbox && (React__default.createElement(TableCell, { padding: "checkbox" },
|
|
15348
|
+
React__default.createElement(Checkbox$1, { checked: isSelectedRow(row[primaryKey]), onChange: () => onChangeSelectRow(row[primaryKey]), size: "small", sx: { m: 0 } }))),
|
|
15349
|
+
columns
|
|
15350
|
+
.filter((columns) => columns.visible !== false)
|
|
15351
|
+
.map((column, columnIndex) => {
|
|
15352
|
+
const key = String(column.name || column.label || columnIndex);
|
|
15353
|
+
return (React__default.createElement(GridCell, { canEditTable: canEditTable, bordered: bordered, key: key, isEditing: isEditing, focusedIndex: focusedIndex, rowIndex: rowIndex, columnIndex: columnIndex, value: row[column.name], onInputChange: onInputChange, column: column, onClick: () => onClick(columnIndex, rowIndex), onDoubleClick: () => onDoubleClickCell(columnIndex, rowIndex), setCanSave: setCanSave }));
|
|
15354
|
+
})));
|
|
15355
|
+
}))))));
|
|
15356
|
+
}
|
|
15357
|
+
|
|
15358
|
+
function useEditableGrid() {
|
|
15359
|
+
const [focusedIndex, setFocusedIndex] = useState({ x: 0, y: 0 });
|
|
15360
|
+
const [isEditing, setIsEditing] = useState(false);
|
|
15361
|
+
const [data, setData] = useState([]);
|
|
15362
|
+
const [canEditTable, setCanEditTable] = useState(false);
|
|
15363
|
+
const [selectedRows, setSelectedRows] = useState([]);
|
|
15364
|
+
const [canInsertNewRow, setCanInsertNewRow] = useState(false);
|
|
15365
|
+
const onSave = (newItem) => {
|
|
15366
|
+
const updatedData = data.filter((_, index) => index !== focusedIndex.y);
|
|
15367
|
+
updatedData.splice(focusedIndex.y, 0, newItem);
|
|
15368
|
+
setData(updatedData);
|
|
15369
|
+
};
|
|
15370
|
+
return {
|
|
15371
|
+
data,
|
|
15372
|
+
focusedIndex,
|
|
15373
|
+
isEditing,
|
|
15374
|
+
setData,
|
|
15375
|
+
setIsEditing,
|
|
15376
|
+
setFocusedIndex,
|
|
15377
|
+
onSave,
|
|
15378
|
+
canEditTable,
|
|
15379
|
+
setCanEditTable,
|
|
15380
|
+
selectedRows,
|
|
15381
|
+
setSelectedRows,
|
|
15382
|
+
canInsertNewRow,
|
|
15383
|
+
setCanInsertNewRow,
|
|
15384
|
+
};
|
|
15385
|
+
}
|
|
15386
|
+
|
|
15387
|
+
export { Autocomplete, Checkbox, EditableGrid, EditableTableCell, Grid, Input, InputMask, LargeButton, Radio, Select, Switch, Td, Tr, filterData, useEditableGrid, useEvent };
|
|
14954
15388
|
//# sourceMappingURL=index.js.map
|