|
|
import type { HTMLProps } from 'react' |
|
|
|
|
|
export const DISALLOWED_FORM_PROPS = ['method', 'encType', 'target'] as const |
|
|
|
|
|
type HTMLFormProps = HTMLProps<HTMLFormElement> |
|
|
type DisallowedFormProps = (typeof DISALLOWED_FORM_PROPS)[number] |
|
|
|
|
|
type InternalFormProps = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
action: NonNullable<HTMLFormProps['action']> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prefetch?: false | null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
replace?: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scroll?: boolean |
|
|
} & Omit<HTMLFormProps, 'action' | DisallowedFormProps> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type FormProps<RouteInferType = any> = InternalFormProps |
|
|
|
|
|
export function createFormSubmitDestinationUrl( |
|
|
action: string, |
|
|
formElement: HTMLFormElement |
|
|
) { |
|
|
let targetUrl: URL |
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const base = window.location.href |
|
|
targetUrl = new URL(action, base) |
|
|
} catch (err) { |
|
|
throw new Error(`Cannot parse form action "${action}" as a URL`, { |
|
|
cause: err, |
|
|
}) |
|
|
} |
|
|
if (targetUrl.searchParams.size) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
targetUrl.search = '' |
|
|
} |
|
|
|
|
|
const formData = new FormData(formElement) |
|
|
|
|
|
for (let [name, value] of formData) { |
|
|
if (typeof value !== 'string') { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') { |
|
|
console.warn( |
|
|
`<Form> only supports file inputs if \`action\` is a function. File inputs cannot be used if \`action\` is a string, ` + |
|
|
`because files cannot be encoded as search params.` |
|
|
) |
|
|
} |
|
|
value = value.name |
|
|
} |
|
|
|
|
|
targetUrl.searchParams.append(name, value) |
|
|
} |
|
|
return targetUrl |
|
|
} |
|
|
|
|
|
export function checkFormActionUrl( |
|
|
action: string, |
|
|
source: 'action' | 'formAction' |
|
|
) { |
|
|
const aPropName = source === 'action' ? `an \`action\`` : `a \`formAction\`` |
|
|
|
|
|
let testUrl: URL |
|
|
try { |
|
|
testUrl = new URL(action, 'http://n') |
|
|
} catch (err) { |
|
|
console.error( |
|
|
`<Form> received ${aPropName} that cannot be parsed as a URL: "${action}".` |
|
|
) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
if (testUrl.searchParams.size) { |
|
|
console.warn( |
|
|
`<Form> received ${aPropName} that contains search params: "${action}". This is not supported, and they will be ignored. ` + |
|
|
`If you need to pass in additional search params, use an \`<input type="hidden" />\` instead.` |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
export const isSupportedFormEncType = (value: string) => |
|
|
value === 'application/x-www-form-urlencoded' |
|
|
export const isSupportedFormMethod = (value: string) => value === 'get' |
|
|
export const isSupportedFormTarget = (value: string) => value === '_self' |
|
|
|
|
|
export function hasUnsupportedSubmitterAttributes( |
|
|
submitter: HTMLElement |
|
|
): boolean { |
|
|
|
|
|
const formEncType = submitter.getAttribute('formEncType') |
|
|
if (formEncType !== null && !isSupportedFormEncType(formEncType)) { |
|
|
if (process.env.NODE_ENV === 'development') { |
|
|
console.error( |
|
|
`<Form>'s \`encType\` was set to an unsupported value via \`formEncType="${formEncType}"\`. ` + |
|
|
`This will disable <Form>'s navigation functionality. If you need this, use a native <form> element instead.` |
|
|
) |
|
|
} |
|
|
return true |
|
|
} |
|
|
|
|
|
|
|
|
const formMethod = submitter.getAttribute('formMethod') |
|
|
if (formMethod !== null && !isSupportedFormMethod(formMethod)) { |
|
|
if (process.env.NODE_ENV === 'development') { |
|
|
console.error( |
|
|
`<Form>'s \`method\` was set to an unsupported value via \`formMethod="${formMethod}"\`. ` + |
|
|
`This will disable <Form>'s navigation functionality. If you need this, use a native <form> element instead.` |
|
|
) |
|
|
} |
|
|
return true |
|
|
} |
|
|
|
|
|
|
|
|
const formTarget = submitter.getAttribute('formTarget') |
|
|
if (formTarget !== null && !isSupportedFormTarget(formTarget)) { |
|
|
if (process.env.NODE_ENV === 'development') { |
|
|
console.error( |
|
|
`<Form>'s \`target\` was set to an unsupported value via \`formTarget="${formTarget}"\`. ` + |
|
|
`This will disable <Form>'s navigation functionality. If you need this, use a native <form> element instead.` |
|
|
) |
|
|
} |
|
|
return true |
|
|
} |
|
|
|
|
|
return false |
|
|
} |
|
|
|
|
|
export function hasReactClientActionAttributes(submitter: HTMLElement) { |
|
|
|
|
|
|
|
|
const action = submitter.getAttribute('formAction') |
|
|
return action && /\s*javascript:/i.test(action) |
|
|
} |
|
|
|