playbook_ui 14.9.0.pre.alpha.PLAY1731inputmasking4927 → 14.9.0.pre.alpha.PLAY16264952

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
- import React, { useState } from 'react'
2
- import { render, screen, fireEvent, within } from '../utilities/test-utils'
1
+ import React from 'react'
2
+ import { render, screen } from '../utilities/test-utils'
3
3
 
4
4
  import TextInput from './_text_input'
5
5
 
@@ -89,140 +89,3 @@ test('returns additional class name', () => {
89
89
  const kit = screen.getByTestId(testId)
90
90
  expect(kit).toHaveClass(`${kitClass} mb_lg`)
91
91
  })
92
-
93
-
94
- const TextInputCurrencyMask = (props) => {
95
- const [currency, setValue] = useState('')
96
- const handleOnChange = ({ target }) => {
97
- setValue(target.value)
98
- }
99
-
100
- return (
101
- <TextInput
102
- mask="currency"
103
- onChange={handleOnChange}
104
- value={currency}
105
- {...props}
106
- />
107
- )
108
- }
109
-
110
- test('returns masked currency value', () => {
111
- render(
112
- <TextInputCurrencyMask
113
- data={{ testid: testId }}
114
- />
115
- )
116
-
117
- const kit = screen.getByTestId(testId)
118
-
119
- const input = within(kit).getByRole('textbox');
120
-
121
- fireEvent.change(input, { target: { value: '123456' } });
122
-
123
- expect(input.value).toBe('$1,234.56')
124
-
125
- fireEvent.change(input, { target: { value: '1' } });
126
-
127
- expect(input.value).toBe('$0.01')
128
-
129
- fireEvent.change(input, { target: { value: '' } });
130
-
131
- expect(input.value).toBe('')
132
- })
133
-
134
- const TextInputZipCodeMask = (props) => {
135
- const [zipCode, setValue] = useState('')
136
- const handleOnChange = ({ target }) => {
137
- setValue(target.value)
138
- }
139
-
140
- return (
141
- <TextInput
142
- mask="zipCode"
143
- onChange={handleOnChange}
144
- value={zipCode}
145
- {...props}
146
- />
147
- )
148
- }
149
-
150
- test('returns masked zip code value', () => {
151
- render(
152
- <TextInputZipCodeMask
153
- data={{ testid: testId }}
154
- />
155
- )
156
-
157
- const kit = screen.getByTestId(testId)
158
-
159
- const input = within(kit).getByRole('textbox');
160
-
161
- fireEvent.change(input, { target: { value: '123456' } });
162
-
163
- expect(input.value).toBe('12345')
164
- })
165
-
166
- const TextInputPostalCodeMask = (props) => {
167
- const [postalCode, setValue] = useState('')
168
- const handleOnChange = ({ target }) => {
169
- setValue(target.value)
170
- }
171
-
172
- return (
173
- <TextInput
174
- mask="postalCode"
175
- onChange={handleOnChange}
176
- value={postalCode}
177
- {...props}
178
- />
179
- )
180
- }
181
-
182
- test('returns masked postal code value', () => {
183
- render(
184
- <TextInputPostalCodeMask
185
- data={{ testid: testId }}
186
- />
187
- )
188
-
189
- const kit = screen.getByTestId(testId)
190
-
191
- const input = within(kit).getByRole('textbox');
192
-
193
- fireEvent.change(input, { target: { value: '123456789' } });
194
-
195
- expect(input.value).toBe('12345-6789')
196
- })
197
-
198
- const TextInputSSNMask = (props) => {
199
- const [ssn, setValue] = useState('')
200
- const handleOnChange = ({ target }) => {
201
- setValue(target.value)
202
- }
203
-
204
- return (
205
- <TextInput
206
- mask="ssn"
207
- onChange={handleOnChange}
208
- value={ssn}
209
- {...props}
210
- />
211
- )
212
- }
213
-
214
- test('returns masked ssn value', () => {
215
- render(
216
- <TextInputSSNMask
217
- data={{ testid: testId }}
218
- />
219
- )
220
-
221
- const kit = screen.getByTestId(testId)
222
-
223
- const input = within(kit).getByRole('textbox');
224
-
225
- fireEvent.change(input, { target: { value: '123456789' } });
226
-
227
- expect(input.value).toBe('123-45-6789')
228
- })
@@ -0,0 +1,22 @@
1
+ import { UseFormRegister, FieldValues, RegisterOptions, Path } from 'react-hook-form'
2
+
3
+ export type HookFormProps<T extends FieldValues = FieldValues> = {
4
+ register?: UseFormRegister<T>
5
+ rules?: RegisterOptions
6
+ name: Path<T>
7
+ }
8
+
9
+ export const withHookForm = <T extends FieldValues = FieldValues>(
10
+ props: HookFormProps<T>
11
+ ) => {
12
+ const { register, name, rules } = props
13
+ if (!register) return {}
14
+
15
+ const registration = register(name, rules)
16
+ return {
17
+ onChange: registration.onChange,
18
+ onBlur: registration.onBlur,
19
+ ref: registration.ref,
20
+ name: registration.name,
21
+ }
22
+ }
@@ -0,0 +1,64 @@
1
+ import React from "react"
2
+ import {
3
+ FieldValues,
4
+ Path,
5
+ UseFormRegister,
6
+ RegisterOptions,
7
+ } from "react-hook-form"
8
+
9
+ export type WithReactHookFormProps<T extends FieldValues> = {
10
+ name: Path<T>
11
+ register?: UseFormRegister<T>
12
+ rules?: Omit<
13
+ RegisterOptions<T>,
14
+ "valueAsNumber" | "valueAsDate" | "setValueAs"
15
+ >
16
+ disabled?: boolean
17
+ }
18
+
19
+ function getDisplayName(WrappedComponent: React.ComponentType<any>): string {
20
+ return WrappedComponent.displayName || WrappedComponent.name || 'Component'
21
+ }
22
+
23
+ export function withReactHookForm<
24
+ P extends {
25
+ onChange?: (...args: any[]) => void
26
+ onBlur?: (...args: any[]) => void
27
+ ref?: any
28
+ },
29
+ T extends FieldValues = FieldValues
30
+ >(WrappedComponent: React.ComponentType<P>) {
31
+ const WithReactHookFormComponent = React.forwardRef<HTMLElement, P & WithReactHookFormProps<T>>(
32
+ (props, ref) => {
33
+ const { register, name, rules, disabled, ...rest } = props
34
+
35
+ if (!register) {
36
+ return (<WrappedComponent {...(rest as P)}
37
+ ref={ref}
38
+ />)
39
+ }
40
+
41
+ const fieldRegistration = register(name, rules)
42
+
43
+ const fieldProps = {
44
+ ...rest,
45
+ ...fieldRegistration,
46
+ disabled: disabled || (rest as any).disabled,
47
+ ref: ref || fieldRegistration.ref,
48
+ onChange: (...args: any[]) => {
49
+ fieldRegistration.onChange?.(...args)
50
+ ;(rest as P).onChange?.(...args)
51
+ },
52
+ onBlur: (...args: any[]) => {
53
+ fieldRegistration.onBlur?.(...args)
54
+ ;(rest as P).onBlur?.(...args)
55
+ },
56
+ }
57
+
58
+ return <WrappedComponent {...(fieldProps as P)} />
59
+ }
60
+ )
61
+
62
+ WithReactHookFormComponent.displayName = `WithReactHookForm(${getDisplayName(WrappedComponent)})`
63
+ return WithReactHookFormComponent
64
+ }