Skip to content

Commit 83b4d17

Browse files
committed
Proof of concept branch to investigate fixes for jsx=true warning and errors.
The main issue is that in some components there is some styling declared as <style jsx>, but when running the tests for these components we get the warning “Warning: Received `true` for a non-boolean attribute `jsx`”. If we change to <style jsx = “true”>, the warning is fixed but then we get a compile error: Type error: Type 'string' is not assignable to type 'boolean | undefined'. I do not understand completely why not setting this variable is giving the warning, but some explanation and suggestions for fixes are provided in the following links: vitejs/vite#6136 vercel/styled-jsx#838 https://github.com/vercel/styled-jsx#rendering-in-tests I tried all of them, but they did not fix the warning. However, in the end, by just removing the jsx tag, it seems to work ( the app compiles and not warnings).
1 parent 117f8f4 commit 83b4d17

File tree

6 files changed

+140
-12
lines changed

6 files changed

+140
-12
lines changed

src/core/components/fields/DateField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const DateField = forwardRef<HTMLInputElement, DateFieldProps>(
6161
{normalizedError}
6262
</div>
6363
)}
64-
<style jsx>{`
64+
<style>{`
6565
label {
6666
display: flex;
6767
flex-direction: column;

src/core/components/fields/LabelSelectField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const LabelSelectField = forwardRef<HTMLSelectElement, LabeledSelectField
7373
</div>
7474
)}
7575

76-
<style jsx>{`
76+
<style>{`
7777
label {
7878
display: flex;
7979
flex-direction: column;

src/core/components/fields/LabeledPasswordField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const LabeledPasswordField = forwardRef<HTMLInputElement, LabeledPassWord
6767
</div>
6868
)}
6969

70-
<style jsx>{`
70+
<style>{`
7171
label {
7272
display: flex;
7373
flex-direction: column;

src/core/components/fields/LabeledTextAreaField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const LabeledTextAreaField = forwardRef<HTMLTextAreaElement, LabeledTextA
4343
</div>
4444
)}
4545

