@spscommerce/ds-react 6.2.2 → 6.3.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/lib/index.es.js CHANGED
@@ -1215,8 +1215,9 @@ function useSpsForm(value, validatorMap) {
1215
1215
  return controlSet;
1216
1216
  }, []);
1217
1217
  const scheduledDispatch = useRef();
1218
- let currentValidatorMap = validatorMap;
1218
+ const validatorMapRef = useRef(validatorMap);
1219
1219
  const reducer2 = useCallback$1((currentState, action) => {
1220
+ var _a, _b, _c;
1220
1221
  let newValue;
1221
1222
  scheduledDispatch.current = null;
1222
1223
  if (action) {
@@ -1228,12 +1229,12 @@ function useSpsForm(value, validatorMap) {
1228
1229
  control.revealAllErrors = true;
1229
1230
  }
1230
1231
  } else if (action.newValidators) {
1231
- if (currentValidatorMap) {
1232
+ if (validatorMapRef.current) {
1232
1233
  const controlPath = action.path.join(".");
1233
- const newValidatorMap = __spreadValues({}, currentValidatorMap);
1234
- newValidatorMap[controlPath] = action.newValidators;
1235
- currentValidatorMap = newValidatorMap;
1236
- updateValidators(currentState.formValue, currentState.formMeta, currentValidatorMap, true);
1234
+ validatorMapRef.current = __spreadProps(__spreadValues({}, validatorMapRef.current), {
1235
+ [controlPath]: action.newValidators
1236
+ });
1237
+ updateValidators(currentState.formValue, currentState.formMeta, validatorMapRef.current, true);
1237
1238
  }
1238
1239
  } else {
1239
1240
  newValue = cloneFormValue(currentState.formValue, action.path);
@@ -1243,6 +1244,10 @@ function useSpsForm(value, validatorMap) {
1243
1244
  newValue = action.value;
1244
1245
  }
1245
1246
  }
1247
+ if ((_a = action.updateFormOptions) == null ? void 0 : _a.validators) {
1248
+ validatorMapRef.current = __spreadValues(__spreadValues({}, validatorMapRef.current), action.updateFormOptions.validators);
1249
+ updateValidators(currentState.formValue, currentState.formMeta, validatorMapRef.current, (_c = (_b = action.updateFormOptions) == null ? void 0 : _b.runValidators) != null ? _c : true);
1250
+ }
1246
1251
  if (newValue) {
1247
1252
  deepFreeze(newValue);
1248
1253
  const valueDiff = diff(currentState.formValue, newValue);
@@ -1279,8 +1284,8 @@ function useSpsForm(value, validatorMap) {
1279
1284
  }
1280
1285
  }
1281
1286
  }
1282
- if (currentValidatorMap) {
1283
- updateValidators(newValue, currentState.formMeta, currentValidatorMap);
1287
+ if (validatorMapRef.current) {
1288
+ updateValidators(newValue, currentState.formMeta, validatorMapRef.current);
1284
1289
  const alreadyValidated = new Set();
1285
1290
  validatorMapForEach(currentState.formMeta, validatorMap, (control, validators) => {
1286
1291
  if (typeof validators === "function") {
@@ -1340,12 +1345,12 @@ function useSpsForm(value, validatorMap) {
1340
1345
  updateForm();
1341
1346
  }
1342
1347
  }
1343
- function updateForm(newValue) {
1344
- if (newValue) {
1348
+ function updateForm(newValue, options) {
1349
+ if (newValue || options) {
1345
1350
  if (scheduledDispatch.current) {
1346
1351
  window.clearTimeout(scheduledDispatch.current);
1347
1352
  }
1348
- const sd = window.setTimeout(dispatch, 0, { value: newValue });
1353
+ const sd = window.setTimeout(dispatch, 0, { value: newValue, updateFormOptions: options });
1349
1354
  scheduledDispatch.current = sd;
1350
1355
  } else if (!scheduledDispatch.current) {
1351
1356
  const sd = window.setTimeout(dispatch, 0, {});
@@ -23826,7 +23831,7 @@ const SpsFormExamples = {
23826
23831
  },
23827
23832
  custom_errors: {
23828
23833
  label: "Custom errors",
23829
- description: () => /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("p", null, "When using custom validators you can specify when to show the errors - on Change, on Blur or on Submit.", /* @__PURE__ */ React.createElement("br", null), "On Blur is the default behavior. If you want to change that behavior you can use ", /* @__PURE__ */ React.createElement("b", null, "useCustomValidator hook"), ". It takes validation function as first argument, validator configuration (object containing errorKey as key and ValidationMode as value) as second argument and dependencies as third argument. useCustomValidator returns a React callback of the validation function with the given dependencies."), /* @__PURE__ */ React.createElement("p", null, /* @__PURE__ */ React.createElement("b", null, "!Note:"), " useCustomValidator hook will make changes to the global scope and once you set a custom error key you can't change it.")),
23834
+ description: () => /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("p", null, "When using custom validators you can specify when to show the errors - on Change, on Blur or on Submit.", /* @__PURE__ */ React.createElement("br", null), "On Blur is the default behavior. If you want to change that behavior you can use", " ", /* @__PURE__ */ React.createElement("b", null, "useCustomValidator hook"), ". It takes validation function as first argument, validator configuration (object containing errorKey as key and ValidationMode as value) as second argument and dependencies as third argument. useCustomValidator returns a React callback of the validation function with the given dependencies."), /* @__PURE__ */ React.createElement("p", null, /* @__PURE__ */ React.createElement("b", null, "!Note:"), " useCustomValidator hook will make changes to the global scope and once you set a custom error key you can't change it.")),
23830
23835
  examples: {
23831
23836
  basic: {
23832
23837
  react: code`
@@ -23885,6 +23890,77 @@ const SpsFormExamples = {
23885
23890
  `
23886
23891
  }
23887
23892
  }
23893
+ },
23894
+ updating_validators: {
23895
+ label: "Updating validators",
23896
+ description: () => /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("p", null, "The updateForm function supports a seconds argument (options) that allows modifying multiple field validators at once.")),
23897
+ examples: {
23898
+ basic: {
23899
+ react: code`
23900
+ function DemoComponent() {
23901
+ const initValue = {
23902
+ fieldA: "",
23903
+ fieldB: "",
23904
+ };
23905
+
23906
+ const { formValue, formMeta, updateForm } = useSpsForm(initValue, {
23907
+ "fieldA": [SpsValidators.required],
23908
+ });
23909
+
23910
+ function handleSubmit() {
23911
+ console.log("submit", formValue);
23912
+ }
23913
+
23914
+ function reset() {
23915
+ updateForm(initValue, { validators: { fieldA: [SpsValidators.required], fieldB: [] }});
23916
+ formMeta.markAsPristine();
23917
+ }
23918
+
23919
+ // This will make field A non required and field B required in a single operation
23920
+ function updateValidators() {
23921
+ updateForm(undefined, { validators: { fieldA: [], fieldB: [SpsValidators.required] }});
23922
+ }
23923
+
23924
+ return <>
23925
+ <SpsForm formMeta={formMeta} onSubmit={handleSubmit}>
23926
+ <div className="sfg-row">
23927
+ <div className="sfg-col-8">
23928
+ <SpsLabel
23929
+ for={formMeta.fields.fieldA}
23930
+ >
23931
+ Field A
23932
+ </SpsLabel>
23933
+ <SpsTextInput
23934
+ value={formValue.fieldA}
23935
+ formMeta={formMeta.fields.fieldA}
23936
+ />
23937
+ </div>
23938
+ <div className="sfg-col-8">
23939
+ <SpsLabel
23940
+ for={formMeta.fields.fieldB}
23941
+ >
23942
+ Field B
23943
+ </SpsLabel>
23944
+ <SpsTextInput
23945
+ value={formValue.fieldB}
23946
+ formMeta={formMeta.fields.fieldB}
23947
+ />
23948
+ </div>
23949
+ </div>
23950
+
23951
+ <div className="sfg-row">
23952
+ <div className="sfg-col-12">
23953
+ <SpsButton className="mr-1" onClick={reset}>Reset</SpsButton>
23954
+ <SpsButton className="mr-1" onClick={updateValidators}>Update Validators</SpsButton>
23955
+ <SpsButton type={ButtonType.SUBMIT} kind={ButtonKind.CONFIRM}>Submit</SpsButton>
23956
+ </div>
23957
+ </div>
23958
+ </SpsForm>
23959
+ </>;
23960
+ }
23961
+ `
23962
+ }
23963
+ }
23888
23964
  }
23889
23965
  };
23890
23966
  const SpsAddRemoveFormRowExamples = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@spscommerce/ds-react",
3
3
  "description": "SPS Design System React components",
4
- "version": "6.2.2",
4
+ "version": "6.3.0",
5
5
  "author": "SPS Commerce",
6
6
  "license": "UNLICENSED",
7
7
  "repository": "https://github.com/spscommerce/design-system/tree/main/packages/@spscommerce/ds-react",
@@ -28,11 +28,11 @@
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@react-stately/collections": "^3.3.3",
31
- "@spscommerce/ds-colors": "6.2.2",
32
- "@spscommerce/ds-illustrations": "6.2.2",
33
- "@spscommerce/ds-shared": "6.2.2",
34
- "@spscommerce/positioning": "6.2.2",
35
- "@spscommerce/utils": "6.2.2",
31
+ "@spscommerce/ds-colors": "6.3.0",
32
+ "@spscommerce/ds-illustrations": "6.3.0",
33
+ "@spscommerce/ds-shared": "6.3.0",
34
+ "@spscommerce/positioning": "6.3.0",
35
+ "@spscommerce/utils": "6.3.0",
36
36
  "moment": "^2.25.3",
37
37
  "moment-timezone": "^0.5.28",
38
38
  "react": "^16.9.0",
@@ -40,11 +40,11 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@react-stately/collections": "^3.3.3",
43
- "@spscommerce/ds-colors": "6.2.2",
44
- "@spscommerce/ds-illustrations": "6.2.2",
45
- "@spscommerce/ds-shared": "6.2.2",
46
- "@spscommerce/positioning": "6.2.2",
47
- "@spscommerce/utils": "6.2.2",
43
+ "@spscommerce/ds-colors": "6.3.0",
44
+ "@spscommerce/ds-illustrations": "6.3.0",
45
+ "@spscommerce/ds-shared": "6.3.0",
46
+ "@spscommerce/positioning": "6.3.0",
47
+ "@spscommerce/utils": "6.3.0",
48
48
  "@testing-library/react": "^9.3.2",
49
49
  "@types/prop-types": "^15.7.1",
50
50
  "@types/react": "^16.9.0",