offers an empty values as a choice on single select survey questions that are not required.

Also changes the single select component to a <Select variant='single'> component
following patternfly design guidelines
This commit is contained in:
Alex Corey
2021-06-16 15:00:54 -04:00
committed by Shane McDonald
parent 5a7a1b8f20
commit 560b4ebf71
3 changed files with 36 additions and 16 deletions

View File

@@ -10,7 +10,6 @@ import {
SelectVariant, SelectVariant,
} from '@patternfly/react-core'; } from '@patternfly/react-core';
import FormField from '../../FormField'; import FormField from '../../FormField';
import AnsibleSelect from '../../AnsibleSelect';
import Popover from '../../Popover'; import Popover from '../../Popover';
import { import {
required, required,
@@ -92,12 +91,16 @@ function NumberField({ question }) {
} }
function MultipleChoiceField({ question }) { function MultipleChoiceField({ question }) {
const [field, meta] = useField({ const [field, meta, helpers] = useField({
name: `survey_${question.variable}`, name: `survey_${question.variable}`,
validate: question.required ? required(null) : null, validate: question.required ? required(null) : null,
}); });
const [isOpen, setIsOpen] = useState(false);
const id = `survey-question-${question.variable}`; const id = `survey-question-${question.variable}`;
const isValid = !(meta.touched && meta.error); const isValid = !(meta.touched && meta.error);
const options = question.choices.split('\n');
return ( return (
<FormGroup <FormGroup
fieldId={id} fieldId={id}
@@ -107,16 +110,27 @@ function MultipleChoiceField({ question }) {
label={question.question_name} label={question.question_name}
labelIcon={<Popover content={question.question_description} />} labelIcon={<Popover content={question.question_description} />}
> >
<AnsibleSelect <Select
onToggle={setIsOpen}
onSelect={(event, option) => {
helpers.setValue(option);
setIsOpen(false);
}}
selections={field.value}
variant={SelectVariant.single}
id={id} id={id}
isValid={isValid} isValid={isValid}
{...field} isOpen={isOpen}
data={question.choices.split('\n').map(opt => ({ placeholderText={t`Select an option`}
key: opt, onClear={() => {
value: opt, helpers.setTouched(true);
label: opt, helpers.setValue('');
}))} }}
/> >
{options.map(opt => (
<SelectOption key={opt} value={opt} />
))}
</Select>
</FormGroup> </FormGroup>
); );
} }
@@ -145,6 +159,7 @@ function MultiSelectField({ question }) {
<Select <Select
variant={SelectVariant.typeaheadMulti} variant={SelectVariant.typeaheadMulti}
id={id} id={id}
placeholderText={!field.value.length && t`Select option(s)`}
onToggle={setIsOpen} onToggle={setIsOpen}
onSelect={(event, option) => { onSelect={(event, option) => {
if (field?.value?.includes(option)) { if (field?.value?.includes(option)) {

View File

@@ -71,9 +71,6 @@ function getInitialValues(launchConfig, surveyConfig, resource) {
values[`survey_${question.variable}`] = question.default values[`survey_${question.variable}`] = question.default
? question.default.split('\n') ? question.default.split('\n')
: []; : [];
} else if (question.type === 'multiplechoice') {
values[`survey_${question.variable}`] =
question.default || question.choices.split('\n')[0];
} else { } else {
values[`survey_${question.variable}`] = question.default || ''; values[`survey_${question.variable}`] = question.default || '';
} }

View File

@@ -15,6 +15,10 @@ const InputGroup = styled(PFInputGroup)`
padding-bottom: 5px; padding-bottom: 5px;
`; `;
const HelperTextWrapper = styled.div`
font-size: var(--pf-c-form__label--FontSize);
`;
const CheckIcon = styled(PFCheckIcon)` const CheckIcon = styled(PFCheckIcon)`
color: var(--pf-c-button--m-plain--disabled--Color); color: var(--pf-c-button--m-plain--disabled--Color);
${props => ${props =>
@@ -53,9 +57,13 @@ function MultipleChoiceField({ label, tooltip }) {
name="formattedChoices" name="formattedChoices"
id="formattedChoices" id="formattedChoices"
helperText={ helperText={
!formattedChoicesField.value[0].choice.trim().length <HelperTextWrapper>
? t`Type answer then click checkbox on right to select answer as default.` {t`Type answer then click checkbox on right to select answer as
: t`Press 'Enter' to add more answer choices. One answer choice per line. ` default.`}
<br />
{t`Press 'Enter' to add more answer choices. One answer
choice per line.`}
</HelperTextWrapper>
} }
helperTextInvalid={formattedChoicesMeta.error} helperTextInvalid={formattedChoicesMeta.error}
onBlur={e => { onBlur={e => {