Skip to content
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 FileUpload element cannot validate #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ export interface FileUploadElementBlockProps {

export const FileUploadElementBlock = (props: FileUploadElementBlockProps) => {
const { element } = props;
const { elementContext, extraAttr, validatorClasses, handleChange, handleBlur, checkVisible } = useElement(element);
let allowedTypes = ""
element.properties.fileTypes && element.properties.fileTypes.split(",").forEach(ext => {
if (ext[0] != ".")
allowedTypes += "."
allowedTypes += ext + ","
});
const { elementContext, extraAttr, validatorClasses, handleChange, checkVisible } = useElement(element);
let allowedTypes = element.properties.fileTypes ? element.properties.fileTypes.split(",").map((ext) => {
ext = ext.trim();
return (ext[0] != ".") ? `.${ext}` : ext;
}).join(",") : "";

return (
<ElementWrapper className={`FormFileUpload ${validatorClasses}`} isVisible={checkVisible()}>
<ElementCaption element={element} />
Expand All @@ -29,9 +28,8 @@ export const FileUploadElementBlock = (props: FileUploadElementBlockProps) => {
multiple={element.properties.allowMultiple}
className="FormFileUpload__Input"
accept={allowedTypes}
aria-describedby={element.displayName + "_desc"}
aria-describedby={element.key + "_desc"}
onChange={handleChange}
onBlur={handleBlur}
/>
<div className="FormFileUpload__PostedFile"></div>

Expand Down
52 changes: 35 additions & 17 deletions src/@optimizely/forms-react/src/hooks/useElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,26 @@ export const useElement = (element: FormElementBase) => {
isDependenciesSatisfied
} as ElementContext);
},[element.key]);

const dispatchUpdateValidation = (validationResults: FormValidationResult[]) => {
dispatch({
type: ActionType.UpdateValidation,
elementKey: element.key,
validationResults
});
}
const dispatchUpdateValue = (value: any) => {
dispatch({
type: ActionType.UpdateValue,
elementKey: element.key,
value
});
}

const handleChange = (e: any) => {
const {name, value, type, checked} = e.target;
const {name, value, type, checked, files} = e.target;
let submissionValue = value;
let validationResults = [...elementContext.validationResults];

//get selected value for choice
if(/checkbox|radio/.test(type)){
Expand All @@ -92,43 +108,43 @@ export const useElement = (element: FormElementBase) => {

submissionValue = arrayValue.length > 0 ? arrayValue.join(",") : null;
}

if(/file/.test(type)){
submissionValue = files;
validationResults = doValidate(name, files);
dispatchUpdateValidation(validationResults);
}

//update form context
dispatch({
type: ActionType.UpdateValue,
elementKey: name,
value: submissionValue
});
dispatchUpdateValue(submissionValue);

//update element state
setElementContext({
...elementContext,
value: submissionValue
value: submissionValue,
validationResults
} as ElementContext);
}

const handleBlur = (e: any) => {
const {name} = e.target;
//call validation from form-sdk
doValidate(name);
let validationResults = doValidate(name, elementContext.value);

//call dependencies from form-sdk
}

const doValidate = (elementKey: string) => {
let validationResults = formValidation.validate(elementContext.value);

//update form context
dispatch({
type: ActionType.UpdateValidation,
elementKey,
validationResults
});
dispatchUpdateValidation(validationResults);

//update element state
setElementContext({
...elementContext,
validationResults
} as ElementContext);
}

const doValidate = (elementKey: string, value: any) => {
let validationResults = formValidation.validate(value);

let isValidationFail = !isNull(validationResults) && validationResults.some(r => !r.valid);
let arrClass = validatorClasses.current.split(" ");
Expand All @@ -144,6 +160,8 @@ export const useElement = (element: FormElementBase) => {
}
}
validatorClasses.current = arrClass.join(" ");

return validationResults;
}

const checkVisible = (): boolean => {
Expand Down