46-
<style jsx>{`
46+
<style>{`
4747
label {
4848
display: flex;
4949
flex-direction: column;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
5+
import { expect, test } from "vitest"
6+
import { fireEvent, render, screen } from "test/utils"
7+
import { Form, FormProps } from "src/core/components/fields/Form"
8+
import { z } from "zod"
9+
import LabeledTextField from "./LabeledTextField"
10+
11+
interface MyFormProps<S extends z.ZodType<any, any>> extends FormProps<S> {
12+
name: string
13+
placeholder: string
14+
label: string
15+
inputType?: "text" | "password" | "email" | "number" | "textarea"
16+
onChangeCallback: (val) => void
17+
}
18+
19+
const placeholder = "Container placeholder"
20+
const name = "input 1"
21+
const label = "input_label"
22+
23+
function MyForm<S extends z.ZodType<any, any>>(props: MyFormProps<S>) {
24+
const { name, placeholder, inputType, onChangeCallback, label, ...formProps } = props
25+
26+
return (
27+
<div>
28+
<Form {...formProps}>
29+
<LabeledTextField
30+
name={name}
31+
label={label}
32+
placeholder={placeholder}
33+
type={inputType}
34+
className="input mb-4 w-1/2 text-primary input-primary input-bordered border-2 bg-base-300"
35+
onChange={onChangeCallback}
36+
/>
37+
</Form>
38+
</div>
39+
)
40+
}
41+
42+
test("renders labeled field for text type", async () => {
43+
const inputType = "text"
44+
let value = ""
45+
const onChangeHandle = (val) => {
46+
value = val.currentTarget.value
47+
}
48+
49+
render(
50+
<MyForm
51+
name={name}
52+
placeholder={placeholder}
53+
onChangeCallback={onChangeHandle}
54+
onSubmit={(val) => {}}
55+
inputType={inputType}
56+
label={label}
57+
></MyForm>
58+
)
59+
60+
const elView = screen.getByTestId("labeledinput-testid")
61+
const input = screen.getByPlaceholderText(placeholder)
62+
expect(elView).toBeInTheDocument()
63+
expect(input).toBeInTheDocument()
64+
expect(screen.getByText(/input_label/i)).toBeInTheDocument()
65+
expect(input).not.toHaveAttribute("value", "test value")
66+
fireEvent.change(input, { target: { value: "test value" } })
67+
expect(value).equals("test value")
68+
expect(input).toHaveAttribute("type", inputType)
69+
expect(input).toHaveAttribute("name", name)
70+
expect(input).not.toHaveAttribute("type", "password")
71+
})
72+
73+
test("renders labeled field for numeric type", async () => {
74+
const inputType = "number"
75+
let value = ""
76+
const onChangeHandle = (val) => {
77+
value = val.currentTarget.value
78+
}
79+
80+
render(
81+
<MyForm
82+
name={name}
83+
placeholder={placeholder}
84+
onChangeCallback={onChangeHandle}
85+
onSubmit={(val) => {}}
86+
label={label}
87+
inputType={inputType}
88+
></MyForm>
89+
)
90+
const elView = screen.getByTestId("labeledinput-testid")
91+
const input = screen.getByPlaceholderText(placeholder)
92+
expect(elView).toBeInTheDocument()
93+
expect(input).toBeInTheDocument()
94+
expect(input).not.toHaveAttribute("value", "1234567")
95+
fireEvent.change(input, { target: { value: "1234567a" } })
96+
expect(value).not.equal("1234567")
97+
fireEvent.change(input, { target: { value: "1234567" } })
98+
expect(value).equal("1234567")
99+
expect(input).toHaveAttribute("type", inputType)
100+
expect(input).not.toHaveAttribute("type", "text")
101+
})
102+
103+
test("renders labeled field for password type", async () => {
104+
const inputType = "password"
105+
const testPassword = "pass"
106+
let value = ""
107+
const onChangeHandle = (val) => {
108+
value = val.currentTarget.value
109+
}
110+
111+
render(
112+
<MyForm
113+
name={name}
114+
placeholder={placeholder}
115+
onChangeCallback={onChangeHandle}
116+
onSubmit={(val) => {}}
117+
label={label}
118+
inputType={inputType}
119+
></MyForm>
120+
)
121+
122+
const elView = screen.getByTestId("labeledinput-testid")
123+
const input = screen.getByPlaceholderText(placeholder)
124+
expect(elView).toBeInTheDocument()
125+
expect(input).toBeInTheDocument()
126+
expect(input).not.toHaveAttribute("value", testPassword)
127+
fireEvent.change(input, { target: { value: testPassword } })
128+
expect(value).equal(testPassword)
129+
expect(input).toHaveAttribute("type", inputType)
130+
expect(input).not.toHaveAttribute("type", "text")
131+
})

src/core/components/fields/LabeledTextField.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@ export interface LabeledTextFieldProps extends PropsWithoutRef<JSX.IntrinsicElem
1515

1616
export const LabeledTextField = forwardRef<HTMLInputElement, LabeledTextFieldProps>(
1717
({ name, label, outerProps, fieldProps, labelProps, ...props }, ref) => {
18+
let validValue = (v) => (v === "" ? null : v)
19+
let myType = props.type === "number" ? (Number as any) : validValue
1820
const {
1921
input,
2022
meta: { touched, error, submitError, submitting },
2123
} = useField(name, {
22-
parse:
23-
props.type === "number"
24-
? (Number as any)
25-
: // Converting `""` to `null` ensures empty values will be set to null in the DB
26-
(v) => (v === "" ? null : v),
24+
parse: myType,
2725
...fieldProps,
2826
})
29-
3027
const normalizedError = Array.isArray(error) ? error.join(", ") : error || submitError
3128

3229
return (
33-
<div {...outerProps}>
30+
<div {...outerProps} data-testid="labeledinput-testid">
3431
<label {...labelProps}>
3532
{label}
3633
<input {...input} disabled={submitting} {...props} ref={ref} />
@@ -42,7 +39,7 @@ export const LabeledTextField = forwardRef<HTMLInputElement, LabeledTextFieldPro
4239
</div>
4340
)}
4441

45-
<style jsx>{`
42+
<style>{`
4643
label {
4744
display: flex;
4845
flex-direction: column;

0 commit comments

Comments
 (0)