Skip to content

Commit

Permalink
feat: 规则
Browse files Browse the repository at this point in the history
  • Loading branch information
秋逢 committed Nov 26, 2019
1 parent 459e087 commit fa6215d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 13 deletions.
98 changes: 86 additions & 12 deletions packages/react-schema-editor/src/components/FieldEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,20 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
propsKey
) => {
const newSchema = { ...schema }
_.set(newSchema, `${propsKey}.${property}`, value)
if (propsKey === ComponentPropsTypes.X_RULES) {
const newRules = _.map(schema[propsKey], rule => {
if (_.has(rule, property)) {
return {
...rule,
[property]: value
}
}
return rule
})
_.set(newSchema, `${propsKey}`, newRules)
} else {
_.set(newSchema, `${propsKey}.${property}`, value)
}
onChange(newSchema)
}

Expand All @@ -91,7 +104,20 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
break
}
}
_.set(newSchema, `${propsKey}.${property}`, defaultValue)
if (propsKey === ComponentPropsTypes.X_RULES) {
const newRules = _.map(schema[propsKey], rule => {
if (_.has(rule, property)) {
return {
...rule,
[property]: defaultValue
}
}
return rule
})
_.set(newSchema, `${propsKey}`, newRules)
} else {
_.set(newSchema, `${propsKey}.${property}`, defaultValue)
}
onChange(newSchema)
}

Expand Down Expand Up @@ -124,22 +150,65 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
})
}

const handleMinusClick = property => {
onChange({
..._.omit(schema, `${propsKey}.${property}`)
const handleRuleMessageChange = (value, property) => {
const newRules = _.map(schema[propsKey], rule => {
if (_.has(rule, property)) {
return {
...rule,
message: value
}
}
return rule
})
}

const handlePlusClick = () => {
onChange({
...schema,
[propsKey]: {
...schema[propsKey],
[componentPropsData.defaultValue]: BLANK_PROPERTY_VALUE
}
[ComponentPropsTypes.X_RULES]: newRules
})
}

const handleMinusClick = property => {
if (propsKey === ComponentPropsTypes.X_RULES) {
const newRules = _.reduce(
schema[propsKey],
(result, rule) => {
if (_.has(rule, property)) {
return result
}
return _.concat(result, rule)
},
[]
)
onChange({
...schema,
[ComponentPropsTypes.X_RULES]: newRules
})
} else {
onChange({
..._.omit(schema, `${propsKey}.${property}`)
})
}
}

const handlePlusClick = () => {
if (propsKey === ComponentPropsTypes.X_RULES) {
onChange({
...schema,
[propsKey]: _.concat(schema[propsKey], {
[componentPropsData.defaultValue]: BLANK_PROPERTY_VALUE
})
})
} else {
onChange({
...schema,
[propsKey]: {
...schema[propsKey],
[componentPropsData.defaultValue]: BLANK_PROPERTY_VALUE
}
})
}
}

return (
<div className="field-group">
<div className="field-group-title">{title}</div>
Expand Down Expand Up @@ -274,7 +343,12 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
{...formItemLayout}
className="field-group-form-item"
>
<Input value={getRuleMessage({ schema, property })} />
<Input
value={getRuleMessage({ schema, property })}
onChange={event => {
handleRuleMessageChange(event.target.value, property)
}}
/>
</FormItem>
)}
<FormItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ const isExpression = value => {
}

export const getInputType = value => {
debugger
if (typeof value === 'object' || isExpression(value)) {
return InputTypes.TEXT_AREA
}
Expand Down

0 comments on commit fa6215d

Please sign in to comment.