Explore / Programming j'ai un probleme sur le onchange qui met a jour isFormValid, lorsque on rentre dans la… Save Share Download ▾ Reportj'ai un probleme sur le onchange qui met a jour isFormValid, lorsque on rentre dans la fonction, j'arrive à console.log la valeur du champ qui a changé mais les autres sont vides:
import { addPropertyControls, ControlType, RenderTarget, withCSS } from "framer"
import { HTMLMotionProps, motion } from "framer-motion"
import * as React from "react"
import {
containerStyles,
usePadding,
useRadius,
paddingControl,
borderRadiusControl,
fontControls,
useFontControls,
} from "https://framer.com/m/framer/default-utils.js@^0.45.0 "
import {
ComponentType,
CSSProperties,
useCallback,
useMemo,
useState,
} from "react"
interface Props extends Omit<HTMLMotionProps<"div">, "layout"> {
formId: string
withName: boolean
withEmail: boolean
withMessage: boolean
nameField: any
email: any
message: any
layout: "horizontal" | "vertical"
inputs: any
button: any
gap: number
style: CSSProperties
onSubmit?: () => void
}
const emailRegex =
/^(([^<>()[]\.,;:\s@"]+(.[^<>()[]\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/
const validateEmail = (email) => {
return emailRegex.test(String(email).toLowerCase())
}
/**
FORMSPARK
@framerIntrinsicWidth 550
@framerIntrinsicHeight 290
@framerSupportedLayoutWidth fixed
@framerSupportedLayoutHeight fixed
*/
const FormSpark: ComponentType = withCSS(
function FormSpark({
formId,
withName,
nameField: name,
withEmail,
email,
withMessage,
message,
layout,
inputs,
button,
style,
gap,
onSubmit,
...props
}) {
const [nameValue, setName] = useState(name?.value)
const [emailValue, setEmail] = useState(email?.value)
const [messageValue, setMessage] = useState(message?.value)
const [isNameError, setNameError] = useState(false)
const [isEmailError, setEmailError] = useState(false)
const [isMessageError, setMessageError] = useState(false)
const [isLoading, setLoading] = useState(false)
const [isSuccess, setSuccess] = useState(false)
const [isFormValid, setIsFormValid] = useState(false)
Show full message · 701 lines Assistant2024-09-06 10:42 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Sep 6, 2024
Added to archive Nov 16, 2026
ID 01m7h41tq19evkzcnkx3fkqnx6Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 120b5338160c9dd4642ee230291cb3ee
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
const isCanvas = useMemo(() => {
return RenderTarget.current() === RenderTarget.canvas
}, [])
const gridTemplateRows = useMemo(() => {
const rows = []
if (withName || withMessage) {
rows.push("max-content")
}
if (withMessage) {
rows.push("1fr")
}
return [...rows, "max-content"].join(" ")
}, [withName, withEmail, withMessage])
const gridTemplateColumns = useMemo(() => {
const cols = []
if (
((withName && !withEmail) || (withEmail && !withName)) &&
!withMessage &&
layout === "horizontal"
) {
return "1fr max-content"
}
return "1fr"
}, [withName, withEmail, withMessage, layout])
const { fontFamily, fontSize, fontWeight } = useFontControls(props)
const borderRadius = useRadius(props)
const paddingValue = usePadding(props)
const validateForm = useCallback(() => {
let error = false
setNameError(false)
setEmailError(false)
setMessageError(false)
console.log("email: " + emailValue)
console.log("name: " + nameValue)
console.log("message: " + messageValue)
if (withName && !nameValue) {
setNameError(true)
error = true
}
if (withEmail && (!emailValue || !validateEmail(emailValue))) {
setEmailError(true)
error = true
}
if (withMessage && !messageValue) {
setMessageError(true)
error = true
}
return error
}, [
validateEmail,
withName,
withEmail,
withMessage,
nameValue,
emailValue,
messageValue,
])
const handleSubmit = useCallback(
(event: any) => {
setLoading(true)
event.preventDefault()
if (validateForm()) {
setLoading(false)
} else {
const data = new FormData(event.target)
const entries = Object.fromEntries(data.entries())
fetch(`https://api.formspark.io/${formId}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(entries),
})
.then(() => {
setSuccess(true)
onSubmit()
})
.catch(() => setLoading(false))
}
},
[formId, onSubmit, validateForm]
)
const nameInputRef = React.useRef<HTMLInputElement | null>(null)
const emailInputRef = React.useRef<HTMLInputElement | null>(null)
const messageInputRef = React.useRef<HTMLTextAreaElement | null>(null)
const validateFormOnChange = ({
name,
email,
message,
}: {
name: string
email: string
message: string
}) => {
let error = false
setNameError(false)
setEmailError(false)
setMessageError(false)
console.log("email: " + email)
console.log("name: " + name)
console.log("message: " + message)
if (withName && !name) {
setNameError(true)
error = true
}
if (withEmail && (!email || !validateEmail(email))) {
setEmailError(true)
error = true
}
if (withMessage && !message) {
setMessageError(true)
error = true
}
return error
}
const handleNameChange = useCallback((event: any) => {
setNameError(false)
setName(event.target.value)
console.log(nameInputRef)
setIsFormValid(
!validateFormOnChange({
name: event.target.value,
email: emailInputRef.current.value,
message: messageInputRef.current.value,
})
)
}, [])
const handleEmailChange = useCallback((event: any) => {
setEmailError(false)
setEmail(event.target.value)
setIsFormValid(
!validateFormOnChange({
name: nameInputRef.current.value,
email: event.target.value,
message: messageInputRef.current.value,
})
)
}, [])
const handleMessageChange = useCallback((event: any) => {
setMessageError(false)
setMessage(event.target.value)
setIsFormValid(
!validateFormOnChange({
name: nameInputRef.current.value,
email: emailInputRef.current.value,
message: event.target.value,
})
)
}, [])
return (
<motion.div
style={{
...style,
...containerStyles,
flexDirection: "column",
"--framer-formspark-placeholder-color":
inputs.placeholderColor,
}}
>
{isSuccess ? (
<motion.div
style={{
height: "60px",
width: "60px",
background: isFormValid
? button.color
: button.fill,
borderRadius: "50%",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
initial={{ scale: 0 }}
animate={{
scale: 1,
background: isFormValid
? button.color
: button.fill,
color: isFormValid ? button.fill : button.color,
}}
transition={{
duration: 0.3,
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="28"
height="28"
>
<path
d="M 2 14 L 10 22 L 26 6"
fill="transparent"
strokeWidth="4"
stroke="currentColor"
strokeLinecap="round"
/>
</svg>
</motion.div>
) : (
<form
style={{
display: "grid",
gridTemplateRows,
gridTemplateColumns,
gap,
width: "100%",
height: "100%",
}}
onSubmit={handleSubmit}
method="POST"
>
{(withName || withEmail) && (
<div
style={{
width: "100%",
display: "grid",
gridAutoFlow:
layout === "horizontal"
? "column"
: "row",
gap,
}}
>
{withName && (
<input
ref={nameInputRef}
className="framer-formspark-input"
type="text"
name="name"
placeholder={name.placeholder}
value={
isCanvas ? name.value : nameValue
}
onChange={handleNameChange}
style={{
...defaultStyle,
padding: paddingValue,
borderRadius,
fontFamily,
fontWeight,
fontSize,
background: inputs.fill,
color: inputs.color,
boxShadow: `inset 0 0 0 1px ${
isNameError
? inputs.error
: "transparent"
}`,
}}
/>
)}
{withEmail && (
<input
ref={emailInputRef}
className="framer-formspark-input"
type="email"
name="email"
placeholder={email.placeholder}
value={
isCanvas ? email.value : emailValue
}
onChange={handleEmailChange}
style={{
...defaultStyle,
padding: paddingValue,
borderRadius,
fontFamily,
fontWeight,
fontSize,
background: inputs.fill,
color: inputs.color,
boxShadow: `inset 0 0 0 1px ${
isEmailError
? inputs.error
: "transparent"
}`,
}}
/>
)}
</div>
)}
{withMessage && (
<textarea
ref={messageInputRef}
className="framer-formspark-input"
placeholder={message.placeholder}
name="message"
value={isCanvas ? message.value : messageValue}
onChange={handleMessageChange}
style={{
...defaultStyle,
minHeight: 0,
padding: paddingValue,
resize: "vertical",
borderRadius,
background: inputs.fill,
fontFamily,
fontWeight,
fontSize,
color: inputs.color,
boxShadow: `inset 0 0 0 1px ${
isMessageError
? inputs.error
: "transparent"
}`,
}}
/>
)}
<div>
<motion.input
type="submit"
value={button.label}
style={{
...defaultStyle,
borderRadius,
padding: paddingValue,
fontFamily,
fontWeight: button.fontWeight,
fontSize,
cursor: "pointer",
zIndex: 1,
}}
animate={{
background: isFormValid
? button.color
: button.fill,
color: isFormValid
? button.fill
: button.color,
}}
transition={{ type: "ease", duration: 0.3 }}
whileHover={{ opacity: 0.8 }}
/>
{isLoading && (
<div
style={{
borderRadius,
position: "absolute",
display: "flex",
justifyContent: "center",
alignItems: "center",
width: "100%",
height: "100%",
left: 0,
top: 0,
zIndex: 2,
color: button.color,
background: button.fill,
}}
>
<motion.div
style={{ height: 16, width: 16 }}
initial={{ rotate: 0 }}
animate={{ rotate: 360 }}
transition={{
duration: 2,
repeat: Infinity,
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
>
<path
d="M 8 0 C 3.582 0 0 3.582 0 8 C 0 12.419 3.582 16 8 16 C 12.418 16 16 12.419 16 8 C 15.999 3.582 12.418 0 8 0 Z M 8 14 C 4.687 14 2 11.314 2 8 C 2 4.687 4.687 2 8 2 C 11.314 2 14 4.687 14 8 C 14 11.314 11.314 14 8 14 Z"
fill="currentColor"
opacity="0.2"
/>
<path
d="M 8 0 C 12.418 0 15.999 3.582 16 8 C 16 8 16 9 15 9 C 14 9 14 8 14 8 C 14 4.687 11.314 2 8 2 C 4.687 2 2 4.687 2 8 C 2 8 2 9 1 9 C 0 9 0 8 0 8 C 0 3.582 3.582 0 8 0 Z"
fill="currentColor"
/>
</svg>
</motion.div>
</div>
)}
</div>
</form>
)}
</motion.div>
)
},
[
".framer-formspark-input::placeholder { color: var(--framer-formspark-placeholder-color) !important; }",
]
)
FormSpark.defaultProps = {
fontSize: 16,
fontFamily: "Inter",
fontWeight: 400,
padding: 15,
paddingTop: 15,
paddingBottom: 15,
paddingLeft: 15,
paddingRight: 15,
borderRadius: 8,
topLeftRadius: 8,
topRightRadius: 8,
bottomRightRadius: 8,
bottomLeftRadius: 8,
gap: 15,
nameField: { value: undefined, placeholder: "Name" },
email: { value: undefined, placeholder: "Email" },
message: { value: undefined, placeholder: "Message" },
inputs: {
fill: "#EBEBEB",
color: "#000",
placeholderColor: "rgba(0, 0, 0, 0.5)",
error: "#EE4444",
},
layout: {
fill: "#EBEBEB",
color: "#000",
placeholderColor: "rgba(0, 0, 0, 0.5)",
error: "#EE4444",
},
button: { label: "Sign Up", fontWeight: 600, fill: "#000", color: "#FFF" },
} as any
addPropertyControls(FormSpark, {
formId: {
title: "ID",
placeholder: "7PbPpGN3",
type: ControlType.String,
description:
"Create a FormSpark account, add a new form and copy its ID. Learn more… ",
},
withName: {
title: "Name",
type: ControlType.Boolean,
enabledTitle: "Show",
disabledTitle: "Hide",
defaultValue: true,
},
nameField: {
title: " ",
type: ControlType.Object,
controls: {
placeholder: {
title: "Placeholder",
type: ControlType.String,
defaultValue: "Name",
},
value: {
title: "Value",
type: ControlType.String,
defaultValue: "",
},
},
hidden: (props) => !props.withName,
},
withEmail: {
title: "Email",
type: ControlType.Boolean,
enabledTitle: "Show",
disabledTitle: "Hide",
defaultValue: true,
},
email: {
title: " ",
type: ControlType.Object,
controls: {
placeholder: {
title: "Placeholder",
type: ControlType.String,
defaultValue: "Email",
},
value: {
title: "Value",
type: ControlType.String,
},
},
hidden: (props) => !props.withEmail,
},
withMessage: {
title: "Message",
type: ControlType.Boolean,
enabledTitle: "Show",
disabledTitle: "Hide",
defaultValue: true,
},
message: {
title: " ",
type: ControlType.Object,
controls: {
placeholder: {
title: "Placeholder",
type: ControlType.String,
defaultValue: "Message",
},
value: {
title: "Value",
type: ControlType.String,
},
},
hidden: (props) => !props.withMessage,
},
layout: {
title: "Layout",
type: ControlType.Enum,
options: ["horizontal", "vertical"],
displaySegmentedControl: true,
defaultValue: "horizontal",
},
inputs: {
title: "Inputs",
type: ControlType.Object,
controls: {
fill: {
title: "Fill",
type: ControlType.Color,
defaultValue: "#EBEBEB",
},
color: {
title: "Text",
type: ControlType.Color,
defaultValue: "#000",
},
placeholderColor: {
title: "Placeholder",
type: ControlType.Color,
defaultValue: "rgba(0, 0, 0, 0.5)",
},
error: {
title: "Error",
type: ControlType.Color,
defaultValue: "#EE4444",
},
},
},
button: {
title: "Button",
type: ControlType.Object,
controls: {
label: {
title: "Label",
type: ControlType.String,
defaultValue: "Sign Up",
},
fontWeight: {
...fontControls.fontWeight,
defaultValue: 600,
},
fill: {
title: "Fill",
type: ControlType.Color,
defaultValue: "#000",
},
color: {
title: "Text",
type: ControlType.Color,
defaultValue: "#FFF",
},
},
},
...fontControls,
fontSize: {
title: "Font Size",
type: ControlType.Number,
displayStepper: true,
defaultValue: 16,
},
...paddingControl,
...borderRadiusControl,
gap: {
title: "Gap",
type: ControlType.Number,
displayStepper: true,
min: 0,
},
onSubmit: {
type: ControlType.EventHandler,
},
})
const defaultStyle: CSSProperties = {
WebkitAppearance: "none",
display: "inline-block",
width: "100%",
lineHeight: "1.4em",
outline: "none",
border: "none",
}
The issue you're experiencing seems to be related to how the input values are being handled and updated in your form component. Based on the snippet you provided, the problem might be occurring due to the following reasons:
Refs Not Tracking Changes : It appears that you're using refs to track the values of the inputs. However, since refs don't automatically track changes like state does, you might want to ensure that the refs are always pointing to the current value of each input.
Initial State and Default Values : Make sure the initial values set in the state are correct and reflect any placeholders or initial values you intend to display.
Updating State on Change : While you have callbacks to handle changes, it's critical to ensure they update the state correctly. Since you're using useCallback, you need to ensure the dependencies array reflects any state that affects the function, such as setName, setEmail, and setMessage.
Here are some recommendations to address these problems:
1. Update State Directly for Controlled Components
Since you're controlling the inputs through state, ensure that the state is always updated correctly:
javascript Copy code
const handleNameChange = useCallback((event) => {
setName(event.target.value);
setNameError(false);
setIsFormValid(
!validateFormOnChange({
name: event.target.value,
email: emailValue,
message: messageValue,
})
);
}, [emailValue, messageValue, validateFormOnChange]);
Similarly, ensure you do this for handleEmailChange and handleMessageChange:
javascript Copy code
const handleEmailChange = useCallback((event) => {
setEmail(event.target.value);
setEmailError(false);
setIsFormValid(
!validateFormOnChange({
name: nameValue,
email: event.target.value,
message: messageValue,
})
);
}, [nameValue, messageValue, validateFormOnChange]);
const handleMessageChange = useCallback((event) => {
setMessage(event.target.value);
setMessageError(false);
setIsFormValid(
!validateFormOnChange({
name: nameValue,
email: emailValue,
message: event.target.value,
})
);
}, [nameValue, emailValue, validateFormOnChange]);
2. Fix Dependency Arrays in useCallback
Ensure that your useCallback and useMemo hooks include all necessary dependencies to react to changes properly:
javascript Copy code
const validateFormOnChange = useCallback(
({ name, email, message }) => {
// validation logic
},
[validateEmail, withName, withEmail, withMessage]
);
3. Use Ref for DOM Manipulation Only
Using refs to manage form state isn't necessary here since you're managing form state using React state. Refs should primarily be used when you need to directly manipulate the DOM or when you're dealing with third-party libraries.
By ensuring the component updates state correctly and utilizes dependencies in hooks accurately, you can ensure that all form values are synced correctly and displayed as expected.
Copy first response
Not yet included in a published release.