@rjsf/core 5.23.2 → 5.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3320,10 +3320,12 @@ var Form = class extends import_react17.Component {
3320
3320
  */
3321
3321
  this.onChange = (formData, newErrorSchema, id) => {
3322
3322
  const { extraErrors, omitExtraData, liveOmit, noValidate, liveValidate, onChange } = this.props;
3323
- const { schemaUtils, schema, retrievedSchema } = this.state;
3323
+ const { schemaUtils, schema } = this.state;
3324
+ let retrievedSchema = this.state.retrievedSchema;
3324
3325
  if ((0, import_utils39.isObject)(formData) || Array.isArray(formData)) {
3325
- const newState = this.getStateFromProps(this.props, formData, retrievedSchema);
3326
+ const newState = this.getStateFromProps(this.props, formData);
3326
3327
  formData = newState.formData;
3328
+ retrievedSchema = newState.retrievedSchema;
3327
3329
  }
3328
3330
  const mustValidate = !noValidate && liveValidate;
3329
3331
  let state = { formData, schema };
@@ -3540,8 +3542,9 @@ var Form = class extends import_react17.Component {
3540
3542
  */
3541
3543
  getSnapshotBeforeUpdate(prevProps, prevState) {
3542
3544
  if (!(0, import_utils39.deepEquals)(this.props, prevProps)) {
3545
+ const formDataChangedFields = (0, import_utils39.getChangedFields)(this.props.formData, prevProps.formData);
3543
3546
  const isSchemaChanged = !(0, import_utils39.deepEquals)(prevProps.schema, this.props.schema);
3544
- const isFormDataChanged = !(0, import_utils39.deepEquals)(prevProps.formData, this.props.formData);
3547
+ const isFormDataChanged = formDataChangedFields.length > 0 || !(0, import_utils39.deepEquals)(prevProps.formData, this.props.formData);
3545
3548
  const nextState = this.getStateFromProps(
3546
3549
  this.props,
3547
3550
  this.props.formData,
@@ -3549,7 +3552,8 @@ var Form = class extends import_react17.Component {
3549
3552
  // Or if the `formData` changes, for example in the case of a schema with dependencies that need to
3550
3553
  // match one of the subSchemas, the retrieved schema must be updated.
3551
3554
  isSchemaChanged || isFormDataChanged ? void 0 : this.state.retrievedSchema,
3552
- isSchemaChanged
3555
+ isSchemaChanged,
3556
+ formDataChangedFields
3553
3557
  );
3554
3558
  const shouldUpdate = !(0, import_utils39.deepEquals)(nextState, prevState);
3555
3559
  return { nextState, shouldUpdate };
@@ -3588,9 +3592,10 @@ var Form = class extends import_react17.Component {
3588
3592
  * @param inputFormData - The new or current data for the `Form`
3589
3593
  * @param retrievedSchema - An expanded schema, if not provided, it will be retrieved from the `schema` and `formData`.
3590
3594
  * @param isSchemaChanged - A flag indicating whether the schema has changed.
3595
+ * @param formDataChangedFields - The changed fields of `formData`
3591
3596
  * @returns - The new state for the `Form`
3592
3597
  */
3593
- getStateFromProps(props, inputFormData, retrievedSchema, isSchemaChanged = false) {
3598
+ getStateFromProps(props, inputFormData, retrievedSchema, isSchemaChanged = false, formDataChangedFields = []) {
3594
3599
  const state = this.state || {};
3595
3600
  const schema = "schema" in props ? props.schema : this.props.schema;
3596
3601
  const uiSchema = ("uiSchema" in props ? props.uiSchema : this.props.uiSchema) || {};
@@ -3615,7 +3620,9 @@ var Form = class extends import_react17.Component {
3615
3620
  );
3616
3621
  }
3617
3622
  const formData = schemaUtils.getDefaultFormState(schema, inputFormData);
3618
- const _retrievedSchema = retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData);
3623
+ const _retrievedSchema = this.updateRetrievedSchema(
3624
+ retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData)
3625
+ );
3619
3626
  const getCurrentErrors = () => {
3620
3627
  if (props.noValidate || isSchemaChanged) {
3621
3628
  return { errors: [], errorSchema: {} };
@@ -3652,6 +3659,17 @@ var Form = class extends import_react17.Component {
3652
3659
  const currentErrors = getCurrentErrors();
3653
3660
  errors = currentErrors.errors;
3654
3661
  errorSchema = currentErrors.errorSchema;
3662
+ if (formDataChangedFields.length > 0) {
3663
+ const newErrorSchema = formDataChangedFields.reduce((acc, key) => {
3664
+ acc[key] = void 0;
3665
+ return acc;
3666
+ }, {});
3667
+ errorSchema = schemaValidationErrorSchema = (0, import_utils39.mergeObjects)(
3668
+ currentErrors.errorSchema,
3669
+ newErrorSchema,
3670
+ "preventDuplicates"
3671
+ );
3672
+ }
3655
3673
  }
3656
3674
  if (props.extraErrors) {
3657
3675
  const merged = (0, import_utils39.validationDataMerge)({ errorSchema, errors }, props.extraErrors);
@@ -3745,6 +3763,19 @@ var Form = class extends import_react17.Component {
3745
3763
  };
3746
3764
  return filterNilOrEmptyErrors(filteredErrors);
3747
3765
  }
3766
+ /**
3767
+ * If the retrievedSchema has changed the new retrievedSchema is returned.
3768
+ * Otherwise, the old retrievedSchema is returned to persist reference.
3769
+ * - This ensures that AJV retrieves the schema from the cache when it has not changed,
3770
+ * avoiding the performance cost of recompiling the schema.
3771
+ *
3772
+ * @param retrievedSchema The new retrieved schema.
3773
+ * @returns The new retrieved schema if it has changed, else the old retrieved schema.
3774
+ */
3775
+ updateRetrievedSchema(retrievedSchema) {
3776
+ const isTheSame = (0, import_utils39.deepEquals)(retrievedSchema, this.state?.retrievedSchema);
3777
+ return isTheSame ? this.state.retrievedSchema : retrievedSchema;
3778
+ }
3748
3779
  /** Returns the registry for the form */
3749
3780
  getRegistry() {
3750
3781
  const { translateString: customTranslateString, uiSchema = {} } = this.props;