column copy, fix column rename

This commit is contained in:
Jan Prochazka
2020-10-31 11:37:44 +01:00
parent 9bed46fe01
commit 80e17eff39
2 changed files with 49 additions and 18 deletions

View File

@@ -413,7 +413,8 @@ export default function DataGridCore(props) {
.replace(/\r/g, '') .replace(/\r/g, '')
.split('\n') .split('\n')
.map((row) => row.split('\t')); .map((row) => row.split('\t'));
if (selectedCells.length <= 1) { const selectedRegular = cellsToRegularCells(selectedCells);
if (selectedRegular.length <= 1) {
const startRow = isRegularCell(currentCell) ? currentCell[0] : grider.rowCount; const startRow = isRegularCell(currentCell) ? currentCell[0] : grider.rowCount;
const startCol = isRegularCell(currentCell) ? currentCell[1] : 0; const startCol = isRegularCell(currentCell) ? currentCell[1] : 0;
let rowIndex = startRow; let rowIndex = startRow;
@@ -429,11 +430,10 @@ export default function DataGridCore(props) {
rowIndex += 1; rowIndex += 1;
} }
} }
if (selectedCells.length > 1) { if (selectedRegular.length > 1) {
const regularSelected = selectedCells.filter(isRegularCell); const startRow = _.min(selectedRegular.map((x) => x[0]));
const startRow = _.min(regularSelected.map((x) => x[0])); const startCol = _.min(selectedRegular.map((x) => x[1]));
const startCol = _.min(regularSelected.map((x) => x[1])); for (const cell of selectedRegular) {
for (const cell of regularSelected) {
const [rowIndex, colIndex] = cell; const [rowIndex, colIndex] = cell;
const selectionRow = rowIndex - startRow; const selectionRow = rowIndex - startRow;
const selectionCol = colIndex - startCol; const selectionCol = colIndex - startCol;

View File

@@ -36,32 +36,57 @@ const Icon = styled(FontIcon)`
`; `;
const EditorInput = styled.input` const EditorInput = styled.input`
width: calc(100% - 10px); width: calc(100% - 10px);
background-color: ${(props) =>
// @ts-ignore
props.isError ? '#FFCCCC' : 'white'};
`; `;
function ColumnNameEditor({ onEnter, onBlur = undefined, focusOnCreate = false, blurOnEnter = false, ...other }) { function ColumnNameEditor({
onEnter,
onBlur = undefined,
focusOnCreate = false,
blurOnEnter = false,
existingNames,
defaultValue = '',
...other
}) {
const [value, setValue] = React.useState(defaultValue || '');
const editorRef = React.useRef(null); const editorRef = React.useRef(null);
const isError = value && existingNames && existingNames.includes(value);
const handleKeyDown = (event) => { const handleKeyDown = (event) => {
if (event.keyCode == keycodes.enter) { if (value && event.keyCode == keycodes.enter && !isError) {
onEnter(editorRef.current.value); onEnter(value);
editorRef.current.value = ''; setValue('');
if (blurOnEnter) editorRef.current.blur(); if (blurOnEnter) editorRef.current.blur();
} }
if (event.keyCode == keycodes.escape) { if (event.keyCode == keycodes.escape) {
editorRef.current.value = ''; setValue('');
editorRef.current.blur(); editorRef.current.blur();
} }
}; };
const handleBlur = () => { const handleBlur = () => {
if (editorRef.current.value) { if (value && !isError) {
onEnter(editorRef.current.value); onEnter(value);
editorRef.current.value = ''; setValue('');
} }
if (onBlur) onBlur(); if (onBlur) onBlur();
}; };
React.useEffect(() => { React.useEffect(() => {
if (focusOnCreate) editorRef.current.focus(); if (focusOnCreate) editorRef.current.focus();
}, [focusOnCreate]); }, [focusOnCreate]);
return <EditorInput ref={editorRef} type="text" onKeyDown={handleKeyDown} onBlur={handleBlur} {...other} />; return (
<EditorInput
ref={editorRef}
type="text"
onKeyDown={handleKeyDown}
onBlur={handleBlur}
value={value}
onChange={(ev) => setValue(ev.target.value)}
// @ts-ignore
isError={isError}
{...other}
/>
);
} }
function exchange(array, i1, i2) { function exchange(array, i1, i2) {
@@ -87,14 +112,14 @@ function ColumnManagerRow({ column, onEdit, onRemove, onUp, onDown }) {
); );
} }
function dispatchChangeColumns(props, func) { function dispatchChangeColumns(props, func, rowFunc = null) {
const { modelState, dispatchModel } = props; const { modelState, dispatchModel } = props;
const model = modelState.value; const model = modelState.value;
dispatchModel({ dispatchModel({
type: 'set', type: 'set',
value: { value: {
...model, rows: rowFunc ? model.rows.map(rowFunc) : model.rows,
structure: { structure: {
...model.structure, ...model.structure,
columns: func(model.structure.columns), columns: func(model.structure.columns),
@@ -116,11 +141,16 @@ export default function FreeTableColumnEditor(props) {
<ColumnNameEditor <ColumnNameEditor
defaultValue={column.columnName} defaultValue={column.columnName}
onEnter={(columnName) => { onEnter={(columnName) => {
dispatchChangeColumns(props, (cols) => cols.map((col, i) => (index == i ? { columnName } : col))); dispatchChangeColumns(
props,
(cols) => cols.map((col, i) => (index == i ? { columnName } : col)),
(row) => _.mapKeys(row, (v, k) => (k == column.columnName ? columnName : k))
);
}} }}
onBlur={() => setEditingColumn(null)} onBlur={() => setEditingColumn(null)}
focusOnCreate focusOnCreate
blurOnEnter blurOnEnter
existingNames={model.structure.columns.map((x) => x.columnName)}
/> />
) : ( ) : (
<ColumnManagerRow <ColumnManagerRow
@@ -144,6 +174,7 @@ export default function FreeTableColumnEditor(props) {
dispatchChangeColumns(props, (cols) => [...cols, { columnName }]); dispatchChangeColumns(props, (cols) => [...cols, { columnName }]);
}} }}
placeholder="New column" placeholder="New column"
existingNames={model.structure.columns.map((x) => x.columnName)}
/> />
</ManagerInnerContainer> </ManagerInnerContainer>
</> </>