@formality-ui/react 0.2.3 → 0.2.4
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.cjs +69 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +69 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1088,6 +1088,40 @@ function Field({
|
|
|
1088
1088
|
const label = react.useMemo(() => {
|
|
1089
1089
|
return core.resolveLabel(name, fieldConfig, fieldSelectProps, restProps);
|
|
1090
1090
|
}, [name, fieldConfig, fieldSelectProps, restProps]);
|
|
1091
|
+
const passSubscriptionsEnabled = fieldConfig.passSubscriptions === true;
|
|
1092
|
+
const provideStateEnabled = fieldConfig.provideState === true;
|
|
1093
|
+
const subscribedWatchNames = react.useMemo(
|
|
1094
|
+
() => passSubscriptionsEnabled && allSubscriptions.length > 0 ? allSubscriptions : [],
|
|
1095
|
+
[passSubscriptionsEnabled, allSubscriptions]
|
|
1096
|
+
);
|
|
1097
|
+
const subscribedValues = reactHookForm.useWatch({
|
|
1098
|
+
control: methods.control,
|
|
1099
|
+
name: subscribedWatchNames.length > 0 ? subscribedWatchNames : []
|
|
1100
|
+
});
|
|
1101
|
+
const subscribedState = react.useMemo(() => {
|
|
1102
|
+
if (!passSubscriptionsEnabled || subscribedWatchNames.length === 0) {
|
|
1103
|
+
return void 0;
|
|
1104
|
+
}
|
|
1105
|
+
const result = {};
|
|
1106
|
+
const values = Array.isArray(subscribedValues) ? subscribedValues : [subscribedValues];
|
|
1107
|
+
subscribedWatchNames.forEach((fieldName, i) => {
|
|
1108
|
+
const fs = methods.getFieldState(fieldName);
|
|
1109
|
+
result[fieldName] = makeProxyState({
|
|
1110
|
+
value: values[i],
|
|
1111
|
+
isTouched: fs.isTouched,
|
|
1112
|
+
isDirty: fs.isDirty,
|
|
1113
|
+
isValidating: fs.isValidating,
|
|
1114
|
+
error: fs.error,
|
|
1115
|
+
invalid: fs.invalid
|
|
1116
|
+
});
|
|
1117
|
+
});
|
|
1118
|
+
return result;
|
|
1119
|
+
}, [
|
|
1120
|
+
passSubscriptionsEnabled,
|
|
1121
|
+
subscribedWatchNames,
|
|
1122
|
+
subscribedValues,
|
|
1123
|
+
methods
|
|
1124
|
+
]);
|
|
1091
1125
|
const validationRules = react.useMemo(() => {
|
|
1092
1126
|
return {
|
|
1093
1127
|
...fieldConfig.rules,
|
|
@@ -1165,6 +1199,24 @@ function Field({
|
|
|
1165
1199
|
inputConfig.formatter,
|
|
1166
1200
|
providerConfig.formatters
|
|
1167
1201
|
);
|
|
1202
|
+
const stateInjection = {};
|
|
1203
|
+
if (provideStateEnabled) {
|
|
1204
|
+
stateInjection[providerConfig.defaultSubscriptionPropName] = makeProxyState({
|
|
1205
|
+
value: field.value,
|
|
1206
|
+
isTouched: fieldState.isTouched,
|
|
1207
|
+
isDirty: fieldState.isDirty,
|
|
1208
|
+
isValidating: fieldState.isValidating,
|
|
1209
|
+
error: fieldState.error,
|
|
1210
|
+
invalid: fieldState.invalid
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
if (passSubscriptionsEnabled && subscribedState) {
|
|
1214
|
+
const subsPropName = fieldConfig.passSubscriptionsAs ?? providerConfig.defaultSubscriptionPropName;
|
|
1215
|
+
stateInjection[subsPropName] = subscribedState;
|
|
1216
|
+
}
|
|
1217
|
+
if (provideStateEnabled || passSubscriptionsEnabled) {
|
|
1218
|
+
stateInjection.formState = formState;
|
|
1219
|
+
}
|
|
1168
1220
|
const finalProps = core.mergeFieldProps({
|
|
1169
1221
|
providerDefaultFieldProps: providerConfig.defaultFieldProps,
|
|
1170
1222
|
providerSelectDefaultFieldProps: providerSelectProps,
|
|
@@ -1182,7 +1234,8 @@ function Field({
|
|
|
1182
1234
|
[inputConfig.inputFieldProp ?? "value"]: formattedValue,
|
|
1183
1235
|
onChange: handleChange(field.onChange),
|
|
1184
1236
|
onBlur: field.onBlur,
|
|
1185
|
-
forwardRef: field.ref
|
|
1237
|
+
forwardRef: field.ref,
|
|
1238
|
+
...stateInjection
|
|
1186
1239
|
}
|
|
1187
1240
|
});
|
|
1188
1241
|
const Component = inputConfig.component;
|
|
@@ -1201,10 +1254,22 @@ function Field({
|
|
|
1201
1254
|
}
|
|
1202
1255
|
);
|
|
1203
1256
|
} else if (isHostComponent) {
|
|
1204
|
-
const
|
|
1257
|
+
const subsPropName = fieldConfig.passSubscriptionsAs ?? providerConfig.defaultSubscriptionPropName;
|
|
1258
|
+
const strippedHostProps = {};
|
|
1259
|
+
const nonDomKeys = /* @__PURE__ */ new Set([
|
|
1260
|
+
"forwardRef",
|
|
1261
|
+
"formState",
|
|
1262
|
+
"state",
|
|
1263
|
+
subsPropName
|
|
1264
|
+
]);
|
|
1265
|
+
for (const [key, value] of Object.entries(finalProps)) {
|
|
1266
|
+
if (!nonDomKeys.has(key)) {
|
|
1267
|
+
strippedHostProps[key] = value;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1205
1270
|
renderedField = react.createElement(inputConfig.component, {
|
|
1206
|
-
...
|
|
1207
|
-
ref:
|
|
1271
|
+
...strippedHostProps,
|
|
1272
|
+
ref: finalProps.forwardRef
|
|
1208
1273
|
});
|
|
1209
1274
|
} else {
|
|
1210
1275
|
renderedField = /* @__PURE__ */ jsxRuntime.jsx(Component, { ...finalProps });
|