@onehat/ui 0.3.108 → 0.3.111

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/ui",
3
- "version": "0.3.108",
3
+ "version": "0.3.111",
4
4
  "description": "Base UI for OneHat apps",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -75,6 +75,7 @@ function Form(props) {
75
75
  disableDirtyIcon = false,
76
76
  onBack,
77
77
  onReset,
78
+ onInit,
78
79
  onViewMode,
79
80
  submitBtnLabel,
80
81
  onSubmit,
@@ -127,6 +128,7 @@ function Form(props) {
127
128
  [containerWidth, setContainerWidth] = useState(),
128
129
  initialValues = _.merge(startingValues, (record && !record.isDestroyed ? record.submitValues : {})),
129
130
  defaultValues = isMultiple ? getNullFieldValues(initialValues, Repository) : initialValues, // when multiple entities, set all default values to null
131
+ validatorToUse = validator || (isMultiple ? disableRequiredYupFields(Repository?.schema?.model?.validator) : Repository?.schema?.model?.validator) || yup.object(),
130
132
  {
131
133
  control,
132
134
  formState,
@@ -142,7 +144,7 @@ function Form(props) {
142
144
  // setFocus,
143
145
  getValues: formGetValues,
144
146
  // getFieldState,
145
- // trigger,
147
+ trigger,
146
148
  } = useForm({
147
149
  mode: 'onChange', // onChange | onBlur | onSubmit | onTouched | all
148
150
  // reValidateMode: 'onChange', // onChange | onBlur | onSubmit
@@ -157,7 +159,7 @@ function Form(props) {
157
159
  // delayError: 0,
158
160
  // shouldUnregister: false,
159
161
  // shouldUseNativeValidation: false,
160
- resolver: yupResolver(validator || (isMultiple ? disableRequiredYupFields(Repository?.schema?.model?.validator) : Repository?.schema?.model?.validator) || yup.object()),
162
+ resolver: yupResolver(validatorToUse),
161
163
  context: { isPhantom },
162
164
  }),
163
165
  buildFromColumnsConfig = () => {
@@ -297,6 +299,7 @@ function Form(props) {
297
299
  useSelectorId = false,
298
300
  isHidden = false,
299
301
  getDynamicProps,
302
+ getIsRequired,
300
303
  ...propsToPass
301
304
  } = item,
302
305
  editorTypeProps = {};
@@ -483,7 +486,7 @@ function Form(props) {
483
486
  }
484
487
  onChange(newValue);
485
488
  if (onEditorChange) {
486
- onEditorChange(newValue, formSetValue, formGetValues, formState);
489
+ onEditorChange(newValue, formSetValue, formGetValues, formState, trigger);
487
490
  }
488
491
  }}
489
492
  onBlur={onBlur}
@@ -521,11 +524,11 @@ function Form(props) {
521
524
 
522
525
  let isRequired = false,
523
526
  requiredIndicator = null;
524
- if (editorType === EDITOR_TYPE__PLAIN) {
527
+ if (getIsRequired) {
528
+ isRequired = getIsRequired(formGetValues, formState);
529
+ } else if (validatorToUse?.fields && validatorToUse.fields[name]?.exclusiveTests?.required) {
525
530
  // submitted validator
526
- if (validator?.fields && validator.fields[name]?.exclusiveTests?.required) {
527
- isRequired = true;
528
- }
531
+ isRequired = true;
529
532
  } else if ((propertyDef?.validator?.spec && !propertyDef.validator.spec.optional) ||
530
533
  (propertyDef?.requiredIfPhantom && isPhantom) ||
531
534
  (propertyDef?.requiredIfNotPhantom && !isPhantom)) {
@@ -612,19 +615,25 @@ function Form(props) {
612
615
  alert(errors.message);
613
616
  }
614
617
  },
618
+ doReset = (values) => {
619
+ reset(values);
620
+ if (onReset) {
621
+ onReset(values, formSetValue, formGetValues);
622
+ }
623
+ },
615
624
  onSaveDecorated = async (data, e) => {
616
625
  // reset the form after a save
617
626
  const result = await onSave(data, e);
618
627
  if (result) {
619
628
  const values = record.submitValues;
620
- reset(values);
629
+ doReset(values);
621
630
  }
622
631
  },
623
632
  onSubmitDecorated = async (data, e) => {
624
633
  const result = await onSubmit(data, e);
625
634
  if (result) {
626
635
  const values = record.submitValues;
627
- reset(values);
636
+ doReset(values);
628
637
  }
629
638
  },
630
639
  onLayoutDecorated = (e) => {
@@ -636,9 +645,13 @@ function Form(props) {
636
645
  };
637
646
 
638
647
  useEffect(() => {
639
- if (record !== previousRecord) {
648
+ if (record === previousRecord) {
649
+ if (onInit) {
650
+ onInit(initialValues, formSetValue, formGetValues);
651
+ }
652
+ } else {
640
653
  setPreviousRecord(record);
641
- reset(defaultValues);
654
+ doReset(defaultValues);
642
655
  }
643
656
  if (formSetup) {
644
657
  formSetup(formSetValue, formGetValues, formState)
@@ -847,12 +860,7 @@ function Form(props) {
847
860
  {showResetBtn &&
848
861
  <IconButton
849
862
  key="resetBtn"
850
- onPress={() => {
851
- if (onReset) {
852
- onReset();
853
- }
854
- reset();
855
- }}
863
+ onPress={() => doReset()}
856
864
  icon={Rotate}
857
865
  _icon={{
858
866
  color: !formState.isDirty ? 'trueGray.400' : '#000',