@tomino/dynamic-form-semantic-ui
Version:
Semantic UI form renderer based on dynamic form generation
63 lines • 2.44 kB
JavaScript
import { bindGetValue } from '../helpers';
export function findConflict(props, context, cells, start, end) {
const getValue = bindGetValue(props, context);
return cells.find(e => (getValue(e, 'column') >= start && getValue(e, 'column') <= end) ||
(getValue(e, 'column') + getValue(e, 'width') - 1 >= start &&
getValue(e, 'column') + getValue(e, 'width') - 1 <= end));
}
export function adjustPosition(props, context, where, source, target, parent) {
const getValue = bindGetValue(props, context);
if (!parent) {
return getValue(target, 'column');
}
let dimensions = parent.elements.reduce((prev, next) => ({
rows: prev.rows < next.props.row ? next.props.row : prev.rows,
columns: prev.columns < next.props.column ? next.props.column : prev.columns
}), { rows: 0, columns: 0 });
let column = where === 'left'
? getValue(target, 'column')
: getValue(target, 'column') - getValue(target, 'width') + 1;
column = column < 1 ? 1 : column;
column =
column + source.props.width > dimensions.columns
? dimensions.columns - getValue(source, 'width') + 1
: column;
let cells = parent.elements.filter(e => e.props.row === target.props.row && e !== source);
let conflict = findConflict(props, context, cells, column, column + source.props.width - 1);
if (conflict) {
return -1;
}
return column;
}
export function bind(formElement, item) {
if (formElement.parent) {
let parent = formElement.parent;
let parentPath = '';
while (parent) {
if (parent.props.value && parent.props.value.source) {
parentPath = parentPath + parent.props.value + '.';
}
parent = parent.parent;
}
let path = item.name.replace(parentPath, '');
formElement.setValue('source', path);
return;
}
const schema = item.schema;
item.label = item.name;
item.source = item.name;
if (schema.type === 'boolean') {
item.name = 'Checkbox';
}
else if (schema.type === 'array') {
item.name = 'Table';
}
else if (schema.type === 'number' || schema.type === 'integer') {
item.name = 'Input';
item.controlProps = { type: 'number' };
}
else if (schema.type === 'string') {
item.name = 'Input';
}
}
//# sourceMappingURL=editor_helpers.js.map