Skip to content

Commit

Permalink
Fix destructor (alibaba#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Dec 15, 2019
1 parent 0b061bb commit 4d82891
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const SchemaMarkupField: React.FC<IMarkupSchemaFieldProps> = ({
const parentSchema = useContext(MarkupContext)
if (!parentSchema) return <Fragment />
if (parentSchema.isObject()) {
const propName = name || getRadomName()
const propName = (name || getRadomName()).replace(/\s*/g, '')
const schema = parentSchema.setProperty(propName, props)
return (
<MarkupContext.Provider value={schema}>{children}</MarkupContext.Provider>
Expand Down Expand Up @@ -80,7 +80,7 @@ export function createVirtualBox<T = {}>(
const props = schema.getExtendsComponentProps()
return React.createElement(component, {
children,
...props,
...props
})
}
: () => <Fragment />
Expand Down
42 changes: 39 additions & 3 deletions packages/react-schema-renderer/src/shared/linkage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
import { JSONCondition } from '../shared/condition'
import { FormPath, IFormEffect } from '@uform/react'
import { isFn } from '@uform/shared'
import { isFn, isStr } from '@uform/shared'
import { ISchemaFormActions } from '../types'
import { compileObject } from './expression'

const pathExpRE = /\[\s*(?:([+-])\s*(\d+)?)?\s*\]/g

const transformTargetPath = (target: string, indexes: number[]) => {
if (!isStr(target)) return target
let index = 0
const newTarget = target.replace(
pathExpRE,
(_: string, operator: string, delta: string) => {
if (delta) {
if (operator === '+') {
return String(indexes[index++] + Number(delta))
} else if (operator === '-') {
return String(indexes[index++] - Number(delta))
}
} else {
if (operator === '+') {
return String(indexes[index++] + 1)
} else if (operator === '-') {
return String(indexes[index++] - 1)
}
}
return String(indexes[index++])
}
)
pathExpRE.lastIndex = 0
return newTarget
}

const getPathIndexes = (path: any): number[] => {
return path.transform(/\d+/, (...args) => args.map(i => Number(i))) as any
}

export const presetLinkage = (
resolve: (params: any, actions: ISchemaFormActions) => void,
reject?: (params: any, actions: ISchemaFormActions) => void
Expand All @@ -12,9 +44,12 @@ export const presetLinkage = (
const { name, target, triggerType = 'onFieldChange' } = params
const { hasChanged, getFormState, getFieldState } = actions
$(triggerType, name).subscribe(fieldState => {
const fieldName = FormPath.parse(fieldState.name)
const fieldIndexes = getPathIndexes(fieldName)
const formState = getFormState()
const targetState = getFieldState(target)
const fieldValue = FormPath.parse(name).getIn(formState.values)
const newTarget = transformTargetPath(target, fieldIndexes)
const targetState = getFieldState(newTarget)
const fieldValue = fieldName.getIn(formState.values)
const options = compileObject(params, {
...context,
$value: fieldValue,
Expand All @@ -23,6 +58,7 @@ export const presetLinkage = (
$target: targetState || {}
})
options.condition = JSONCondition.calculate(options.condition, fieldValue)
options.target = newTarget
if (
hasChanged(fieldState, 'value') ||
hasChanged(fieldState, 'initialized') ||
Expand Down
2 changes: 1 addition & 1 deletion packages/react-schema-renderer/src/shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Schema implements ISchema {
this.key = key
}
if (this.parent && this.parent.isArray()) {
this.path = this.parent.path + '.*.' + this.key
this.path = this.parent.path + '.*'
} else {
if (this.parent) {
this.path = this.parent.path
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ export const createFormEffects = <Payload = any, Actions = any>(
isFn(matcher) && !matcher['path']
? matcher
: (payload: T): boolean => {
return FormPath.parse(matcher as any).match(
payload && (payload as any).name
return FormPath.parse(matcher as any).matchAliasGroup(
payload && (payload as any).name,
payload && (payload as any).path
)
}
)
Expand Down

0 comments on commit 4d82891

Please sign in to comment.