Skip to content

Commit

Permalink
feat: 表达式value
Browse files Browse the repository at this point in the history
  • Loading branch information
秋逢 committed Nov 28, 2019
1 parent e9f2c04 commit 73c9091
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
37 changes: 28 additions & 9 deletions packages/react-schema-editor/src/components/FieldEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getComponentPropsValue,
getInputType,
getPropertyValue,
getExpressionValue,
getRuleMessage
} from '../utils/fieldEditorHelpers'
import { InputTypes, ComponentPropsTypes } from '../utils/types'
Expand Down Expand Up @@ -60,7 +61,7 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
const componentPropsValue = getComponentPropsValue({ schema, propsKey })

const handleXComponentPropsValueChange = (value, property) => {
const newSchema = { ...schema }
let newSchema
if (propsKey === ComponentPropsTypes.X_RULES) {
const newRules = _.map(schema[propsKey], rule => {
if (_.has(rule, property)) {
Expand All @@ -71,15 +72,24 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
}
return rule
})
_.set(newSchema, `${propsKey}`, newRules)
newSchema = {
...schema,
[propsKey]: newRules
}
} else {
_.set(newSchema, `${propsKey}.${property}`, value)
newSchema = {
...schema,
[propsKey]: {
...schema[propsKey],
[property]: value
}
}
}
onChange(newSchema)
}

const handleInputTypeChange = (value, property) => {
const newSchema = { ...schema }
let newSchema
let defaultValue
switch (value) {
case InputTypes.INPUT: {
Expand Down Expand Up @@ -109,9 +119,18 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
}
return rule
})
_.set(newSchema, `${propsKey}`, newRules)
newSchema = {
...schema,
[propsKey]: newRules
}
} else {
_.set(newSchema, `${propsKey}.${property}`, defaultValue)
newSchema = {
...schema,
[propsKey]: {
...schema[propsKey],
[property]: defaultValue
}
}
}
onChange(newSchema)
}
Expand Down Expand Up @@ -305,9 +324,9 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
className="field-group-form-item"
>
<Input.TextArea
placeholder="{{}}/{}/[]"
// TODO, 这里暂时用 defaultValue
defaultValue={value}
key={getExpressionValue(value)}
placeholder="格式:{{}}/{}/[],失去焦点生效"
defaultValue={getExpressionValue(value)}
onBlur={event => {
let value = event.target.value
try {
Expand Down
1 change: 1 addition & 0 deletions packages/react-schema-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const components = [
export const SchemaEditor: React.FC = () => {
const initialSchema = jsonToSchema(json)
const [schema, setSchema] = React.useState(initialSchema)

return (
<div className="schema-editor">
<div className="schema-menus">
Expand Down
6 changes: 6 additions & 0 deletions packages/react-schema-editor/src/utils/fieldEditorHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ const isExpression = value => {
return /^{{(.*)}}$/.test(value)
}

export const getExpressionValue = value => {
if (_.isObject(value)) {
return JSON.stringify(value)
}
return value
}
export const getInputType = value => {
if (typeof value === 'object' || isExpression(value)) {
return InputTypes.TEXT_AREA
Expand Down
3 changes: 2 additions & 1 deletion packages/react-schema-editor/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default {
'x-props': {
help: '不得超过6个汉字',
validateStatus: 'success',
labelCol: [{ col: 2 }],
extra: null
},
'x-component': 'Input',
'x-component-props': {
Expand All @@ -19,7 +21,6 @@ export default {
message: '此项必填'
},
{
//TODO
pattern: '[\\u4e00-\\u9fa5]',
message: '正则'
},
Expand Down

0 comments on commit 73c9091

Please sign in to comment.