Skip to content

Commit

Permalink
refactor: 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
秋逢 committed Nov 26, 2019
1 parent fa6215d commit e9f2c04
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
43 changes: 12 additions & 31 deletions packages/react-schema-editor/src/components/FieldEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({

const componentPropsValue = getComponentPropsValue({ schema, propsKey })

const handleXComponentPropsValueChange = (
value,
property,
schema,
propsKey
) => {
const handleXComponentPropsValueChange = (value, property) => {
const newSchema = { ...schema }
if (propsKey === ComponentPropsTypes.X_RULES) {
const newRules = _.map(schema[propsKey], rule => {
Expand All @@ -83,10 +78,10 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
onChange(newSchema)
}

const handleInputTypeChange = (inputType, property, schema, propsKey) => {
const handleInputTypeChange = (value, property) => {
const newSchema = { ...schema }
let defaultValue
switch (inputType) {
switch (value) {
case InputTypes.INPUT: {
defaultValue = ''
break
Expand Down Expand Up @@ -230,7 +225,7 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
handlePropertyChange(value, property)
}}
>
{componentPropsData.options.map(({ label, value }) => (
{_.map(componentPropsData.options, ({ label, value }) => (
<SelectOption value={value} key={value}>
{label}
</SelectOption>
Expand All @@ -245,10 +240,10 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
<Select
value={inputType}
onChange={value => {
handleInputTypeChange(value, property, schema, propsKey)
handleInputTypeChange(value, property)
}}
>
{inputTypeData.options.map(({ label, value }) => (
{_.map(inputTypeData.options, ({ label, value }) => (
<SelectOption value={value} key={value}>
{label}
</SelectOption>
Expand All @@ -266,9 +261,7 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
onChange={event => {
handleXComponentPropsValueChange(
event.target.value,
property,
schema,
propsKey
property
)
}}
/>
Expand All @@ -283,12 +276,7 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
<InputNumber
value={value}
onChange={value => {
handleXComponentPropsValueChange(
value,
property,
schema,
propsKey
)
handleXComponentPropsValueChange(value, property)
}}
/>
</FormItem>
Expand All @@ -304,9 +292,7 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
onChange={event => {
handleXComponentPropsValueChange(
event.target.checked,
property,
schema,
propsKey
property
)
}}
/>
Expand All @@ -327,12 +313,7 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
try {
value = JSON.parse(value)
} catch (error) {}
handleXComponentPropsValueChange(
value,
property,
schema,
propsKey
)
handleXComponentPropsValueChange(value, property)
}}
/>
</FormItem>
Expand Down Expand Up @@ -409,7 +390,7 @@ const FieldEditor: React.FC<IFieldEditorProps> = ({
})
}}
>
{fieldTypeData.options.map(({ label, value }) => (
{_.map(fieldTypeData.options, ({ label, value }) => (
<SelectOption value={value} key={value}>
{label}
</SelectOption>
Expand All @@ -431,7 +412,7 @@ const FieldEditor: React.FC<IFieldEditorProps> = ({
})
}}
>
{xComponentData.options.map(({ label, value }) => (
{_.map(xComponentData.options, ({ label, value }) => (
<SelectOption value={value} key={value}>
{label}
</SelectOption>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const convertKeysToSelectData = keys => {
}

export const getXComponentData = components => {
return convertKeysToSelectData(_.keys(components))
return convertKeysToSelectData(_.map(components, ({ name }) => name))
}

export const getRemainingKeys = (allKeys, usedKeys) => {
Expand Down

0 comments on commit e9f2c04

Please sign in to comment.