dirk-cfx-react 1.1.10 → 1.1.12

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.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { BorderedIcon, BorderedIconProps, ButtonProps, Counter, FloatingParticles, FloatingParticlesProps, InfoBox, InfoBoxProps, InputContainer, InputContainerProps, LevelBanner, LevelPanel, ModalContext, ModalProvider, MotionFlex, MotionIcon, MotionImage, MotionText, NavBar, NavigationContext, NavigationProvider, NavigationStore, ParticleState, ProgressProps, Prompt, PromptButton, PromptModal, SegmentProps, SegmentedControl, SegmentedControlProps, SegmentedProgress, Title, TitleProps, useModal, useModalActions, useNavigation, useNavigationStore } from './components/index.cjs';
2
2
  export { InitialFetch, InternalEvent, SettingsState, SkillSettings, UploadImageProps, colorWithAlpha, copyToClipboard, createSkill, fetchNui, gameToMap, getImageShape, initialFetches, internalEvent, isEnvBrowser, isProfanity, latPr100, locale, localeStore, mapCenter, mapToGame, noop, numberToRoman, openLink, registerInitialFetch, runFetches, splitFAString, updatePresignedURL, uploadImage, useAutoFetcher, useProfanityStore, useSettings } from './utils/index.cjs';
3
- export { FormProvider, FormState, NuiHandlerSignature, NuiMessageData, TornEdgeSVGFilter, ValidationRules, createFormStore, createScriptSettings, useForm, useNuiEvent, useTornEdges } from './hooks/index.cjs';
3
+ export { FormProvider, FormState, NuiHandlerSignature, NuiMessageData, TornEdgeSVGFilter, ValidationRules, ValidatorFn, createFormStore, createScriptSettings, useForm, useNuiEvent, useTornEdges } from './hooks/index.cjs';
4
4
  export { DirkProvider, DirkProviderProps } from './providers/index.cjs';
5
5
  import 'react/jsx-runtime';
6
6
  import 'react';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { BorderedIcon, BorderedIconProps, ButtonProps, Counter, FloatingParticles, FloatingParticlesProps, InfoBox, InfoBoxProps, InputContainer, InputContainerProps, LevelBanner, LevelPanel, ModalContext, ModalProvider, MotionFlex, MotionIcon, MotionImage, MotionText, NavBar, NavigationContext, NavigationProvider, NavigationStore, ParticleState, ProgressProps, Prompt, PromptButton, PromptModal, SegmentProps, SegmentedControl, SegmentedControlProps, SegmentedProgress, Title, TitleProps, useModal, useModalActions, useNavigation, useNavigationStore } from './components/index.js';
2
2
  export { InitialFetch, InternalEvent, SettingsState, SkillSettings, UploadImageProps, colorWithAlpha, copyToClipboard, createSkill, fetchNui, gameToMap, getImageShape, initialFetches, internalEvent, isEnvBrowser, isProfanity, latPr100, locale, localeStore, mapCenter, mapToGame, noop, numberToRoman, openLink, registerInitialFetch, runFetches, splitFAString, updatePresignedURL, uploadImage, useAutoFetcher, useProfanityStore, useSettings } from './utils/index.js';
3
- export { FormProvider, FormState, NuiHandlerSignature, NuiMessageData, TornEdgeSVGFilter, ValidationRules, createFormStore, createScriptSettings, useForm, useNuiEvent, useTornEdges } from './hooks/index.js';
3
+ export { FormProvider, FormState, NuiHandlerSignature, NuiMessageData, TornEdgeSVGFilter, ValidationRules, ValidatorFn, createFormStore, createScriptSettings, useForm, useNuiEvent, useTornEdges } from './hooks/index.js';
4
4
  export { DirkProvider, DirkProviderProps } from './providers/index.js';
5
5
  import 'react/jsx-runtime';
6
6
  import 'react';
package/dist/index.js CHANGED
@@ -229,7 +229,7 @@ async function fetchNui(eventName, data, mockData) {
229
229
  },
230
230
  body: JSON.stringify(data)
231
231
  };
