-
Notifications
You must be signed in to change notification settings - Fork 79
fix(input, input-number, input-text, text-area): ensure all applicable props are considered in form validation #8655
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
Changes from all commits
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,38 @@ | ||
/* eslint-disable jest/no-conditional-expect -- Using conditional logic in a confined test helper to handle specific scenarios, reducing duplication, balancing test readability and maintainability. **/ | ||
import { minMaxLengthTypes, minMaxStepTypes, patternTypes, syncHiddenFormInput } from "./input"; | ||
|
||
describe("common input utils", () => { | ||
it("syncHiddenFormInput", async () => { | ||
const minMaxLengthTestValues = { minLength: 0, maxLength: 10 }; | ||
const patternTestValue = { pattern: "test" }; | ||
const minMaxStepTestValues = { min: 0, max: 10, step: 1 }; | ||
|
||
const allTypes = Array.from(new Set([...minMaxLengthTypes, ...patternTypes, ...minMaxStepTypes])); | ||
const allValueFakeInputComponent = { ...minMaxLengthTestValues, ...minMaxStepTestValues, ...patternTestValue }; | ||
|
||
const hiddenFormInput = document.createElement("input"); | ||
|
||
allTypes.forEach((type) => { | ||
syncHiddenFormInput(type, allValueFakeInputComponent, hiddenFormInput); | ||
|
||
const expectedType = type === "textarea" ? "text" : type; | ||
|
||
expect(hiddenFormInput.type).toBe(expectedType); | ||
|
||
if (minMaxStepTypes.includes(type)) { | ||
expect(hiddenFormInput.min).toBe("0"); | ||
expect(hiddenFormInput.max).toBe("10"); | ||
expect(hiddenFormInput.step).toBe("1"); | ||
} | ||
|
||
if (minMaxLengthTypes.includes(type)) { | ||
expect(hiddenFormInput.minLength).toBe(0); | ||
expect(hiddenFormInput.maxLength).toBe(10); | ||
} | ||
|
||
if (patternTypes.includes(type)) { | ||
expect(hiddenFormInput.pattern).toBe("test"); | ||
} | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export type InputComponent = NumericInputComponent | TextualInputComponent; | ||
|
||
export interface NumericInputComponent { | ||
min: number; | ||
max: number; | ||
step: number | "any"; | ||
} | ||
|
||
export interface TextualInputComponent { | ||
pattern?: string; | ||
minLength: number; | ||
maxLength: number; | ||
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. We should probably deprecate 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. Agreed. Good catch. 🎣 |
||
} | ||
|
||
/** | ||
* Exported for testing purposes only | ||
*/ | ||
export const minMaxStepTypes = ["date", "datetime-local", "month", "number", "range", "time", "week"]; | ||
|
||
/** | ||
* Exported for testing purposes only | ||
*/ | ||
export const patternTypes = ["email", "password", "search", "tel", "text", "url"]; | ||
|
||
/** | ||
* Exported for testing purposes only | ||
*/ | ||
export const minMaxLengthTypes = ["email", "password", "search", "tel", "text", "textarea", "url"]; | ||
|
||
function updateConstraintValidation( | ||
inputComponent: InputComponent, | ||
input: HTMLInputElement, | ||
propName: string, | ||
matchesType: boolean, | ||
): void { | ||
const attributeName = propName.toLowerCase(); | ||
const value = inputComponent[propName]; | ||
|
||
if (matchesType && value != null) { | ||
input.setAttribute(attributeName, `${value}`); | ||
} else { | ||
// we remove the attribute to ensure validation-constraints are properly reset | ||
input.removeAttribute(attributeName); | ||
} | ||
} | ||
|
||
/** | ||
* Synchronizes the hidden form input with the validation-related input properties. | ||
* | ||
* Note: loss of precision is expected due to the hidden input's value and validation-constraint props being strings. | ||
* | ||
* @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; | ||
|
||
const isMinMaxStepType = minMaxStepTypes.includes(type); | ||
const numericInputComponent = inputComponent as NumericInputComponent; | ||
|
||
updateConstraintValidation(numericInputComponent, hiddenFormInput, "min", isMinMaxStepType); | ||
updateConstraintValidation(numericInputComponent, hiddenFormInput, "max", isMinMaxStepType); | ||
updateConstraintValidation(numericInputComponent, hiddenFormInput, "step", isMinMaxStepType); | ||
|
||
const isMinMaxLengthType = minMaxLengthTypes.includes(type); | ||
|
||
const textualInputComponent = inputComponent as TextualInputComponent; | ||
|
||
updateConstraintValidation(textualInputComponent, hiddenFormInput, "minLength", isMinMaxLengthType); | ||
updateConstraintValidation(textualInputComponent, hiddenFormInput, "maxLength", isMinMaxLengthType); | ||
|
||
const isPatternType = patternTypes.includes(type); | ||
|
||
updateConstraintValidation(textualInputComponent, hiddenFormInput, "pattern", isPatternType); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.