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

Add default placeholder for Selection element #30

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
@@ -1,4 +1,4 @@
import { Number, extractParams } from "@optimizely/forms-sdk"
import { Number } from "@optimizely/forms-sdk"
import React from "react";
import ElementWrapper from "../ElementWrapper";
import { useElement } from "../../hooks/useElement";
Expand All @@ -11,11 +11,10 @@ export interface NumberElementBlockProps {
export const NumberElementBlock = (props: NumberElementBlockProps) => {
const { element } = props;
const { elementContext, extraAttr, validatorClasses, handleChange, handleBlur, checkVisible } = useElement(element);
const { language } = extractParams(window.location.pathname)

return (
<ElementWrapper className={`FormTextbox FormTextbox--Number ${validatorClasses ?? ""}`} isVisible={checkVisible()}>
<div lang={language}>
<div lang={element.locale}>
<ElementCaption element={element} />

<input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Selection } from "@optimizely/forms-sdk"
import { Selection, isNullOrEmpty } from "@optimizely/forms-sdk"
import React from "react";
import ElementWrapper from "../ElementWrapper";
import { useElement } from "../../hooks/useElement";
Expand All @@ -11,6 +11,7 @@ export interface SelectionElementBlockProps {
export const SelectionElementBlock = (props: SelectionElementBlockProps) => {
const { element } = props;
const { elementContext, extraAttr, validatorClasses, handleChange, handleBlur, checkVisible } = useElement(element);
const defaultOptionSelected = !element.properties.allowMultiSelect && !element.properties.items.some(i => i.checked);
return (
<ElementWrapper className={`FormSelection ${validatorClasses}`} isVisible={checkVisible()}>
<ElementCaption element={element}></ElementCaption>
Expand All @@ -24,8 +25,11 @@ export const SelectionElementBlock = (props: SelectionElementBlockProps) => {
onChange={handleChange}
onBlur={handleBlur}
>
<option disabled value={""}>
{element.properties.placeHolder}
<option disabled selected={defaultOptionSelected} value="">
{isNullOrEmpty(element.properties.placeHolder)
? element.localizations["selectionDefaultPlaceholder"]
: element.properties.placeHolder
}
</option>
{element.properties.items.map(feed => (
<option key={feed.value} value={feed.value} defaultChecked={feed.checked}>{feed.caption}</option>
Expand Down
20 changes: 15 additions & 5 deletions src/@optimizely/forms-react/src/hooks/useElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import {
ValidatorType,
SatisfiedActionType,
FormValidationResult,
FormSubmission,
FormValidation,
FormDependencies,
//functions
equals,
getDefaultValue,
isNull,
isNullOrEmpty,
isNullOrEmpty,
isInArray,
//class
FormValidator
} from "@optimizely/forms-sdk";
Expand Down Expand Up @@ -133,7 +131,19 @@ export const useElement = (element: FormElementBase) => {
} as ElementContext);

let isValidationFail = !isNull(validationResults) && validationResults.some(r => !r.valid);
validatorClasses.current = `${isValidationFail ? "ValidationFail" : ""} ${validatorClasses.current}`
let arrClass = validatorClasses.current.split(" ");
let failClass = "ValidationFail";
if(isValidationFail){
if(!isInArray(failClass, arrClass)){
arrClass.push(failClass);
}
}
else {
if(isInArray(failClass, arrClass)){
arrClass = arrClass.filter(c => c !== failClass);
}
}
validatorClasses.current = arrClass.join(" ");
}

const checkVisible = (): boolean => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export interface FormElementBase {
contentType: string
displayName: string
properties: FormElementPropertiesBase
localizations: Record<string, string>
locale: string
}

export interface FormElementPropertiesBase {
description: string
label: string
localizations: Record<string, string>
}