@kaspernj/api-maker 1.0.341 → 1.0.343

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
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.341",
19
+ "version": "1.0.343",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -460,7 +460,7 @@ export default class BaseModel {
460
460
  BaseModel.sendValidationErrorsEvent(validationErrors, options)
461
461
 
462
462
  if (!options || options.throwValidationError != false) {
463
- throw new ValidationError(validationErrors, digg(error, "args"))
463
+ throw error
464
464
  }
465
465
  }
466
466
 
package/src/use-input.mjs CHANGED
@@ -1,16 +1,15 @@
1
- import {dig, digg, digs} from "diggerize"
1
+ import {dig, digg} from "diggerize"
2
2
  import {useCallback, useEffect, useMemo} from "react"
3
3
  import idForComponent from "./inputs/id-for-component.mjs"
4
4
  import nameForComponent from "./inputs/name-for-component.mjs"
5
5
  import strftime from "strftime"
6
- import useEventListener from "./use-event-listener.mjs"
7
6
  import useShape from "set-state-compare/src/use-shape.js"
7
+ import useValidationErrors from "./use-validation-errors.mjs"
8
8
 
9
9
  const useInput = ({props, wrapperOptions}) => {
10
10
  const s = useShape(props)
11
11
 
12
12
  s.useStates({
13
- errors: [],
14
13
  form: undefined
15
14
  })
16
15
 
@@ -18,12 +17,7 @@ const useInput = ({props, wrapperOptions}) => {
18
17
  setForm()
19
18
  }, [s.props.inputRef?.current])
20
19
 
21
- const fakeComponent = useMemo(
22
- () => ({
23
- props
24
- }),
25
- []
26
- )
20
+ s.meta.fakeComponent = {props}
27
21
 
28
22
  const formatValue = useCallback((value) => {
29
23
  const {formatValue} = s.props
@@ -78,12 +72,6 @@ const useInput = ({props, wrapperOptions}) => {
78
72
  }
79
73
  }, [])
80
74
 
81
- const inputName = useCallback(() => {
82
- if (s.state.blankInputName) return ""
83
-
84
- return getName()
85
- }, [])
86
-
87
75
  const inputRefBackup = useCallback(() => {
88
76
  if (!s.meta._inputRefBackup) s.meta._inputRefBackup = React.createRef()
89
77
 
@@ -110,16 +98,6 @@ const useInput = ({props, wrapperOptions}) => {
110
98
  }
111
99
  }, [])
112
100
 
113
- const onValidationErrors = useCallback((event) => {
114
- const errors = event.detail.getValidationErrorsForInput({
115
- attribute: s.props.attribute,
116
- inputName: inputName(),
117
- onMatchValidationError: s.props.onMatchValidationError
118
- })
119
-
120
- s.set({errors})
121
- }, [])
122
-
123
101
  const setForm = useCallback(() => {
124
102
  const inputElement = inputRef().current
125
103
 
@@ -129,8 +107,8 @@ const useInput = ({props, wrapperOptions}) => {
129
107
  if (form && form != s.s.form) s.set({form})
130
108
  }, [])
131
109
 
132
- const getId = useCallback(() => idForComponent(fakeComponent), [])
133
- const getName = useCallback(() => nameForComponent(fakeComponent), [])
110
+ const getId = useCallback(() => idForComponent(s.m.fakeComponent), [])
111
+ const getName = useCallback(() => nameForComponent(s.m.fakeComponent), [])
134
112
 
135
113
  const getInputProps = useCallback(() => {
136
114
  const givenInputProps = s.props.inputProps || {}
@@ -160,21 +138,25 @@ const useInput = ({props, wrapperOptions}) => {
160
138
 
161
139
  const {inputProps: oldInputProps, ...restProps} = props
162
140
  const type = inputType()
163
- const inputProps = getInputProps()
164
141
 
165
- if (!inputProps.ref) throw new Error("No input ref?")
166
- if (!handleAsSelect()) inputProps.type = type
142
+ s.meta.inputProps = getInputProps()
143
+ s.meta.inputNameWithoutId = useMemo(() => s.m.inputProps.name?.replace(/\[(.+)_id\]$/, "[$1]"), [s.m.inputProps.name])
144
+
145
+ if (!s.m.inputProps.ref) throw new Error("No input ref?")
146
+ if (!handleAsSelect()) s.m.inputProps.type = type
147
+
148
+ const {validationErrors} = useValidationErrors((validationError) =>
149
+ validationError.inputName == s.m.inputProps.name || validationError.inputName == s.m.inputNameWithoutId
150
+ )
167
151
 
168
152
  const wrapperOpts = {
169
- errors: s.s.errors,
153
+ errors: validationErrors,
170
154
  form: s.s.form,
171
155
  label: label()
172
156
  }
173
157
 
174
- useEventListener(s.s.form, "validation-errors", onValidationErrors)
175
-
176
158
  return {
177
- inputProps,
159
+ inputProps: s.m.inputProps,
178
160
  wrapperOpts,
179
161
  restProps
180
162
  }
@@ -1,4 +1,5 @@
1
1
  import events from "./events.mjs"
2
+ import {useCallback} from "react"
2
3
  import useEventEmitter from "./use-event-emitter.mjs"
3
4
  import useShape from "set-state-compare/src/use-shape.js"
4
5
 
@@ -11,6 +11,9 @@ class ValidationError extends BaseError {
11
11
  this.validationErrors = validationErrors
12
12
  }
13
13
 
14
+ getUnhandledErrors = () => this.validationErrors.getValidationErrors().filter((validationError) => !validationError.getHandled())
15
+ getValidationErrors = () => digg(this, "validationErrors")
16
+
14
17
  hasUnhandledErrors() {
15
18
  const unhandledError = this.validationErrors.getValidationErrors().find((validationError) => !validationError.getHandled())
16
19
 
@@ -89,7 +89,7 @@ class ValidationErrors {
89
89
  if (onMatchValidationError) {
90
90
  return onMatchValidationError(validationError)
91
91
  } else {
92
- return validationError.matchesAttributeAndInputName(attribute, inputName, onMatchValidationError)
92
+ return validationError.matchesAttributeAndInputName(attribute, inputName)
93
93
  }
94
94
  })
95
95