Skip to content

Commit

Permalink
feat(reactive): computed annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Mar 5, 2021
1 parent 917a7cb commit b9e6f09
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ export class Field<
componentProps: observable.shallow,
validator: observable,
feedbacks: observable,
component: observable.computed,
decorator: observable.computed,
errors: observable.computed,
warnings: observable.computed,
successes: observable.computed,
valid: observable.computed,
invalid: observable.computed,
validateStatus: observable.computed,
value: observable.computed,
initialValue: observable.computed,
display: observable.computed,
pattern: observable.computed,
required: observable.computed,
hidden: observable.computed,
visible: observable.computed,
disabled: observable.computed,
readOnly: observable.computed,
readPretty: observable.computed,
editable: observable.computed,
setDisplay: batch,
setTitle: batch,
setDescription: batch,
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/models/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ export class Form<ValueType extends object = any> {
unmounted: observable.ref,
values: observable,
initialValues: observable,
valid: observable.computed,
invalid: observable.computed,
errors: observable.computed,
warnings: observable.computed,
successes: observable.computed,
hidden: observable.computed,
visible: observable.computed,
editable: observable.computed,
readOnly: observable.computed,
readPretty: observable.computed,
disabled: observable.computed,
setValues: batch,
setValuesIn: batch,
setInitialValues: batch,
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/models/VoidField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ export class VoidField<Decorator = any, Component = any, TextType = any> {
decoratorProps: observable.shallow,
componentType: observable.ref,
componentProps: observable.shallow,
display: observable.computed,
pattern: observable.computed,
hidden: observable.computed,
visible: observable.computed,
disabled: observable.computed,
readOnly: observable.computed,
readPretty: observable.computed,
editable: observable.computed,
component: observable.computed,
decorator: observable.computed,
setTitle: batch,
setDescription: batch,
setDisplay: batch,
Expand Down
28 changes: 22 additions & 6 deletions packages/reactive/src/annotations/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,28 @@ export const computed: IComputed = createAnnotation(

const context = target ? target : store
const property = target ? key : 'value'
const getter = target
? Reflect.getOwnPropertyDescriptor(target, key)?.get
: value
const setter = target
? Reflect.getOwnPropertyDescriptor(target, key)?.set
: value?.set
const getter = getGetter(context)
const setter = getSetter(context)

function getGetter(target: any) {
if (!target) {
if (value?.get) return value?.get
return value
}
const descriptor = Reflect.getOwnPropertyDescriptor(target, property)
if (descriptor?.get) return descriptor.get
return getGetter(Object.getPrototypeOf(target))
}

function getSetter(target: any) {
if (!target) {
if (value?.set) return value?.set
return
}
const descriptor = Object.getOwnPropertyDescriptor(target, property)
if (descriptor?.set) return descriptor.set
return getSetter(Object.getPrototypeOf(target))
}

function compute() {
const oldValue = store.value
Expand Down

0 comments on commit b9e6f09

Please sign in to comment.