mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 01:33:59 +00:00
macro default parameter values
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { runMacro } from '@dbgate/datalib';
|
import { runMacro } from '@dbgate/datalib';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import _ from 'lodash';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
import { ManagerMainContainer, ManagerOuterContainer_60, ManagerOuterContainer_40 } from '../datagrid/ManagerStyles';
|
import { ManagerMainContainer, ManagerOuterContainer_60, ManagerOuterContainer_40 } from '../datagrid/ManagerStyles';
|
||||||
@@ -20,6 +21,14 @@ const DataGridContainer = styled.div`
|
|||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
function extractMacroValuesForMacro(macroValues, macro) {
|
||||||
|
if (!macro) return {};
|
||||||
|
return {
|
||||||
|
..._.fromPairs((macro.args || []).filter((x) => x.default != null).map((x) => [x.name, x.default])),
|
||||||
|
..._.mapKeys(macroValues, (v, k) => k.replace(/^.*#/, '')),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function FreeTableGrid(props) {
|
export default function FreeTableGrid(props) {
|
||||||
const { modelState, dispatchModel } = props;
|
const { modelState, dispatchModel } = props;
|
||||||
const [managerSize, setManagerSize] = React.useState(0);
|
const [managerSize, setManagerSize] = React.useState(0);
|
||||||
@@ -54,7 +63,7 @@ export default function FreeTableGrid(props) {
|
|||||||
<FreeTableGridCore
|
<FreeTableGridCore
|
||||||
{...props}
|
{...props}
|
||||||
macroPreview={selectedMacro}
|
macroPreview={selectedMacro}
|
||||||
macroValues={macroValues}
|
macroValues={extractMacroValuesForMacro(macroValues, selectedMacro)}
|
||||||
onSelectionChanged={setSelectedCells}
|
onSelectionChanged={setSelectedCells}
|
||||||
setSelectedMacro={setSelectedMacro}
|
setSelectedMacro={setSelectedMacro}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -93,9 +93,11 @@ export default function MacroDetail({ selectedMacro, setSelectedMacro, onChangeV
|
|||||||
<WidgetTitle>Parameters</WidgetTitle>
|
<WidgetTitle>Parameters</WidgetTitle>
|
||||||
{selectedMacro.args && selectedMacro.args.length > 0 ? (
|
{selectedMacro.args && selectedMacro.args.length > 0 ? (
|
||||||
<MacroParameters
|
<MacroParameters
|
||||||
|
key={selectedMacro.name}
|
||||||
args={selectedMacro.args}
|
args={selectedMacro.args}
|
||||||
onChangeValues={onChangeValues}
|
onChangeValues={onChangeValues}
|
||||||
initialValues={macroValues}
|
macroValues={macroValues}
|
||||||
|
namePrefix={`${selectedMacro.name}#`}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<TextWrapper>This macro has no parameters</TextWrapper>
|
<TextWrapper>This macro has no parameters</TextWrapper>
|
||||||
|
|||||||
@@ -10,13 +10,14 @@ import {
|
|||||||
} from '../utility/forms';
|
} from '../utility/forms';
|
||||||
import { Formik, Form, useFormikContext } from 'formik';
|
import { Formik, Form, useFormikContext } from 'formik';
|
||||||
|
|
||||||
function MacroArgument({ arg }) {
|
function MacroArgument({ arg, namePrefix }) {
|
||||||
|
const name = `${namePrefix}${arg.name}`;
|
||||||
if (arg.type == 'text') {
|
if (arg.type == 'text') {
|
||||||
return <FormTextField label={arg.label} name={arg.name} />;
|
return <FormTextField label={arg.label} name={name} />;
|
||||||
}
|
}
|
||||||
if (arg.type == 'select') {
|
if (arg.type == 'select') {
|
||||||
return (
|
return (
|
||||||
<FormSelectField label={arg.label} name={arg.name}>
|
<FormSelectField label={arg.label} name={name}>
|
||||||
{arg.options.map((opt) =>
|
{arg.options.map((opt) =>
|
||||||
_.isString(opt) ? <option value={opt}>{opt}</option> : <option value={opt.value}>{opt.name}</option>
|
_.isString(opt) ? <option value={opt}>{opt}</option> : <option value={opt.value}>{opt.name}</option>
|
||||||
)}
|
)}
|
||||||
@@ -26,7 +27,7 @@ function MacroArgument({ arg }) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function MacroArgumentList({ args, onChangeValues }) {
|
function MacroArgumentList({ args, onChangeValues, namePrefix }) {
|
||||||
const { values } = useFormikContext();
|
const { values } = useFormikContext();
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (onChangeValues) onChangeValues(values);
|
if (onChangeValues) onChangeValues(values);
|
||||||
@@ -35,18 +36,22 @@ function MacroArgumentList({ args, onChangeValues }) {
|
|||||||
<>
|
<>
|
||||||
{' '}
|
{' '}
|
||||||
{args.map((arg) => (
|
{args.map((arg) => (
|
||||||
<MacroArgument arg={arg} key={arg.name} />
|
<MacroArgument arg={arg} key={arg.name} namePrefix={namePrefix} />
|
||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function MacroParameters({ args, onChangeValues, initialValues }) {
|
export default function MacroParameters({ args, onChangeValues, macroValues, namePrefix }) {
|
||||||
if (!args || args.length == 0) return null;
|
if (!args || args.length == 0) return null;
|
||||||
|
const initialValues = {
|
||||||
|
..._.fromPairs(args.filter((x) => x.default != null).map((x) => [`${namePrefix}${x.name}`, x.default])),
|
||||||
|
...macroValues,
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<Formik initialValues={initialValues} onSubmit={() => {}}>
|
<Formik initialValues={initialValues} onSubmit={() => {}}>
|
||||||
<Form>
|
<Form>
|
||||||
<MacroArgumentList args={args} onChangeValues={onChangeValues} />
|
<MacroArgumentList args={args} onChangeValues={onChangeValues} namePrefix={namePrefix} />
|
||||||
</Form>
|
</Form>
|
||||||
</Formik>
|
</Formik>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -38,10 +38,11 @@ const macros = [
|
|||||||
type: 'select',
|
type: 'select',
|
||||||
options: ['toUpper', 'toLower', 'lowerCase', 'upperCase', 'kebabCase', 'snakeCase', 'camelCase', 'startCase'],
|
options: ['toUpper', 'toLower', 'lowerCase', 'upperCase', 'kebabCase', 'snakeCase', 'camelCase', 'startCase'],
|
||||||
label: 'Type',
|
label: 'Type',
|
||||||
name: 'caseTransform',
|
name: 'type',
|
||||||
|
default: 'toUpper',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
code: `return modules.lodash[args.caseTransform || 'toUpper'](value)`,
|
code: `return modules.lodash[args.type](value)`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Row index',
|
title: 'Row index',
|
||||||
@@ -65,10 +66,11 @@ const macros = [
|
|||||||
{ value: 'uuidv4', name: 'V4 - random generated' },
|
{ value: 'uuidv4', name: 'V4 - random generated' },
|
||||||
],
|
],
|
||||||
label: 'Version',
|
label: 'Version',
|
||||||
name: 'uuidVersion',
|
name: 'version',
|
||||||
|
default: 'uuidv1',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
code: `return modules[args.uuidVersion || 'uuidv1']()`,
|
code: `return modules[args.version]()`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Current date',
|
title: 'Current date',
|
||||||
@@ -80,10 +82,11 @@ const macros = [
|
|||||||
{
|
{
|
||||||
type: 'text',
|
type: 'text',
|
||||||
label: 'Format',
|
label: 'Format',
|
||||||
name: 'dateFormat',
|
name: 'format',
|
||||||
|
default: 'YYYY-MM-DD HH:mm:ss',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
code: `return modules.moment().format(args.dateFormat || 'YYYY-MM-DD HH:mm:ss')`,
|
code: `return modules.moment().format(args.format)`,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user