@gaozh1024/rn-kit 0.2.0-beta.0 → 0.2.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.d.mts +12 -14
- package/dist/index.d.ts +12 -14
- package/dist/index.js +61 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +61 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2117,6 +2117,17 @@ var styles5 = StyleSheet5.create({
|
|
|
2117
2117
|
}
|
|
2118
2118
|
});
|
|
2119
2119
|
|
|
2120
|
+
// src/ui/form/group.ts
|
|
2121
|
+
var toggleGroupValue = (values, optionValue, checked) => {
|
|
2122
|
+
if (checked) {
|
|
2123
|
+
return values.includes(optionValue) ? values : [...values, optionValue];
|
|
2124
|
+
}
|
|
2125
|
+
return values.filter((value) => value !== optionValue);
|
|
2126
|
+
};
|
|
2127
|
+
var isGroupOptionDisabled = (groupDisabled, optionDisabled) => {
|
|
2128
|
+
return groupDisabled || Boolean(optionDisabled);
|
|
2129
|
+
};
|
|
2130
|
+
|
|
2120
2131
|
// src/ui/form/CheckboxGroup.tsx
|
|
2121
2132
|
import { jsx as jsx21 } from "nativewind/jsx-runtime";
|
|
2122
2133
|
function CheckboxGroup({
|
|
@@ -2128,18 +2139,15 @@ function CheckboxGroup({
|
|
|
2128
2139
|
}) {
|
|
2129
2140
|
const handleChange = (optionValue, checked) => {
|
|
2130
2141
|
if (!onChange) return;
|
|
2131
|
-
|
|
2132
|
-
onChange([...value, optionValue]);
|
|
2133
|
-
} else {
|
|
2134
|
-
onChange(value.filter((v) => v !== optionValue));
|
|
2135
|
-
}
|
|
2142
|
+
onChange(toggleGroupValue(value, optionValue, checked));
|
|
2136
2143
|
};
|
|
2137
|
-
|
|
2144
|
+
const isRow = direction === "row";
|
|
2145
|
+
return /* @__PURE__ */ jsx21(AppView, { row: isRow, flex: isRow, gap: 4, children: options.map((option) => /* @__PURE__ */ jsx21(
|
|
2138
2146
|
Checkbox,
|
|
2139
2147
|
{
|
|
2140
2148
|
checked: value.includes(option.value),
|
|
2141
2149
|
onChange: (checked) => handleChange(option.value, checked),
|
|
2142
|
-
disabled: disabled
|
|
2150
|
+
disabled: isGroupOptionDisabled(disabled, option.disabled),
|
|
2143
2151
|
children: option.label
|
|
2144
2152
|
},
|
|
2145
2153
|
option.value
|
|
@@ -2226,12 +2234,13 @@ function RadioGroup({
|
|
|
2226
2234
|
direction = "column",
|
|
2227
2235
|
disabled = false
|
|
2228
2236
|
}) {
|
|
2229
|
-
|
|
2237
|
+
const isRow = direction === "row";
|
|
2238
|
+
return /* @__PURE__ */ jsx23(AppView, { row: isRow, flex: isRow, gap: 4, children: options.map((option) => /* @__PURE__ */ jsx23(
|
|
2230
2239
|
Radio,
|
|
2231
2240
|
{
|
|
2232
2241
|
checked: value === option.value,
|
|
2233
2242
|
onChange: () => onChange?.(option.value),
|
|
2234
|
-
disabled: disabled
|
|
2243
|
+
disabled: isGroupOptionDisabled(disabled, option.disabled),
|
|
2235
2244
|
children: option.label
|
|
2236
2245
|
},
|
|
2237
2246
|
option.value
|
|
@@ -3092,10 +3101,21 @@ function FormItem({
|
|
|
3092
3101
|
|
|
3093
3102
|
// src/ui/form/useForm.ts
|
|
3094
3103
|
import { useState as useState19, useCallback as useCallback15, useMemo as useMemo7 } from "react";
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3104
|
+
var getIssuePath = (issue) => issue.path.map(String).join(".");
|
|
3105
|
+
var getFieldError = (issues, name) => {
|
|
3106
|
+
const exactIssue = issues.find((issue) => getIssuePath(issue) === name);
|
|
3107
|
+
if (exactIssue) return exactIssue.message;
|
|
3108
|
+
const nestedIssue = issues.find((issue) => issue.path[0] === name);
|
|
3109
|
+
return nestedIssue?.message;
|
|
3110
|
+
};
|
|
3111
|
+
var buildFormErrors = (issues) => {
|
|
3112
|
+
return issues.reduce((acc, issue) => {
|
|
3113
|
+
const path = getIssuePath(issue);
|
|
3114
|
+
acc[path || "root"] = issue.message;
|
|
3115
|
+
return acc;
|
|
3116
|
+
}, {});
|
|
3117
|
+
};
|
|
3118
|
+
function useForm({ schema, defaultValues }) {
|
|
3099
3119
|
const [values, setValues] = useState19(defaultValues);
|
|
3100
3120
|
const [errors, setErrors] = useState19({});
|
|
3101
3121
|
const [isSubmitting, setIsSubmitting] = useState19(false);
|
|
@@ -3105,14 +3125,21 @@ function useForm({
|
|
|
3105
3125
|
const isValid = useMemo7(() => {
|
|
3106
3126
|
return Object.keys(errors).length === 0;
|
|
3107
3127
|
}, [errors]);
|
|
3108
|
-
const
|
|
3109
|
-
setValues((prev) => ({ ...prev, [name]: value }));
|
|
3128
|
+
const clearFieldError = useCallback15((name) => {
|
|
3110
3129
|
setErrors((prev) => {
|
|
3130
|
+
if (!(name in prev)) return prev;
|
|
3111
3131
|
const next = { ...prev };
|
|
3112
3132
|
delete next[name];
|
|
3113
3133
|
return next;
|
|
3114
3134
|
});
|
|
3115
3135
|
}, []);
|
|
3136
|
+
const setValue = useCallback15(
|
|
3137
|
+
(name, value) => {
|
|
3138
|
+
setValues((prev) => ({ ...prev, [name]: value }));
|
|
3139
|
+
clearFieldError(name);
|
|
3140
|
+
},
|
|
3141
|
+
[clearFieldError]
|
|
3142
|
+
);
|
|
3116
3143
|
const getValue = useCallback15(
|
|
3117
3144
|
(name) => {
|
|
3118
3145
|
return values[name];
|
|
@@ -3121,42 +3148,33 @@ function useForm({
|
|
|
3121
3148
|
);
|
|
3122
3149
|
const validateField = useCallback15(
|
|
3123
3150
|
async (name) => {
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
return true;
|
|
3134
|
-
}
|
|
3151
|
+
const fieldName = name;
|
|
3152
|
+
const result = await schema.safeParseAsync(values);
|
|
3153
|
+
if (result.success) {
|
|
3154
|
+
clearFieldError(fieldName);
|
|
3155
|
+
return true;
|
|
3156
|
+
}
|
|
3157
|
+
const errorMessage = getFieldError(result.error.issues, fieldName);
|
|
3158
|
+
if (!errorMessage) {
|
|
3159
|
+
clearFieldError(fieldName);
|
|
3135
3160
|
return true;
|
|
3136
|
-
} catch (error) {
|
|
3137
|
-
setErrors((prev) => ({
|
|
3138
|
-
...prev,
|
|
3139
|
-
[name]: error.errors?.[0]?.message || "\u9A8C\u8BC1\u5931\u8D25"
|
|
3140
|
-
}));
|
|
3141
|
-
return false;
|
|
3142
3161
|
}
|
|
3162
|
+
setErrors((prev) => ({
|
|
3163
|
+
...prev,
|
|
3164
|
+
[fieldName]: errorMessage
|
|
3165
|
+
}));
|
|
3166
|
+
return false;
|
|
3143
3167
|
},
|
|
3144
|
-
[schema, values]
|
|
3168
|
+
[schema, values, clearFieldError]
|
|
3145
3169
|
);
|
|
3146
3170
|
const validate = useCallback15(async () => {
|
|
3147
|
-
|
|
3148
|
-
|
|
3171
|
+
const result = await schema.safeParseAsync(values);
|
|
3172
|
+
if (result.success) {
|
|
3149
3173
|
setErrors({});
|
|
3150
3174
|
return true;
|
|
3151
|
-
} catch (error) {
|
|
3152
|
-
const formErrors = {};
|
|
3153
|
-
error.errors?.forEach((err) => {
|
|
3154
|
-
const path = err.path.join(".");
|
|
3155
|
-
formErrors[path] = err.message;
|
|
3156
|
-
});
|
|
3157
|
-
setErrors(formErrors);
|
|
3158
|
-
return false;
|
|
3159
3175
|
}
|
|
3176
|
+
setErrors(buildFormErrors(result.error.issues));
|
|
3177
|
+
return false;
|
|
3160
3178
|
}, [schema, values]);
|
|
3161
3179
|
const reset = useCallback15(() => {
|
|
3162
3180
|
setValues(defaultValues);
|