@contember/react-form 2.0.0-alpha.26 → 2.0.0-alpha.28
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/development/index.cjs +6 -2
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +5 -1
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/components/FormCheckbox.cjs +9 -6
- package/dist/development/src/components/FormCheckbox.cjs.map +1 -1
- package/dist/development/src/components/FormCheckbox.js +10 -7
- package/dist/development/src/components/FormCheckbox.js.map +1 -1
- package/dist/development/src/components/FormError.cjs +5 -5
- package/dist/development/src/components/FormError.cjs.map +1 -1
- package/dist/development/src/components/FormError.js +6 -6
- package/dist/development/src/components/FormError.js.map +1 -1
- package/dist/development/src/components/FormFieldScope.cjs +24 -14
- package/dist/development/src/components/FormFieldScope.cjs.map +1 -1
- package/dist/development/src/components/FormFieldScope.js +25 -15
- package/dist/development/src/components/FormFieldScope.js.map +1 -1
- package/dist/development/src/components/FormFieldStateProvider.cjs +56 -0
- package/dist/development/src/components/FormFieldStateProvider.cjs.map +1 -0
- package/dist/development/src/components/FormFieldStateProvider.js +56 -0
- package/dist/development/src/components/FormFieldStateProvider.js.map +1 -0
- package/dist/development/src/components/FormHasManyRelationScope.cjs +24 -14
- package/dist/development/src/components/FormHasManyRelationScope.cjs.map +1 -1
- package/dist/development/src/components/FormHasManyRelationScope.js +25 -15
- package/dist/development/src/components/FormHasManyRelationScope.js.map +1 -1
- package/dist/development/src/components/FormHasOneRelationScope.cjs +25 -15
- package/dist/development/src/components/FormHasOneRelationScope.cjs.map +1 -1
- package/dist/development/src/components/FormHasOneRelationScope.js +25 -15
- package/dist/development/src/components/FormHasOneRelationScope.js.map +1 -1
- package/dist/development/src/components/FormInput.cjs +11 -6
- package/dist/development/src/components/FormInput.cjs.map +1 -1
- package/dist/development/src/components/FormInput.js +12 -7
- package/dist/development/src/components/FormInput.js.map +1 -1
- package/dist/development/src/components/FormLabel.cjs +7 -5
- package/dist/development/src/components/FormLabel.cjs.map +1 -1
- package/dist/development/src/components/FormLabel.js +8 -6
- package/dist/development/src/components/FormLabel.js.map +1 -1
- package/dist/development/src/components/FormRadioInput.cjs +10 -7
- package/dist/development/src/components/FormRadioInput.cjs.map +1 -1
- package/dist/development/src/components/FormRadioInput.js +11 -8
- package/dist/development/src/components/FormRadioInput.js.map +1 -1
- package/dist/development/src/contexts.cjs +7 -8
- package/dist/development/src/contexts.cjs.map +1 -1
- package/dist/development/src/contexts.js +8 -9
- package/dist/development/src/contexts.js.map +1 -1
- package/dist/production/index.cjs +6 -2
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +5 -1
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/components/FormCheckbox.cjs +7 -4
- package/dist/production/src/components/FormCheckbox.cjs.map +1 -1
- package/dist/production/src/components/FormCheckbox.js +8 -5
- package/dist/production/src/components/FormCheckbox.js.map +1 -1
- package/dist/production/src/components/FormError.cjs +5 -5
- package/dist/production/src/components/FormError.cjs.map +1 -1
- package/dist/production/src/components/FormError.js +6 -6
- package/dist/production/src/components/FormError.js.map +1 -1
- package/dist/production/src/components/FormFieldScope.cjs +16 -6
- package/dist/production/src/components/FormFieldScope.cjs.map +1 -1
- package/dist/production/src/components/FormFieldScope.js +17 -7
- package/dist/production/src/components/FormFieldScope.js.map +1 -1
- package/dist/production/src/components/FormFieldStateProvider.cjs +34 -0
- package/dist/production/src/components/FormFieldStateProvider.cjs.map +1 -0
- package/dist/production/src/components/FormFieldStateProvider.js +34 -0
- package/dist/production/src/components/FormFieldStateProvider.js.map +1 -0
- package/dist/production/src/components/FormHasManyRelationScope.cjs +16 -6
- package/dist/production/src/components/FormHasManyRelationScope.cjs.map +1 -1
- package/dist/production/src/components/FormHasManyRelationScope.js +17 -7
- package/dist/production/src/components/FormHasManyRelationScope.js.map +1 -1
- package/dist/production/src/components/FormHasOneRelationScope.cjs +17 -7
- package/dist/production/src/components/FormHasOneRelationScope.cjs.map +1 -1
- package/dist/production/src/components/FormHasOneRelationScope.js +17 -7
- package/dist/production/src/components/FormHasOneRelationScope.js.map +1 -1
- package/dist/production/src/components/FormInput.cjs +9 -4
- package/dist/production/src/components/FormInput.cjs.map +1 -1
- package/dist/production/src/components/FormInput.js +10 -5
- package/dist/production/src/components/FormInput.js.map +1 -1
- package/dist/production/src/components/FormLabel.cjs +7 -5
- package/dist/production/src/components/FormLabel.cjs.map +1 -1
- package/dist/production/src/components/FormLabel.js +8 -6
- package/dist/production/src/components/FormLabel.js.map +1 -1
- package/dist/production/src/components/FormRadioInput.cjs +8 -5
- package/dist/production/src/components/FormRadioInput.cjs.map +1 -1
- package/dist/production/src/components/FormRadioInput.js +9 -6
- package/dist/production/src/components/FormRadioInput.js.map +1 -1
- package/dist/production/src/contexts.cjs +7 -8
- package/dist/production/src/contexts.cjs.map +1 -1
- package/dist/production/src/contexts.js +8 -9
- package/dist/production/src/contexts.js.map +1 -1
- package/dist/types/components/FormCheckbox.d.ts.map +1 -1
- package/dist/types/components/FormFieldScope.d.ts +2 -1
- package/dist/types/components/FormFieldScope.d.ts.map +1 -1
- package/dist/types/components/FormFieldStateProvider.d.ts +36 -0
- package/dist/types/components/FormFieldStateProvider.d.ts.map +1 -0
- package/dist/types/components/FormHasManyRelationScope.d.ts +2 -1
- package/dist/types/components/FormHasManyRelationScope.d.ts.map +1 -1
- package/dist/types/components/FormHasOneRelationScope.d.ts +2 -1
- package/dist/types/components/FormHasOneRelationScope.d.ts.map +1 -1
- package/dist/types/components/FormInput.d.ts.map +1 -1
- package/dist/types/components/FormLabel.d.ts.map +1 -1
- package/dist/types/components/FormRadioInput.d.ts.map +1 -1
- package/dist/types/components/index.d.ts +1 -0
- package/dist/types/components/index.d.ts.map +1 -1
- package/dist/types/contexts.d.ts +10 -6
- package/dist/types/contexts.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/types/index.d.ts +12 -0
- package/dist/types/types/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/components/FormCheckbox.tsx +17 -6
- package/src/components/FormError.tsx +6 -6
- package/src/components/FormFieldScope.tsx +17 -12
- package/src/components/FormFieldStateProvider.tsx +71 -0
- package/src/components/FormHasManyRelationScope.tsx +18 -13
- package/src/components/FormHasOneRelationScope.tsx +17 -13
- package/src/components/FormInput.tsx +12 -5
- package/src/components/FormLabel.tsx +8 -6
- package/src/components/FormRadioInput.tsx +11 -6
- package/src/components/index.ts +1 -0
- package/src/contexts.tsx +18 -0
- package/src/tsconfig.json +5 -2
- package/src/types/index.ts +13 -0
- package/src/contexts.ts +0 -12
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
import { ErrorAccessor } from '@contember/react-binding';
|
|
1
2
|
import * as React from 'react';
|
|
2
3
|
export type FormInputHandler = {
|
|
3
4
|
parseValue: (value: string) => any;
|
|
4
5
|
formatValue: (value: any) => string;
|
|
5
6
|
defaultInputProps?: React.InputHTMLAttributes<HTMLInputElement>;
|
|
6
7
|
};
|
|
8
|
+
export type FormFieldState = {
|
|
9
|
+
htmlId: string;
|
|
10
|
+
errors: ErrorAccessor.Error[];
|
|
11
|
+
required: boolean;
|
|
12
|
+
dirty: boolean;
|
|
13
|
+
field?: {
|
|
14
|
+
entityName: string;
|
|
15
|
+
fieldName: string;
|
|
16
|
+
enumName?: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
7
19
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,MAAM,MAAM,gBAAgB,GAAG;IAC9B,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,GAAG,CAAA;IAClC,WAAW,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,MAAM,CAAA;IACnC,iBAAiB,CAAC,EAAE,KAAK,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,CAAA;CAC/D,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AACxD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,MAAM,MAAM,gBAAgB,GAAG;IAC9B,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,GAAG,CAAA;IAClC,WAAW,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,MAAM,CAAA;IACnC,iBAAiB,CAAC,EAAE,KAAK,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,CAAA;CAC/D,CAAA;AAED,MAAM,MAAM,cAAc,GAAG;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,aAAa,CAAC,KAAK,EAAE,CAAA;IAC7B,QAAQ,EAAE,OAAO,CAAA;IACjB,KAAK,EAAE,OAAO,CAAA;IACd,KAAK,CAAC,EAAE;QACP,UAAU,EAAE,MAAM,CAAA;QAClB,SAAS,EAAE,MAAM,CAAA;QACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;KACjB,CAAA;CACD,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/react-form",
|
|
3
3
|
"license": "Apache-2.0",
|
|
4
|
-
"version": "2.0.0-alpha.
|
|
4
|
+
"version": "2.0.0-alpha.28",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "./dist/production/index.js",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"directory": "packages/react-form"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@contember/react-binding": "2.0.0-alpha.
|
|
37
|
-
"@contember/react-utils": "2.0.0-alpha.
|
|
38
|
-
"@contember/utilities": "2.0.0-alpha.
|
|
36
|
+
"@contember/react-binding": "2.0.0-alpha.28",
|
|
37
|
+
"@contember/react-utils": "2.0.0-alpha.28",
|
|
38
|
+
"@contember/utilities": "2.0.0-alpha.28",
|
|
39
39
|
"@radix-ui/react-slot": "^1.1.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { ChangeEventHandler, ReactElement, useCallback, useEffect } from 'react'
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
Component,
|
|
5
|
+
Field,
|
|
6
|
+
OptionallyVariableFieldValue,
|
|
7
|
+
SugaredRelativeSingleField,
|
|
8
|
+
TreeNodeEnvironmentFactory,
|
|
9
|
+
useField,
|
|
10
|
+
} from '@contember/react-binding'
|
|
11
|
+
import { useFormFieldState } from '../contexts'
|
|
5
12
|
import { dataAttribute } from '@contember/utilities'
|
|
6
13
|
import { useFormInputValidationHandler } from '../hooks'
|
|
7
14
|
import { Slot } from '@radix-ui/react-slot'
|
|
@@ -16,10 +23,13 @@ export interface FormCheckboxProps {
|
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
export const FormCheckbox = Component<FormCheckboxProps>(({ field, isNonbearing, defaultValue, ...props }) => {
|
|
19
|
-
const id = useFormFieldId()
|
|
20
26
|
const accessor = useField<boolean>(field)
|
|
27
|
+
|
|
28
|
+
const formState = useFormFieldState()
|
|
29
|
+
const id = formState?.htmlId
|
|
30
|
+
const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0
|
|
31
|
+
const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges
|
|
21
32
|
const value = accessor.value
|
|
22
|
-
const errors = useFormError() ?? accessor.errors?.errors ?? []
|
|
23
33
|
const accessorGetter = accessor.getAccessor
|
|
24
34
|
const { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)
|
|
25
35
|
const [checkboxRef, setCheckboxRef] = React.useState<HTMLInputElement | null>(null)
|
|
@@ -40,8 +50,9 @@ export const FormCheckbox = Component<FormCheckboxProps>(({ field, isNonbearing,
|
|
|
40
50
|
type="checkbox"
|
|
41
51
|
checked={accessor.value === true}
|
|
42
52
|
data-state={value === null ? 'indeterminate' : (value ? 'checked' : 'unchecked')}
|
|
43
|
-
data-invalid={dataAttribute(
|
|
44
|
-
|
|
53
|
+
data-invalid={dataAttribute(hasErrors)}
|
|
54
|
+
data-dirty={dataAttribute(dirty)}
|
|
55
|
+
id={id ? `${id}-input` : undefined}
|
|
45
56
|
onFocus={onFocus}
|
|
46
57
|
onBlur={onBlur}
|
|
47
58
|
onChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { cloneElement, ReactElement, ReactNode, useMemo } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { useFormFieldState } from '../contexts'
|
|
3
3
|
import { BindingError, ErrorAccessor } from '@contember/react-binding'
|
|
4
4
|
|
|
5
5
|
export const FormError = ({ children, formatter }: {
|
|
6
6
|
formatter: (errors: ErrorAccessor.Error[]) => ReactNode[]
|
|
7
7
|
children: ReactElement
|
|
8
8
|
}) => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const formatted = useMemo(() => formatter(errors ?? []).filter((it, index, arr) => arr.indexOf(it) === index), [errors, formatter])
|
|
12
|
-
if (errors === undefined || id === undefined) {
|
|
9
|
+
const formState = useFormFieldState()
|
|
10
|
+
if (!formState) {
|
|
13
11
|
throw new BindingError('FormError must be used inside a FormField')
|
|
14
12
|
}
|
|
13
|
+
const { errors, htmlId } = formState
|
|
14
|
+
const formatted = useMemo(() => formatter(errors ?? []).filter((it, index, arr) => arr.indexOf(it) === index), [errors, formatter])
|
|
15
15
|
return formatted.map((it, index) => {
|
|
16
16
|
return cloneElement(children, {
|
|
17
17
|
key: index,
|
|
18
|
-
...{ id: `${
|
|
18
|
+
...(htmlId ? { id: `${htmlId}-error-${index}` } : {}),
|
|
19
19
|
children: it,
|
|
20
20
|
})
|
|
21
21
|
})
|
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
|
-
import {
|
|
3
|
-
import { FormErrorContext, FormFieldIdContext } from '../contexts'
|
|
2
|
+
import { useMemo } from 'react'
|
|
4
3
|
import { SugaredRelativeSingleField, useField } from '@contember/react-binding'
|
|
5
|
-
|
|
6
|
-
const emptyArr: [] = []
|
|
4
|
+
import { FormFieldStateProvider } from './FormFieldStateProvider'
|
|
7
5
|
|
|
8
6
|
export type FormFieldScopeProps = {
|
|
9
7
|
field: SugaredRelativeSingleField['field']
|
|
10
8
|
children: React.ReactNode
|
|
9
|
+
required?: boolean
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
export const FormFieldScope = ({ field, children }: FormFieldScopeProps) => {
|
|
14
|
-
const id = useId()
|
|
12
|
+
export const FormFieldScope = ({ field, children, required }: FormFieldScopeProps) => {
|
|
15
13
|
const fieldAccessor = useField({ field })
|
|
16
|
-
const
|
|
14
|
+
const entityName = fieldAccessor.getParent().name
|
|
15
|
+
const fieldName = fieldAccessor.fieldName
|
|
16
|
+
const enumName = fieldAccessor.schema.enumName ?? undefined
|
|
17
|
+
const fieldInfo = useMemo(() => ({ entityName, fieldName, enumName }), [entityName, fieldName, enumName])
|
|
18
|
+
|
|
17
19
|
return (
|
|
18
|
-
<
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
<FormFieldStateProvider
|
|
21
|
+
errors={fieldAccessor.errors?.errors}
|
|
22
|
+
required={required ?? !fieldAccessor.schema.nullable}
|
|
23
|
+
dirty={fieldAccessor.hasUnpersistedChanges}
|
|
24
|
+
field={fieldInfo}
|
|
25
|
+
>
|
|
26
|
+
{children}
|
|
27
|
+
</FormFieldStateProvider>
|
|
23
28
|
)
|
|
24
29
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ErrorAccessor } from '@contember/react-binding'
|
|
2
|
+
import { useEffect, useId, useMemo } from 'react'
|
|
3
|
+
import { FormFieldStateContext, useFormFieldState } from '../contexts'
|
|
4
|
+
import { FormFieldState } from '../types'
|
|
5
|
+
|
|
6
|
+
const emptyArr: [] = []
|
|
7
|
+
|
|
8
|
+
export type FormFieldStateProvider =
|
|
9
|
+
& Partial<FormFieldState>
|
|
10
|
+
& {
|
|
11
|
+
children: React.ReactNode
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const FormFieldStateProvider = ({ children, required = false, errors = emptyArr, dirty = false, htmlId, field }: FormFieldStateProvider) => {
|
|
15
|
+
const generatedId = useId()
|
|
16
|
+
htmlId ??= generatedId
|
|
17
|
+
const value = useMemo(() => ({ htmlId, required, errors, dirty, field }), [htmlId, required, errors, dirty, field])
|
|
18
|
+
return <FormFieldStateContext.Provider value={value}>{children}</FormFieldStateContext.Provider>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* BC for
|
|
23
|
+
* <FormFieldIdContext.Provider value={id}>
|
|
24
|
+
* <FormErrorContext.Provider value={[]}>
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated use `FormFieldStateProvider` instead
|
|
29
|
+
*/
|
|
30
|
+
export const FormFieldIdContext = {
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated use `FormFieldStateProvider` instead
|
|
33
|
+
*/
|
|
34
|
+
Provider: ({ children, value }: { children: React.ReactNode; value: string }) => {
|
|
35
|
+
const state = useFormFieldState()
|
|
36
|
+
if (import.meta.env.DEV) {
|
|
37
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
console.warn('FormFieldIdContext.Provider is deprecated, use FormFieldStateProvider instead')
|
|
40
|
+
}, [])
|
|
41
|
+
}
|
|
42
|
+
return (
|
|
43
|
+
<FormFieldStateProvider {...state} htmlId={value}>
|
|
44
|
+
{children}
|
|
45
|
+
</FormFieldStateProvider>
|
|
46
|
+
)
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated use `FormFieldStateProvider` instead
|
|
52
|
+
*/
|
|
53
|
+
export const FormErrorContext = {
|
|
54
|
+
/**
|
|
55
|
+
* @deprecated use `FormFieldStateProvider` instead
|
|
56
|
+
*/
|
|
57
|
+
Provider: ({ children, value }: { children: React.ReactNode; value: ErrorAccessor.Error[] }) => {
|
|
58
|
+
const state = useFormFieldState()
|
|
59
|
+
if (import.meta.env.DEV) {
|
|
60
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
console.warn('FormErrorContext.Provider is deprecated, use FormFieldStateProvider instead')
|
|
63
|
+
}, [])
|
|
64
|
+
}
|
|
65
|
+
return (
|
|
66
|
+
<FormFieldStateProvider {...state} errors={value}>
|
|
67
|
+
{children}
|
|
68
|
+
</FormFieldStateProvider>
|
|
69
|
+
)
|
|
70
|
+
},
|
|
71
|
+
}
|
|
@@ -1,24 +1,29 @@
|
|
|
1
|
-
import { SugaredRelativeEntityList, useEntityList } from '@contember/react-binding'
|
|
1
|
+
import { HasManyRelationMarker, SugaredRelativeEntityList, useEntityList } from '@contember/react-binding'
|
|
2
2
|
import * as React from 'react'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
const emptyArr: [] = []
|
|
3
|
+
import { FormFieldStateProvider } from './FormFieldStateProvider'
|
|
4
|
+
import { useMemo } from 'react'
|
|
7
5
|
|
|
8
6
|
export type FormHasManyRelationScopeProps = {
|
|
9
7
|
field: SugaredRelativeEntityList['field']
|
|
10
8
|
children: React.ReactNode
|
|
9
|
+
required?: boolean
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
export const FormHasManyRelationScope = ({ field, children }: FormHasManyRelationScopeProps) => {
|
|
14
|
-
const id = useId()
|
|
12
|
+
export const FormHasManyRelationScope = ({ field, children, required }: FormHasManyRelationScopeProps) => {
|
|
15
13
|
const entityRelation = useEntityList({ field })
|
|
16
|
-
const
|
|
14
|
+
const entityName = entityRelation.getParent()!.name
|
|
15
|
+
const marker = entityRelation.getMarker() as HasManyRelationMarker
|
|
16
|
+
const fieldName = marker.parameters.field
|
|
17
|
+
const fieldInfo = useMemo(() => ({ entityName, fieldName }), [entityName, fieldName])
|
|
18
|
+
|
|
17
19
|
return (
|
|
18
|
-
<
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
<FormFieldStateProvider
|
|
21
|
+
errors={entityRelation.errors?.errors}
|
|
22
|
+
dirty={entityRelation.hasUnpersistedChanges}
|
|
23
|
+
required={required}
|
|
24
|
+
field={fieldInfo}
|
|
25
|
+
>
|
|
26
|
+
{children}
|
|
27
|
+
</FormFieldStateProvider>
|
|
23
28
|
)
|
|
24
29
|
}
|
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
const emptyArr: [] = []
|
|
2
|
+
import { HasOneRelationMarker, SugaredRelativeSingleEntity, useEntity } from '@contember/react-binding'
|
|
3
|
+
import { FormFieldStateProvider } from './FormFieldStateProvider'
|
|
4
|
+
import { useMemo } from 'react'
|
|
7
5
|
|
|
8
6
|
export type FormHasOneRelationScopeProps = {
|
|
9
7
|
field: SugaredRelativeSingleEntity['field']
|
|
10
8
|
children: React.ReactNode
|
|
9
|
+
required?: boolean
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
export const FormHasOneRelationScope = ({ field, children }: FormHasOneRelationScopeProps) => {
|
|
14
|
-
const id = useId()
|
|
12
|
+
export const FormHasOneRelationScope = ({ field, children, required }: FormHasOneRelationScopeProps) => {
|
|
15
13
|
const entityRelation = useEntity({ field })
|
|
16
|
-
const
|
|
14
|
+
const marker = entityRelation.getMarker() as HasOneRelationMarker
|
|
15
|
+
const fieldName = marker.parameters.field
|
|
16
|
+
const entityName = entityRelation.getParent()!.name
|
|
17
|
+
const fieldInfo = useMemo(() => ({ entityName, fieldName }), [entityName, fieldName])
|
|
17
18
|
return (
|
|
18
|
-
<
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
<FormFieldStateProvider
|
|
20
|
+
errors={entityRelation.errors?.errors}
|
|
21
|
+
dirty={entityRelation.hasUnpersistedChanges}
|
|
22
|
+
required={required}
|
|
23
|
+
field={fieldInfo}
|
|
24
|
+
>
|
|
25
|
+
{children}
|
|
26
|
+
</FormFieldStateProvider>
|
|
23
27
|
)
|
|
24
28
|
}
|
|
@@ -4,7 +4,7 @@ import { Slot } from '@radix-ui/react-slot'
|
|
|
4
4
|
import { Component, Field, OptionallyVariableFieldValue, SugaredRelativeSingleField, useField } from '@contember/react-binding'
|
|
5
5
|
import { dataAttribute } from '@contember/utilities'
|
|
6
6
|
import { useFormInputHandler } from '../internal/useFormInputHandler'
|
|
7
|
-
import {
|
|
7
|
+
import { useFormFieldState } from '../contexts'
|
|
8
8
|
import { useFormInputValidationHandler } from '../hooks/useFormInputValidationHandler'
|
|
9
9
|
import { FormInputHandler } from '../types'
|
|
10
10
|
|
|
@@ -21,9 +21,14 @@ export interface FormInputProps {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export const FormInput = Component<FormInputProps>(({ field, isNonbearing, defaultValue, formatValue: formatValueIn, parseValue: parseValueIn, ...props }) => {
|
|
24
|
-
const id = useFormFieldId()
|
|
25
24
|
const accessor = useField(field)
|
|
26
|
-
|
|
25
|
+
|
|
26
|
+
const formState = useFormFieldState()
|
|
27
|
+
const id = formState?.htmlId
|
|
28
|
+
const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0
|
|
29
|
+
const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges
|
|
30
|
+
const required = formState?.required ?? !accessor.schema.nullable
|
|
31
|
+
|
|
27
32
|
const { parseValue, formatValue, defaultInputProps } = useFormInputHandler(accessor, { formatValue: formatValueIn, parseValue: parseValueIn })
|
|
28
33
|
const accessorGetter = accessor.getAccessor
|
|
29
34
|
const { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)
|
|
@@ -32,7 +37,9 @@ export const FormInput = Component<FormInputProps>(({ field, isNonbearing, defau
|
|
|
32
37
|
<SlotInput
|
|
33
38
|
ref={ref}
|
|
34
39
|
value={formatValue(accessor.value)}
|
|
35
|
-
data-invalid={dataAttribute(
|
|
40
|
+
data-invalid={dataAttribute(hasErrors)}
|
|
41
|
+
data-dirty={dataAttribute(dirty)}
|
|
42
|
+
data-required={dataAttribute(required)}
|
|
36
43
|
onFocus={onFocus}
|
|
37
44
|
onBlur={onBlur}
|
|
38
45
|
onChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {
|
|
@@ -40,7 +47,7 @@ export const FormInput = Component<FormInputProps>(({ field, isNonbearing, defau
|
|
|
40
47
|
}, [accessorGetter, parseValue])}
|
|
41
48
|
{...defaultInputProps}
|
|
42
49
|
id={id ? `${id}-input` : undefined}
|
|
43
|
-
required={
|
|
50
|
+
required={required}
|
|
44
51
|
{...props}
|
|
45
52
|
/>
|
|
46
53
|
)
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { Slot } from '@radix-ui/react-slot'
|
|
3
3
|
import { dataAttribute } from '@contember/utilities'
|
|
4
|
-
import {
|
|
4
|
+
import { useFormFieldState } from '../contexts'
|
|
5
5
|
import { BindingError } from '@contember/react-binding'
|
|
6
6
|
|
|
7
7
|
export const FormLabel = (props: {
|
|
8
8
|
children: React.ReactElement
|
|
9
9
|
}) => {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
throw new BindingError('FormLabel must be used inside a FormField')
|
|
10
|
+
const formState = useFormFieldState()
|
|
11
|
+
if (!formState) {
|
|
12
|
+
throw new BindingError('FormError must be used inside a FormField')
|
|
14
13
|
}
|
|
14
|
+
const { errors, htmlId, dirty, required } = formState
|
|
15
15
|
return (
|
|
16
16
|
<Slot
|
|
17
17
|
data-invalid={dataAttribute(errors.length > 0)}
|
|
18
|
-
{
|
|
18
|
+
data-dirty={dataAttribute(dirty)}
|
|
19
|
+
data-required={dataAttribute(required)}
|
|
20
|
+
{...(htmlId ? { htmlFor: `${htmlId}-input` } : {})}
|
|
19
21
|
{...props}
|
|
20
22
|
/>
|
|
21
23
|
)
|
|
@@ -3,7 +3,7 @@ import { dataAttribute } from '@contember/utilities'
|
|
|
3
3
|
import { Slot } from '@radix-ui/react-slot'
|
|
4
4
|
import * as React from 'react'
|
|
5
5
|
import { ChangeEventHandler, useCallback } from 'react'
|
|
6
|
-
import {
|
|
6
|
+
import { useFormFieldState } from '../contexts'
|
|
7
7
|
import { useFormInputValidationHandler } from '../hooks'
|
|
8
8
|
|
|
9
9
|
const SlotInput = Slot as React.ForwardRefExoticComponent<React.RefAttributes<HTMLInputElement> & React.InputHTMLAttributes<HTMLInputElement>>
|
|
@@ -17,9 +17,13 @@ export interface FormRadioItemProps {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export const FormRadioInput = Component<FormRadioItemProps>(({ field, value, defaultValue, isNonbearing, ...props }) => {
|
|
20
|
-
const id = useFormFieldId()
|
|
21
20
|
const accessor = useField(field)
|
|
22
|
-
|
|
21
|
+
|
|
22
|
+
const formState = useFormFieldState()
|
|
23
|
+
const id = formState?.htmlId
|
|
24
|
+
const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0
|
|
25
|
+
const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges
|
|
26
|
+
|
|
23
27
|
const accessorGetter = accessor.getAccessor
|
|
24
28
|
const { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)
|
|
25
29
|
|
|
@@ -33,9 +37,10 @@ export const FormRadioInput = Component<FormRadioItemProps>(({ field, value, def
|
|
|
33
37
|
type="radio"
|
|
34
38
|
value={typeof value === 'string' ? value : undefined}
|
|
35
39
|
checked={accessor.value === value}
|
|
36
|
-
data-invalid={dataAttribute(
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
data-invalid={dataAttribute(hasErrors)}
|
|
41
|
+
data-dirty={dataAttribute(dirty)}
|
|
42
|
+
name={id ? id + '-input' : undefined}
|
|
43
|
+
id={id ? `${id}-input` : undefined}
|
|
39
44
|
onFocus={onFocus}
|
|
40
45
|
onBlur={onBlur}
|
|
41
46
|
onChange={handleChange}
|
package/src/components/index.ts
CHANGED
package/src/contexts.tsx
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createContext } from '@contember/react-utils'
|
|
2
|
+
import { FormFieldState } from './types'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const FormFieldStateContext_ = createContext<FormFieldState | undefined>('FormFieldStateContext', undefined)
|
|
6
|
+
export const FormFieldStateContext = FormFieldStateContext_[0]
|
|
7
|
+
export const useFormFieldState = FormFieldStateContext_[1]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated use `useFormState` instead
|
|
12
|
+
*/
|
|
13
|
+
export const useFormFieldId = () => useFormFieldState()?.htmlId
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated use `useFormState` instead
|
|
17
|
+
*/
|
|
18
|
+
export const useFormError = () => useFormFieldState()?.errors
|
package/src/tsconfig.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../../tsconfig.settings.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
"outDir": "../dist/types"
|
|
4
|
+
"outDir": "../dist/types",
|
|
5
|
+
"types": [
|
|
6
|
+
"vite/client"
|
|
7
|
+
]
|
|
5
8
|
},
|
|
6
9
|
"references": [
|
|
7
10
|
{ "path": "../../react-binding/src" },
|
|
8
11
|
{ "path": "../../react-utils/src" },
|
|
9
|
-
{ "path": "../../utilities/src" }
|
|
12
|
+
{ "path": "../../utilities/src" }
|
|
10
13
|
]
|
|
11
14
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ErrorAccessor } from '@contember/react-binding'
|
|
1
2
|
import * as React from 'react'
|
|
2
3
|
|
|
3
4
|
export type FormInputHandler = {
|
|
@@ -5,3 +6,15 @@ export type FormInputHandler = {
|
|
|
5
6
|
formatValue: (value: any) => string
|
|
6
7
|
defaultInputProps?: React.InputHTMLAttributes<HTMLInputElement>
|
|
7
8
|
}
|
|
9
|
+
|
|
10
|
+
export type FormFieldState = {
|
|
11
|
+
htmlId: string
|
|
12
|
+
errors: ErrorAccessor.Error[]
|
|
13
|
+
required: boolean
|
|
14
|
+
dirty: boolean
|
|
15
|
+
field?: {
|
|
16
|
+
entityName: string
|
|
17
|
+
fieldName: string
|
|
18
|
+
enumName?: string
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/contexts.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createContext } from '@contember/react-utils'
|
|
2
|
-
import { ErrorAccessor } from '@contember/react-binding'
|
|
3
|
-
|
|
4
|
-
const FormFieldIdContext_ = createContext<string | undefined>('FormFieldIdContext', undefined)
|
|
5
|
-
/** @internal */
|
|
6
|
-
export const FormFieldIdContext = FormFieldIdContext_[0]
|
|
7
|
-
export const useFormFieldId = FormFieldIdContext_[1]
|
|
8
|
-
|
|
9
|
-
const FormErrorContext_ = createContext<ErrorAccessor.Error[] | undefined>('FormErrorContext', undefined)
|
|
10
|
-
/** @internal */
|
|
11
|
-
export const FormErrorContext = FormErrorContext_[0]
|
|
12
|
-
export const useFormError = FormErrorContext_[1]
|