|
1 | 1 | import { |
2 | | - createContext, useContext, useEffect, useId, useState, type ReactNode, |
| 2 | + createContext, useContext, useEffect, useId, useMemo, useState, type ReactNode, |
3 | 3 | } from "react"; |
4 | 4 | import isEqual from "lodash.isequal"; |
5 | 5 | import { useQuery } from "@tanstack/react-query"; |
@@ -101,7 +101,7 @@ export const FormContextProvider = <T extends ConfigurableProps>({ |
101 | 101 | const [ |
102 | 102 | sdkErrors, |
103 | 103 | setSdkErrors, |
104 | | - ] = useState<SdkError[]>([]) |
| 104 | + ] = useState<SdkError[]>([]); |
105 | 105 |
|
106 | 106 | const [ |
107 | 107 | enabledOptionalProps, |
@@ -187,20 +187,30 @@ export const FormContextProvider = <T extends ConfigurableProps>({ |
187 | 187 | ]); |
188 | 188 |
|
189 | 189 | // XXX fix types of dynamicProps, props.component so this type decl not needed |
190 | | - let configurableProps: T = dynamicProps?.configurableProps || formProps.component.configurable_props || []; |
191 | | - if (propNames?.length) { |
192 | | - const _configurableProps = []; |
193 | | - for (const prop of configurableProps) { |
194 | | - // TODO decided propNames (and hideOptionalProps) should NOT filter dynamic props |
195 | | - if (propNames.findIndex((name) => prop.name === name) >= 0) { |
196 | | - _configurableProps.push(prop); |
| 190 | + const configurableProps = useMemo<T>(() => { |
| 191 | + let props: T = dynamicProps?.configurableProps || formProps.component.configurable_props || []; |
| 192 | + if (propNames?.length) { |
| 193 | + const _configurableProps = []; |
| 194 | + for (const prop of props) { |
| 195 | + // TODO decided propNames (and hideOptionalProps) should NOT filter dynamic props |
| 196 | + if (propNames.findIndex((name) => prop.name === name) >= 0) { |
| 197 | + _configurableProps.push(prop); |
| 198 | + } |
197 | 199 | } |
| 200 | + props = _configurableProps as unknown as T; // XXX |
198 | 201 | } |
199 | | - configurableProps = _configurableProps as unknown as T; // XXX |
200 | | - } |
201 | | - if (reloadPropIdx != null) { |
202 | | - configurableProps = configurableProps.slice(0, reloadPropIdx + 1) as unknown as T; // XXX |
203 | | - } |
| 202 | + if (reloadPropIdx != null) { |
| 203 | + props = Array.isArray(props) |
| 204 | + ? props.slice(0, reloadPropIdx + 1) as unknown as T // eslint-disable-line react/prop-types |
| 205 | + : props; // XXX |
| 206 | + } |
| 207 | + return props; |
| 208 | + }, [ |
| 209 | + dynamicProps?.configurableProps, |
| 210 | + formProps.component.configurable_props, |
| 211 | + propNames, |
| 212 | + reloadPropIdx, |
| 213 | + ]); |
204 | 214 |
|
205 | 215 | // these validations are necessary because they might override PropInput for number case for instance |
206 | 216 | // so can't rely on that base control form validation |
|
0 commit comments