-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(input, input-number, input-text, text-area): ensure all applicable props are considered in form validation #8655
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
export type InputComponent = NumericInputComponent | TextualInputComponent; | ||
|
||
export interface NumericInputComponent { | ||
min: number; | ||
max: number; | ||
step: number | "any"; | ||
} | ||
|
||
export interface TextualInputComponent { | ||
pattern?: string; | ||
minLength: number; | ||
maxLength: number; | ||
} | ||
|
||
const minMaxStepTypes = ["date", "datetime-local", "month", "number", "range", "time", "week"]; | ||
const patternTypes = ["email", "password", "search", "tel", "text", "url"]; | ||
const minMaxLengthTypes = ["email", "password", "search", "tel", "text", "textarea", "url"]; | ||
|
||
function toString(num: number): string { | ||
return num?.toString() ?? ""; | ||
} | ||
|
||
/** | ||
* Synchronizes the hidden form input with the validation-related input properties. | ||
* | ||
* @param type - The input type. | ||
* @param inputComponent | ||
* @param hiddenFormInput | ||
*/ | ||
export function syncHiddenFormInput( | ||
type: HTMLInputElement["type"] | "textarea", | ||
inputComponent: InputComponent, | ||
hiddenFormInput: HTMLInputElement, | ||
): void { | ||
hiddenFormInput.type = type === "textarea" ? "text" : type; | ||
|
||
if (minMaxStepTypes.includes(type)) { | ||
const numericInputComponent = inputComponent as NumericInputComponent; | ||
hiddenFormInput.min = toString(numericInputComponent.min); | ||
hiddenFormInput.max = toString(numericInputComponent.max); | ||
jcfranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const step = numericInputComponent.step; | ||
hiddenFormInput.step = step === "any" ? step : toString(step); | ||
} | ||
|
||
if (minMaxLengthTypes.includes(type)) { | ||
const textualInputComponent = inputComponent as TextualInputComponent; | ||
|
||
if (hiddenFormInput.minLength != null) { | ||
hiddenFormInput.minLength = textualInputComponent.minLength; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was originally setting it via the props (vs attributes), but switched the approach since there are some props that can't be reset via the prop. 🥲 |
||
} | ||
|
||
if (hiddenFormInput.maxLength != null) { | ||
hiddenFormInput.maxLength = textualInputComponent.maxLength; | ||
} | ||
} | ||
|
||
if (patternTypes.includes(type)) { | ||
const textualInputComponent = inputComponent as TextualInputComponent; | ||
hiddenFormInput.pattern = textualInputComponent.pattern || ""; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably deprecate
minLength
andmaxLength
oncalcite-input-number
. That's on me - I didn't check the constraint validation doc when I ported the input code over to the new component.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Good catch. 🎣