( props: P, stateMap: (state: JsonFormsState, props: P) => R, dispatchMap?: (dispatch: Dispatch<CoreActions>) => D )
| 176 | // TODO fix @typescript-eslint/ban-types |
| 177 | // eslint-disable-next-line @typescript-eslint/ban-types |
| 178 | export function useControl< |
| 179 | R, |
| 180 | D, |
| 181 | P extends { schema: JsonSchema; uischema: UISchemaElement & Scopable } |
| 182 | >( |
| 183 | props: P, |
| 184 | stateMap: (state: JsonFormsState, props: P) => R, |
| 185 | dispatchMap?: (dispatch: Dispatch<CoreActions>) => D |
| 186 | ) { |
| 187 | const jsonforms = useJsonForms(); |
| 188 | const dispatch = useDispatch(); |
| 189 | |
| 190 | const id = ref<string | undefined>(undefined); |
| 191 | const control = computed(() => ({ |
| 192 | ...props, |
| 193 | ...stateMap({ jsonforms }, props), |
| 194 | id: id.value, |
| 195 | })); |
| 196 | |
| 197 | const dispatchMethods = dispatchMap?.(dispatch); |
| 198 | |
| 199 | onBeforeMount(() => { |
| 200 | if (control.value.uischema.scope) { |
| 201 | id.value = Id.createId(control.value.uischema.scope); |
| 202 | } |
| 203 | }); |
| 204 | |
| 205 | watch( |
| 206 | () => props.schema, |
| 207 | (newSchema, prevSchem) => { |
| 208 | if (newSchema !== prevSchem && isControl(control.value.uischema)) { |
| 209 | if (id.value) { |
| 210 | Id.removeId(id.value); |
| 211 | } |
| 212 | id.value = Id.createId(control.value.uischema.scope); |
| 213 | } |
| 214 | } |
| 215 | ); |
| 216 | |
| 217 | onUnmounted(() => { |
| 218 | if (id.value) { |
| 219 | Id.removeId(id.value); |
| 220 | id.value = undefined; |
| 221 | } |
| 222 | }); |
| 223 | |
| 224 | return { |
| 225 | control: control, |
| 226 | ...dispatchMethods, |
| 227 | }; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Provides generic bindings for 'Control' elements. |
no test coverage detected
searching dependent graphs…