232
- if (isEnvBrowser() && mockData) return mockData;
232
+ if (isEnvBrowser() && mockData !== void 0) return mockData;
233
233
  if (isEnvBrowser() && mockData === void 0) {
234
234
  console.warn(
235
235
  `[fetchNui] Called fetchNui for event "${eventName}" in browser environment without mockData. Returning empty object.`
@@ -2012,6 +2012,10 @@ function flattenRules(rules, prefix = "") {
2012
2012
  }
2013
2013
  return result;
2014
2014
  }
2015
+ async function runRule(rule, value, values) {
2016
+ const result = rule(value, values);
2017
+ return result instanceof Promise ? await result : result;
2018
+ }
2015
2019
  function createFormStore(initialValues, validationRules, onSubmit) {
2016
2020
  const flatRules = validationRules ? flattenRules(validationRules) : {};
2017
2021
  const history = [];
@@ -2026,17 +2030,16 @@ function createFormStore(initialValues, validationRules, onSubmit) {
2026
2030
  changedFields: [],
2027
2031
  changedCount: 0,
2028
2032
  onSubmit,
2029
- submit: () => {
2033
+ submit: async () => {
2030
2034
  const state = get();
2031
- const isValid = state.validate();
2032
- if (isValid && state.onSubmit) state.onSubmit(get());
2035
+ const isValid = await state.validate();
2036
+ if (isValid && state.onSubmit) {
2037
+ state.onSubmit(get());
2038
+ }
2033
2039
  },
2034
2040
  resetChangeCount: () => {
2035
2041
  changed.clear();
2036
- set(() => ({
2037
- changedFields: [],
2038
- changedCount: 0
2039
- }));
2042
+ set({ changedFields: [], changedCount: 0 });
2040
2043
  },
2041
2044
  setInitialValues: (newInitialValues) => set({ initialValues: newInitialValues }),
2042
2045
  setValue: (path, value) => {
@@ -2055,37 +2058,37 @@ function createFormStore(initialValues, validationRules, onSubmit) {
2055
2058
  changedCount: changed.size
2056
2059
  });
2057
2060
  const rule = flatRules[path];
2058
- if (rule) {
2059
- const error2 = rule(value, newValues);
2061
+ if (!rule) return;
2062
+ Promise.resolve(runRule(rule, value, newValues)).then((error2) => {
2060
2063
  if (error2)
2061
- set((state) => ({ errors: setNested(state.errors, path, error2) }));
2062
- else set((state) => ({ errors: deleteNested(state.errors, path) }));
2063
- }
2064
+ set((s) => ({ errors: setNested(s.errors, path, error2) }));
2065
+ else
2066
+ set((s) => ({ errors: deleteNested(s.errors, path) }));
2067
+ });
2064
2068
  },
2065
- setError: (path, message) => set((state) => ({ errors: setNested(state.errors, path, message) })),
2066
- clearError: (path) => set((state) => ({ errors: deleteNested(state.errors, path) })),
2067
- validateField: (path) => {
2069
+ setError: (path, message) => set((s) => ({ errors: setNested(s.errors, path, message) })),
2070
+ clearError: (path) => set((s) => ({ errors: deleteNested(s.errors, path) })),
2071
+ validateField: async (path) => {
2068
2072
  const state = get();
2069
2073
  const rule = flatRules[path];
2070
2074
  if (!rule) return true;
2071
2075
  const value = getNested(state.values, path);
2072
- const error2 = rule(value, state.values);
2076
+ const error2 = await runRule(rule, value, state.values);
2073
2077
  if (error2) {
2074
- set((state2) => ({ errors: setNested(state2.errors, path, error2) }));
2078
+ set((s) => ({ errors: setNested(s.errors, path, error2) }));
2075
2079
  return false;
2076
- } else {
2077
- set((state2) => ({ errors: deleteNested(state2.errors, path) }));
2078
- return true;
2079
2080
  }
2081
+ set((s) => ({ errors: deleteNested(s.errors, path) }));
2082
+ return true;
2080
2083
  },
2081
- validate: () => {
2084
+ validate: async () => {
2082
2085
  const state = get();
2083
2086
  let isValid = true;
2084
2087
  let newErrors = {};
2085
2088
  for (const path in flatRules) {
2086
2089
  const rule = flatRules[path];
2087
2090
  const value = getNested(state.values, path);
2088
- const error2 = rule(value, state.values);
2091
+ const error2 = await runRule(rule, value, state.values);
2089
2092
  if (error2) {
2090
2093
  isValid = false;
2091
2094
  newErrors = setNested(newErrors, path, error2);
@@ -2108,15 +2111,13 @@ function createFormStore(initialValues, validationRules, onSubmit) {
2108
2111
  });
2109
2112
  },
2110
2113
  back: () => {
2111
- const state = get();
2112
- if (history.length === 0) return;
2114
+ if (!history.length) return;
2113
2115
  const prev = history.pop();
2114
- future.push(state.values);
2116
+ future.push(get().values);
2115
2117
  changed.clear();
2116
- const current = prev;
2117
2118
  const initial = get().initialValues;
2118
- for (const key in current) {
2119
- if (JSON.stringify(current[key]) !== JSON.stringify(initial[key]))
2119
+ for (const key in prev) {
2120
+ if (JSON.stringify(prev[key]) !== JSON.stringify(initial[key]))
2120
2121
  changed.add(key);
2121
2122
  }
2122
2123
  set({
@@ -2128,15 +2129,13 @@ function createFormStore(initialValues, validationRules, onSubmit) {
2128
2129
  });
2129
2130
  },
2130
2131
  forward: () => {
2131
- const state = get();
2132
- if (future.length === 0) return;
2132
+ if (!future.length) return;
2133
2133
  const next = future.pop();
2134
- history.push(state.values);
2134
+ history.push(get().values);
2135
2135
  changed.clear();
2136
- const current = next;
2137
2136
  const initial = get().initialValues;
2138
- for (const key in current) {
2139
- if (JSON.stringify(current[key]) !== JSON.stringify(initial[key]))
2137
+ for (const key in next) {
2138
+ if (JSON.stringify(next[key]) !== JSON.stringify(initial[key]))
2140
2139
  changed.add(key);
2141
2140
  }
2142
2141
  set({
@@ -2156,12 +2155,16 @@ function FormProvider({
2156
2155
  onSubmit,
2157
2156
  children
2158
2157
  }) {
2159
- const storeRef = useRef(createFormStore(initialValues, validate, onSubmit));
2158
+ const storeRef = useRef(
2159
+ createFormStore(initialValues, validate, onSubmit)
2160
+ );
2160
2161
  return /* @__PURE__ */ jsx(FormContext.Provider, { value: storeRef.current, children });
2161
2162
  }
2162
2163
  function useForm() {
2163
2164
  const store = useContext(FormContext);
2164
- if (!store) throw new Error("useForm must be used inside a <FormProvider>");
2165
+ if (!store) {
2166
+ throw new Error("useForm must be used inside <FormProvider>");
2167
+ }
2165
2168
  return useStore(store);
2166
2169
  }
2167
2170
  function useTornEdges() {