Skip to content

Commit

Permalink
Validate element names.
Browse files Browse the repository at this point in the history
  • Loading branch information
supnate committed Feb 24, 2018
1 parent 990c849 commit 522ae4c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 0 additions & 1 deletion packages/rekit-studio/src/features/home/ProjectExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ export class ProjectExplorer extends Component {
break;
case 'new-file':
case 'new-folder':
console.log(this.cmdContext);
this.props.actions.showCmdDialog('cmd', {
type: evt.key,
...this.cmdContext,
Expand Down
3 changes: 2 additions & 1 deletion packages/rekit-studio/src/features/rekit-cmds/CmdForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ export class CmdForm extends Component {
label = meta.label;
}

const rules = [];
const rules = [...(meta.rules || [])];
if (meta.required) {
rules.push({
required: true,
message: `${meta.label} is required.`,
});
}

return (
<Form.Item
key={meta.key}
Expand Down
24 changes: 19 additions & 5 deletions packages/rekit-studio/src/features/rekit-cmds/cmdFormHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ import _ from 'lodash';

const baseMeta = {
feature: { label: 'Feature', key: 'feature', type: 'string', widget: 'feature', required: true },
name: { label: 'Name', key: 'name', type: 'string', widget: 'textbox', required: true },
name: {
label: 'Name',
key: 'name',
type: 'string',
widget: 'textbox',
required: true,
},
checkbox: { type: 'bool', widget: 'checkbox' },
textbox: { type: 'string', widget: 'textbox' },
};

// NOTE: autoFocus only supports textbox now
const elementNameRules = [{ pattern: /^[a-zA-Z_]/, message: 'Name should start with letter or _.' }];
export function getMeta(cmdType, cmdArgs) {
const meta = {};
const fields = [];
switch (cmdType) {
case 'add-feature':
fields.push({ ...baseMeta.name, autoFocus: true });
fields.push({ ...baseMeta.name, autoFocus: true, rules: elementNameRules });
break;
case 'add-action':
fields.push(
{ ...baseMeta.feature, initialValue: cmdArgs.feature || null },
{ ...baseMeta.name, autoFocus: true },
{ ...baseMeta.name, autoFocus: true, rules: elementNameRules },
{
...baseMeta.checkbox,
label: 'Async',
Expand All @@ -30,7 +37,7 @@ export function getMeta(cmdType, cmdArgs) {
case 'add-component':
fields.push(
{ ...baseMeta.feature, initialValue: cmdArgs.feature || null },
{ ...baseMeta.name, autoFocus: true },
{ ...baseMeta.name, autoFocus: true, rules: elementNameRules },
{
...baseMeta.checkbox,
label: 'Connect to store',
Expand All @@ -54,7 +61,14 @@ export function getMeta(cmdType, cmdArgs) {
key: 'targetFeature',
label: 'Target feature',
},
{ ...baseMeta.name, autoFocus: true, initialValue: cmdArgs.elementName, key: 'newName', label: 'New name' }
{
...baseMeta.name,
autoFocus: true,
initialValue: cmdArgs.elementName,
key: 'newName',
label: 'New name',
rules: /action|component|feature/.test(cmdArgs.elementType) ? elementNameRules : null,
}
);
break;
case 'new-folder':
Expand Down

0 comments on commit 522ae4c

Please sign in to comment.