(
options: UseAsterDataBindingOptions = {},
)
| 71 | * ``` |
| 72 | */ |
| 73 | export function useAsterDataBinding( |
| 74 | options: UseAsterDataBindingOptions = {}, |
| 75 | ): UseAsterDataBindingReturn { |
| 76 | const { propertyValue, defaultValue = null, debounceMs = 0 } = options; |
| 77 | |
| 78 | // Try to inject context from AsterSurface |
| 79 | const context = inject<DataBindingContext | null>(DATA_BINDING_CONTEXT_KEY, null); |
| 80 | |
| 81 | // Determine if this is a path reference |
| 82 | const isPath = propertyValue ? isPathReference(propertyValue) : false; |
| 83 | const path = isPath && propertyValue ? (propertyValue as { path: string }).path : null; |
| 84 | |
| 85 | // Debounce timer |
| 86 | let debounceTimer: ReturnType<typeof setTimeout> | null = null; |
| 87 | |
| 88 | // Computed value that resolves the property |
| 89 | const value = computed<DataValue | undefined>(() => { |
| 90 | if (!propertyValue) { |
| 91 | return defaultValue ?? undefined; |
| 92 | } |
| 93 | |
| 94 | // Literal values |
| 95 | if ('literalString' in propertyValue) { |
| 96 | return propertyValue.literalString; |
| 97 | } |
| 98 | if ('literalNumber' in propertyValue) { |
| 99 | return propertyValue.literalNumber; |
| 100 | } |
| 101 | if ('literalBoolean' in propertyValue) { |
| 102 | return propertyValue.literalBoolean; |
| 103 | } |
| 104 | |
| 105 | // Path reference - get from data model |
| 106 | if ('path' in propertyValue && context?.dataModel.value) { |
| 107 | const result = getData(context.dataModel.value, propertyValue.path); |
| 108 | return result ?? defaultValue ?? undefined; |
| 109 | } |
| 110 | |
| 111 | return defaultValue ?? undefined; |
| 112 | }); |
| 113 | |
| 114 | /** |
| 115 | * Update the value in the data model |
| 116 | */ |
| 117 | function updateValue(newValue: DataValue): void { |
| 118 | if (!isPath || !path || !context) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | const doUpdate = () => { |
| 123 | if (context.processor.value) { |
| 124 | context.processor.value.setData(context.surfaceId, path, newValue); |
| 125 | } |
| 126 | else if (context.dataModel.value) { |
| 127 | setData(context.dataModel.value, path, newValue); |
| 128 | } |
| 129 | }; |
| 130 |
no test coverage detected