-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
秋逢
committed
Nov 15, 2019
1 parent
1b184a6
commit bed060f
Showing
7 changed files
with
300 additions
and
63 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/react-schema-editor/src/components/FieldEditor.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.field-group{ | ||
display: flex; | ||
margin-bottom: 5px; | ||
} | ||
.field-group .field-input:not(:first-of-type){ | ||
margin-left: 5px; | ||
} | ||
.field-group .op-btn-delete{ | ||
margin-left: 5px; | ||
} |
284 changes: 232 additions & 52 deletions
284
packages/react-schema-editor/src/components/FieldEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,248 @@ | ||
import { Input, Select, Checkbox, Button, Icon } from '@alifd/next' | ||
import { Input, Select, Button } from '@alifd/next' | ||
import React from 'react' | ||
import { IFieldEditorProps } from '../utils/types' | ||
import './FieldEditor.css' | ||
|
||
const typeOptions = [ | ||
const types = [ | ||
{ label: '字符串', value: 'string' }, | ||
{ label: '布尔值', value: 'boolean' }, | ||
{ label: '数字', value: 'number' }, | ||
{ label: '数组', value: 'array' }, | ||
{ label: '对象', value: 'object' } | ||
] | ||
|
||
export const FieldEditor: React.FC = ({ children }) => { | ||
const getComponentNames = components => { | ||
return components.map(({ name }) => name) | ||
} | ||
|
||
const getComponentByComponentName = (componentName: string, components) => { | ||
return components.find(({ name }) => name === componentName) | ||
} | ||
|
||
const getXPropsByComponentName = (componentName: string, components) => { | ||
const component = getComponentByComponentName(componentName, components) | ||
if (!component) { | ||
return {} | ||
} | ||
return component['x-props'] | ||
} | ||
|
||
const getXComponentPropsByComponentName = ( | ||
componentName: string, | ||
components | ||
) => { | ||
const component = getComponentByComponentName(componentName, components) | ||
if (!component) { | ||
return [] | ||
} | ||
return component['x-component-props'] | ||
} | ||
|
||
export const FieldEditor: React.FC<IFieldEditorProps> = ({ | ||
schema, | ||
components, | ||
xRules, | ||
onChange | ||
}) => { | ||
const componentName = schema['x-component'] | ||
const componentNames = getComponentNames(components) | ||
const xProps = getXPropsByComponentName(componentName, components) | ||
const xComponentProps = getXComponentPropsByComponentName( | ||
componentName, | ||
components | ||
) | ||
|
||
const handleChange = (property, value) => { | ||
onChange({ | ||
...schema, | ||
...{ | ||
[property]: value | ||
} | ||
}) | ||
} | ||
|
||
return ( | ||
<div className="field-editor"> | ||
<div className="field-operate"> | ||
<Button className="op-btn op-btn-add" size="small"> | ||
+ | ||
</Button> | ||
<Button className="op-btn op-btn-delete" size="small"> | ||
- | ||
</Button> | ||
<Button className="op-btn op-btn-delete" size="small"> | ||
<Icon type="set" /> | ||
</Button> | ||
<div> | ||
<h3>字段</h3> | ||
<div className="field-group"> | ||
<Input | ||
className="field-input" | ||
label="label" | ||
size="small" | ||
value={schema.title} | ||
onChange={value => { | ||
handleChange('title', value) | ||
}} | ||
/> | ||
<Select | ||
className="field-input" | ||
label="类型" | ||
size="small" | ||
dataSource={types} | ||
value={schema.type} | ||
onChange={value => { | ||
handleChange('type', value) | ||
}} | ||
/> | ||
<Select | ||
className="field-input" | ||
label="组件" | ||
size="small" | ||
dataSource={componentNames} | ||
style={{ width: 300 }} | ||
value={componentName} | ||
onChange={value => { | ||
handleChange('x-component', value) | ||
}} | ||
/> | ||
<Input | ||
className="field-input" | ||
label="描述" | ||
size="small" | ||
value={schema.description} | ||
onChange={value => { | ||
handleChange('description', value) | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<h3>x-props</h3> | ||
{Object.keys(schema['x-props']).map(property => ( | ||
<div className="field-group" key={property}> | ||
<Select.AutoComplete | ||
className="field-input" | ||
label="属性" | ||
size="small" | ||
dataSource={Object.keys(xProps)} | ||
style={{ width: 150 }} | ||
value={property} | ||
/> | ||
<Select | ||
className="field-input" | ||
label="类型" | ||
name="x-props-type" | ||
size="small" | ||
dataSource={types} | ||
value={schema['x-props'][property].constructor.name.toLowerCase()} | ||
/> | ||
<Input | ||
className="field-input" | ||
label="值" | ||
name="x-props-value" | ||
size="small" | ||
value={schema['x-props'][property]} | ||
/> | ||
<Button className="op-btn op-btn-delete" size="small"> | ||
- | ||
</Button> | ||
</div> | ||
))} | ||
|
||
<div> | ||
<Button | ||
className="op-btn op-btn-add" | ||
size="small" | ||
onClick={() => { | ||
handleChange('x-props', { | ||
...schema['x-props'], | ||
...{ | ||
'': '' | ||
} | ||
}) | ||
}} | ||
> | ||
+ | ||
</Button> | ||
</div> | ||
</div> | ||
<div className="field-group"> | ||
<Input | ||
className="field-input" | ||
label="key" | ||
name="key" | ||
size="small" | ||
></Input> | ||
<Input | ||
className="field-input" | ||
label="title" | ||
name="title" | ||
size="small" | ||
></Input> | ||
<Select | ||
className="field-input" | ||
label="type" | ||
name="type" | ||
size="small" | ||
dataSource={typeOptions} | ||
></Select> | ||
<Select | ||
className="field-input" | ||
label="component" | ||
name="component" | ||
size="small" | ||
></Select> | ||
<Input | ||
className="field-input" | ||
label="desc" | ||
name="description" | ||
size="small" | ||
></Input> | ||
<Checkbox | ||
className="field-input" | ||
label="必填" | ||
name="required" | ||
></Checkbox> | ||
|
||
<div> | ||
<h3>x-component-props</h3> | ||
{Object.keys(schema['x-component-props']).map(property => ( | ||
<div className="field-group" key={property}> | ||
<Select | ||
className="field-input" | ||
label="属性" | ||
size="small" | ||
dataSource={Object.keys(xComponentProps)} | ||
style={{ width: 150 }} | ||
value={property} | ||
/> | ||
<Select | ||
className="field-input" | ||
label="类型" | ||
size="small" | ||
dataSource={types} | ||
value={schema['x-component-props'][ | ||
property | ||
].constructor.name.toLowerCase()} | ||
/> | ||
<Input | ||
className="field-input" | ||
label="值" | ||
size="small" | ||
value={schema['x-component-props'][property]} | ||
/> | ||
<Button className="op-btn op-btn-delete" size="small"> | ||
- | ||
</Button> | ||
</div> | ||
))} | ||
|
||
<div> | ||
<Button | ||
className="op-btn op-btn-add" | ||
size="small" | ||
onClick={() => { | ||
handleChange('x-props', { | ||
...schema['x-props'], | ||
...{ | ||
'': '' | ||
} | ||
}) | ||
}} | ||
> | ||
+ | ||
</Button> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<h3>x-rules</h3> | ||
<div className="field-group"> | ||
<Select | ||
className="field-input" | ||
label="属性" | ||
name="xRules" | ||
size="small" | ||
dataSource={xRules} | ||
/> | ||
<Select | ||
className="field-input" | ||
label="类型" | ||
name="x-component-props-type" | ||
size="small" | ||
dataSource={types} | ||
/> | ||
<Input | ||
className="field-input" | ||
label="值" | ||
name="x-component-props-value" | ||
size="small" | ||
/> | ||
</div> | ||
<div> | ||
<Button className="op-btn op-btn-add" size="small"> | ||
+ | ||
</Button> | ||
<Button className="op-btn op-btn-delete" size="small"> | ||
- | ||
</Button> | ||
</div> | ||
</div> | ||
<div className="props-group"></div> | ||
<div className="field-children">{children}</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.