@saas-ui/forms 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,13 +47,19 @@ export function createForm<FieldDefs>({
47
47
  props: WithFields<FormProps<TSchema, TFieldValues, TContext>, FieldDefs>,
48
48
  ref: ForwardedRef<HTMLFormElement>
49
49
  ) => {
50
- const { schema, ...rest } = props
50
+ const {
51
+ schema,
52
+ resolver: resolverProp,
53
+ fieldResolver: fieldResolverProp,
54
+ ...rest
55
+ } = props
56
+
51
57
  return (
52
58
  <FieldsProvider value={fields || {}}>
53
59
  <Form
54
60
  ref={ref}
55
- resolver={resolver?.(props.schema)}
56
- fieldResolver={fieldResolver?.(schema)}
61
+ resolver={resolverProp ?? resolver?.(props.schema)}
62
+ fieldResolver={fieldResolverProp ?? fieldResolver?.(schema)}
57
63
  {...rest}
58
64
  />
59
65
  </FieldsProvider>
@@ -18,6 +18,10 @@ export interface DisplayIfProps<
18
18
  isDisabled?: boolean
19
19
  isExact?: boolean
20
20
  condition?: (value: unknown, context: UseFormReturn<TFieldValues>) => boolean
21
+ onToggle?: (
22
+ conditionMatched: boolean,
23
+ context: UseFormReturn<TFieldValues>
24
+ ) => void
21
25
  }
22
26
  /**
23
27
  * Conditionally render parts of a form.
@@ -34,7 +38,11 @@ export const DisplayIf = <
34
38
  isDisabled,
35
39
  isExact,
36
40
  condition = (value) => !!value,
41
+ onToggle,
37
42
  }: DisplayIfProps<TFieldValues, TName>) => {
43
+ const initializedRef = React.useRef(false)
44
+ const matchesRef = React.useRef(false)
45
+
38
46
  const value = useWatch<TFieldValues>({
39
47
  name,
40
48
  defaultValue: defaultValue as any,
@@ -42,7 +50,20 @@ export const DisplayIf = <
42
50
  exact: isExact,
43
51
  })
44
52
  const context = useFormContext() as any
45
- return condition(value, context) ? children : null
53
+
54
+ const matches = condition(value, context)
55
+
56
+ React.useEffect(() => {
57
+ if (!initializedRef.current) {
58
+ initializedRef.current = true
59
+ return
60
+ }
61
+ if (matchesRef.current === matches) return
62
+ matchesRef.current = matches
63
+ onToggle?.(matches, context)
64
+ }, [value])
65
+
66
+ return matches ? children : null
46
67
  }
47
68
 
48
69
  DisplayIf.displayName = 'DisplayIf'
@@ -42,7 +42,7 @@ export interface NumberInputProps
42
42
 
43
43
  export const NumberInput = forwardRef<NumberInputProps, 'div'>((props, ref) => {
44
44
  const {
45
- hideStepper,
45
+ hideStepper = false,
46
46
  incrementIcon = <ChevronUpIcon />,
47
47
  decrementIcon = <ChevronDownIcon />,
48
48
  placeholder,
@@ -66,8 +66,4 @@ export const NumberInput = forwardRef<NumberInputProps, 'div'>((props, ref) => {
66
66
  )
67
67
  })
68
68
 
69
- NumberInput.defaultProps = {
70
- hideStepper: false,
71
- }
72
-
73
69
  NumberInput.displayName = 'NumberInput'
@@ -39,8 +39,4 @@ export const PinInput = forwardRef<PinInputProps, 'div'>((props, ref) => {
39
39
  )
40
40
  })
41
41
 
42
- PinInput.defaultProps = {
43
- pinLength: 4,
44
- }
45
-
46
42
  PinInput.displayName = 'PinInput'
@@ -28,9 +28,10 @@ export interface SubmitButtonProps extends ButtonProps {
28
28
  export const SubmitButton = forwardRef<SubmitButtonProps, 'button'>(
29
29
  (props, ref) => {
30
30
  const {
31
+ variant = 'primary',
31
32
  children = 'Submit',
32
- disableIfUntouched,
33
- disableIfInvalid,
33
+ disableIfUntouched = false,
34
+ disableIfInvalid = false,
34
35
  isDisabled: isDisabledProp,
35
36
  isLoading,
36
37
  ...rest
@@ -46,6 +47,7 @@ export const SubmitButton = forwardRef<SubmitButtonProps, 'button'>(
46
47
  <Button
47
48
  {...rest}
48
49
  ref={ref}
50
+ variant={variant}
49
51
  type="submit"
50
52
  isLoading={formState.isSubmitting || isLoading}
51
53
  isDisabled={isDisabled}
@@ -56,10 +58,4 @@ export const SubmitButton = forwardRef<SubmitButtonProps, 'button'>(
56
58
  }
57
59
  )
58
60
 
59
- SubmitButton.defaultProps = {
60
- variant: 'primary',
61
- disableIfUntouched: false,
62
- disableIfInvalid: false,
63
- }
64
-
65
61
  SubmitButton.displayName = 'SubmitButton'