@maratus-registry/field 0.2.1 → 0.4.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.
@@ -1,161 +0,0 @@
1
- import type {
2
- ControlElement,
3
- ControlRenderProps,
4
- FieldContextValue,
5
- UseControlOptions,
6
- UseControlResult,
7
- WithValidity,
8
- } from './Field.types'
9
- import clsx from 'clsx'
10
- import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
11
- import './Field.css'
12
-
13
- export function useControl(options: UseControlOptions = {}): UseControlResult {
14
- const {
15
- className,
16
- onBlur,
17
- onChange,
18
- onFocus,
19
- onInput,
20
- onInvalid,
21
- role,
22
- ...controlRootProps
23
- } = options
24
- const field = useRequiredFieldContext('Control')
25
- const relationshipProps = {
26
- 'aria-busy': field.isLoading ? true : undefined,
27
- 'aria-describedby': field.description ? field.descriptionId : undefined,
28
- 'aria-disabled': field.isLoading ? true : undefined,
29
- 'aria-errormessage':
30
- field.visibleErrors.length > 0 ? field.errorId : undefined,
31
- 'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
32
- className: clsx('maratus__Field__control', className),
33
- 'data-loading': field.isLoading ? '' : undefined,
34
- id: field.controlId,
35
- } satisfies Pick<
36
- ControlRenderProps,
37
- | 'aria-busy'
38
- | 'aria-describedby'
39
- | 'aria-disabled'
40
- | 'aria-errormessage'
41
- | 'aria-invalid'
42
- | 'className'
43
- | 'data-loading'
44
- | 'id'
45
- >
46
- const validityHandlerProps = composeValidityHandlerProps(
47
- {
48
- onBlur,
49
- onChange,
50
- onFocus,
51
- onInput,
52
- onInvalid,
53
- },
54
- field.updateValidityState,
55
- )
56
-
57
- if (role) {
58
- return {
59
- controlProps: {
60
- ...controlRootProps,
61
- ...relationshipProps,
62
- ...(field.isReadOnly ? { 'aria-readonly': true } : {}),
63
- ...(field.isRequired ? { 'aria-required': true } : {}),
64
- ...validityHandlerProps,
65
- role,
66
- },
67
- withValidity,
68
- }
69
- }
70
-
71
- return {
72
- controlProps: {
73
- ...controlRootProps,
74
- ...relationshipProps,
75
- ...(field.isLoading ? { disabled: true } : {}),
76
- name: field.name,
77
- ...validityHandlerProps,
78
- ...(field.isReadOnly ? { readOnly: true } : {}),
79
- ...(field.isRequired ? { required: true } : {}),
80
- },
81
- withValidity,
82
- }
83
- }
84
-
85
- const withValidity: WithValidity = (event, validity) => {
86
- const currentTarget = Object.create(
87
- Object.getPrototypeOf(event.currentTarget),
88
- ) as typeof event.currentTarget & { validity: ValidityState }
89
-
90
- Object.assign(currentTarget, event.currentTarget, {
91
- validity: createValidityState(validity),
92
- })
93
-
94
- return {
95
- ...event,
96
- currentTarget,
97
- }
98
- }
99
-
100
- function composeValidityHandlerProps(
101
- userHandlers: Pick<
102
- UseControlOptions,
103
- 'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
104
- >,
105
- updateValidityState: FieldContextValue['updateValidityState'],
106
- ): Pick<
107
- ControlRenderProps,
108
- 'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
109
- > {
110
- return {
111
- onBlur: (event) => {
112
- userHandlers.onBlur?.(event)
113
- updateValidityState('blur', getValidityControl(event))
114
- },
115
- onChange: (event) => {
116
- userHandlers.onChange?.(event)
117
- updateValidityState('change', getValidityControl(event))
118
- },
119
- onFocus: (event) => {
120
- userHandlers.onFocus?.(event)
121
- updateValidityState('focus', getValidityControl(event))
122
- },
123
- onInput: (event) => {
124
- userHandlers.onInput?.(event)
125
- updateValidityState('input', getValidityControl(event))
126
- },
127
- onInvalid: (event) => {
128
- userHandlers.onInvalid?.(event)
129
- updateValidityState('invalid', getValidityControl(event))
130
- },
131
- }
132
- }
133
-
134
- function getValidityControl(event: { currentTarget: ControlElement }) {
135
- return event.currentTarget as ControlElement & { validity: ValidityState }
136
- }
137
-
138
- const validityStateKeys = [
139
- 'badInput',
140
- 'customError',
141
- 'patternMismatch',
142
- 'rangeOverflow',
143
- 'rangeUnderflow',
144
- 'stepMismatch',
145
- 'tooLong',
146
- 'tooShort',
147
- 'typeMismatch',
148
- 'valueMissing',
149
- ] as const satisfies readonly (keyof Omit<ValidityState, 'valid'>)[]
150
-
151
- function createValidityState(validity: Partial<ValidityState>): ValidityState {
152
- const errors = Object.fromEntries(
153
- validityStateKeys.map((key) => [key, validity[key] ?? false]),
154
- ) as Record<(typeof validityStateKeys)[number], boolean>
155
-
156
- return {
157
- ...errors,
158
- valid:
159
- validity.valid ?? validityStateKeys.every((key) => errors[key] === false),
160
- }
161
- }
@@ -1,161 +0,0 @@
1
- import type {
2
- ControlElement,
3
- ControlRenderProps,
4
- FieldContextValue,
5
- UseControlOptions,
6
- UseControlResult,
7
- WithValidity,
8
- } from './Field.types'
9
- import clsx from 'clsx'
10
- import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
11
- import styles from './Field.module.css'
12
-
13
- export function useControl(options: UseControlOptions = {}): UseControlResult {
14
- const {
15
- className,
16
- onBlur,
17
- onChange,
18
- onFocus,
19
- onInput,
20
- onInvalid,
21
- role,
22
- ...controlRootProps
23
- } = options
24
- const field = useRequiredFieldContext('Control')
25
- const relationshipProps = {
26
- 'aria-busy': field.isLoading ? true : undefined,
27
- 'aria-describedby': field.description ? field.descriptionId : undefined,
28
- 'aria-disabled': field.isLoading ? true : undefined,
29
- 'aria-errormessage':
30
- field.visibleErrors.length > 0 ? field.errorId : undefined,
31
- 'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
32
- className: clsx(styles.control, className),
33
- 'data-loading': field.isLoading ? '' : undefined,
34
- id: field.controlId,
35
- } satisfies Pick<
36
- ControlRenderProps,
37
- | 'aria-busy'
38
- | 'aria-describedby'
39
- | 'aria-disabled'
40
- | 'aria-errormessage'
41
- | 'aria-invalid'
42
- | 'className'
43
- | 'data-loading'
44
- | 'id'
45
- >
46
- const validityHandlerProps = composeValidityHandlerProps(
47
- {
48
- onBlur,
49
- onChange,
50
- onFocus,
51
- onInput,
52
- onInvalid,
53
- },
54
- field.updateValidityState,
55
- )
56
-
57
- if (role) {
58
- return {
59
- controlProps: {
60
- ...controlRootProps,
61
- ...relationshipProps,
62
- ...(field.isReadOnly ? { 'aria-readonly': true } : {}),
63
- ...(field.isRequired ? { 'aria-required': true } : {}),
64
- ...validityHandlerProps,
65
- role,
66
- },
67
- withValidity,
68
- }
69
- }
70
-
71
- return {
72
- controlProps: {
73
- ...controlRootProps,
74
- ...relationshipProps,
75
- ...(field.isLoading ? { disabled: true } : {}),
76
- name: field.name,
77
- ...validityHandlerProps,
78
- ...(field.isReadOnly ? { readOnly: true } : {}),
79
- ...(field.isRequired ? { required: true } : {}),
80
- },
81
- withValidity,
82
- }
83
- }
84
-
85
- const withValidity: WithValidity = (event, validity) => {
86
- const currentTarget = Object.create(
87
- Object.getPrototypeOf(event.currentTarget),
88
- ) as typeof event.currentTarget & { validity: ValidityState }
89
-
90
- Object.assign(currentTarget, event.currentTarget, {
91
- validity: createValidityState(validity),
92
- })
93
-
94
- return {
95
- ...event,
96
- currentTarget,
97
- }
98
- }
99
-
100
- function composeValidityHandlerProps(
101
- userHandlers: Pick<
102
- UseControlOptions,
103
- 'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
104
- >,
105
- updateValidityState: FieldContextValue['updateValidityState'],
106
- ): Pick<
107
- ControlRenderProps,
108
- 'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
109
- > {
110
- return {
111
- onBlur: (event) => {
112
- userHandlers.onBlur?.(event)
113
- updateValidityState('blur', getValidityControl(event))
114
- },
115
- onChange: (event) => {
116
- userHandlers.onChange?.(event)
117
- updateValidityState('change', getValidityControl(event))
118
- },
119
- onFocus: (event) => {
120
- userHandlers.onFocus?.(event)
121
- updateValidityState('focus', getValidityControl(event))
122
- },
123
- onInput: (event) => {
124
- userHandlers.onInput?.(event)
125
- updateValidityState('input', getValidityControl(event))
126
- },
127
- onInvalid: (event) => {
128
- userHandlers.onInvalid?.(event)
129
- updateValidityState('invalid', getValidityControl(event))
130
- },
131
- }
132
- }
133
-
134
- function getValidityControl(event: { currentTarget: ControlElement }) {
135
- return event.currentTarget as ControlElement & { validity: ValidityState }
136
- }
137
-
138
- const validityStateKeys = [
139
- 'badInput',
140
- 'customError',
141
- 'patternMismatch',
142
- 'rangeOverflow',
143
- 'rangeUnderflow',
144
- 'stepMismatch',
145
- 'tooLong',
146
- 'tooShort',
147
- 'typeMismatch',
148
- 'valueMissing',
149
- ] as const satisfies readonly (keyof Omit<ValidityState, 'valid'>)[]
150
-
151
- function createValidityState(validity: Partial<ValidityState>): ValidityState {
152
- const errors = Object.fromEntries(
153
- validityStateKeys.map((key) => [key, validity[key] ?? false]),
154
- ) as Record<(typeof validityStateKeys)[number], boolean>
155
-
156
- return {
157
- ...errors,
158
- valid:
159
- validity.valid ?? validityStateKeys.every((key) => errors[key] === false),
160
- }
161
- }
@@ -1,161 +0,0 @@
1
- import type {
2
- ControlElement,
3
- ControlRenderProps,
4
- FieldContextValue,
5
- UseControlOptions,
6
- UseControlResult,
7
- WithValidity,
8
- } from './Field.types'
9
- import clsx from 'clsx'
10
- import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
11
- import './Field.css'
12
-
13
- export function useControl(options: UseControlOptions = {}): UseControlResult {
14
- const {
15
- className,
16
- onBlur,
17
- onChange,
18
- onFocus,
19
- onInput,
20
- onInvalid,
21
- role,
22
- ...controlRootProps
23
- } = options
24
- const field = useRequiredFieldContext('Control')
25
- const relationshipProps = {
26
- 'aria-busy': field.isLoading ? true : undefined,
27
- 'aria-describedby': field.description ? field.descriptionId : undefined,
28
- 'aria-disabled': field.isLoading ? true : undefined,
29
- 'aria-errormessage':
30
- field.visibleErrors.length > 0 ? field.errorId : undefined,
31
- 'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
32
- className: clsx('maratus__Field__control', className),
33
- 'data-loading': field.isLoading ? '' : undefined,
34
- id: field.controlId,
35
- } satisfies Pick<
36
- ControlRenderProps,
37
- | 'aria-busy'
38
- | 'aria-describedby'
39
- | 'aria-disabled'
40
- | 'aria-errormessage'
41
- | 'aria-invalid'
42
- | 'className'
43
- | 'data-loading'
44
- | 'id'
45
- >
46
- const validityHandlerProps = composeValidityHandlerProps(
47
- {
48
- onBlur,
49
- onChange,
50
- onFocus,
51
- onInput,
52
- onInvalid,
53
- },
54
- field.updateValidityState,
55
- )
56
-
57
- if (role) {
58
- return {
59
- controlProps: {
60
- ...controlRootProps,
61
- ...relationshipProps,
62
- ...(field.isReadOnly ? { 'aria-readonly': true } : {}),
63
- ...(field.isRequired ? { 'aria-required': true } : {}),
64
- ...validityHandlerProps,
65
- role,
66
- },
67
- withValidity,
68
- }
69
- }
70
-
71
- return {
72
- controlProps: {
73
- ...controlRootProps,
74
- ...relationshipProps,
75
- ...(field.isLoading ? { disabled: true } : {}),
76
- name: field.name,
77
- ...validityHandlerProps,
78
- ...(field.isReadOnly ? { readOnly: true } : {}),
79
- ...(field.isRequired ? { required: true } : {}),
80
- },
81
- withValidity,
82
- }
83
- }
84
-
85
- const withValidity: WithValidity = (event, validity) => {
86
- const currentTarget = Object.create(
87
- Object.getPrototypeOf(event.currentTarget),
88
- ) as typeof event.currentTarget & { validity: ValidityState }
89
-
90
- Object.assign(currentTarget, event.currentTarget, {
91
- validity: createValidityState(validity),
92
- })
93
-
94
- return {
95
- ...event,
96
- currentTarget,
97
- }
98
- }
99
-
100
- function composeValidityHandlerProps(
101
- userHandlers: Pick<
102
- UseControlOptions,
103
- 'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
104
- >,
105
- updateValidityState: FieldContextValue['updateValidityState'],
106
- ): Pick<
107
- ControlRenderProps,
108
- 'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
109
- > {
110
- return {
111
- onBlur: (event) => {
112
- userHandlers.onBlur?.(event)
113
- updateValidityState('blur', getValidityControl(event))
114
- },
115
- onChange: (event) => {
116
- userHandlers.onChange?.(event)
117
- updateValidityState('change', getValidityControl(event))
118
- },
119
- onFocus: (event) => {
120
- userHandlers.onFocus?.(event)
121
- updateValidityState('focus', getValidityControl(event))
122
- },
123
- onInput: (event) => {
124
- userHandlers.onInput?.(event)
125
- updateValidityState('input', getValidityControl(event))
126
- },
127
- onInvalid: (event) => {
128
- userHandlers.onInvalid?.(event)
129
- updateValidityState('invalid', getValidityControl(event))
130
- },
131
- }
132
- }
133
-
134
- function getValidityControl(event: { currentTarget: ControlElement }) {
135
- return event.currentTarget as ControlElement & { validity: ValidityState }
136
- }
137
-
138
- const validityStateKeys = [
139
- 'badInput',
140
- 'customError',
141
- 'patternMismatch',
142
- 'rangeOverflow',
143
- 'rangeUnderflow',
144
- 'stepMismatch',
145
- 'tooLong',
146
- 'tooShort',
147
- 'typeMismatch',
148
- 'valueMissing',
149
- ] as const satisfies readonly (keyof Omit<ValidityState, 'valid'>)[]
150
-
151
- function createValidityState(validity: Partial<ValidityState>): ValidityState {
152
- const errors = Object.fromEntries(
153
- validityStateKeys.map((key) => [key, validity[key] ?? false]),
154
- ) as Record<(typeof validityStateKeys)[number], boolean>
155
-
156
- return {
157
- ...errors,
158
- valid:
159
- validity.valid ?? validityStateKeys.every((key) => errors[key] === false),
160
- }
161
- }