some refactoring, and checking and unchecking boxes

This commit is contained in:
Alex Corey 2021-04-14 10:34:31 -04:00
parent e088c7385a
commit 35a9e7e565
2 changed files with 62 additions and 28 deletions

View File

@ -1,13 +1,14 @@
import React, { useState } from 'react';
import { useField } from 'formik';
import { useField, useFormikContext } from 'formik';
import { Checkbox, FormGroup, TextInput } from '@patternfly/react-core';
import { Checkbox, FormGroup, TextInput, Radio } from '@patternfly/react-core';
import styled from 'styled-components';
import Popover from '../Popover';
const InputWrapper = styled.span`
&& {
display: flex;
padding-bottom: 20px;
}
`;
function TextAndCheckboxField({
@ -18,62 +19,96 @@ function TextAndCheckboxField({
isValid,
tooltip,
name,
type,
rows,
...rest
}) {
const [field, meta] = useField({ name });
const [fields, setFields] = useState(field.value.split('\n'));
let array = field.value.split('\n');
const { values: formikValues } = useFormikContext();
const [choicesField, choicesMeta, choicesHelpers] = useField('choices');
// const [fields, setFields] = useState(choicesField.value.split('\n'));
// const [defaultValue, setDefaultValue] = useState(
// formikValues.default.split('\n')
// );
const [, , defaultHelpers] = useField('default');
const [isNewValueChecked, setIsNewValueChecked] = useState(false);
console.log('set');
const choicesSplit = choicesField.value.split('\n');
const defaultSplit = formikValues.default.split('\n');
return (
<FormGroup
fieldId={id}
helperText={helperText}
helperTextInvalid={meta.error}
helperTextInvalid={choicesMeta.error}
isRequired={isRequired}
validated={isValid ? 'default' : 'error'}
label={label}
labelIcon={<Popover content={tooltip} />}
>
{fields
{choicesSplit
.map((v, i) => (
<InputWrapper>
<Checkbox />
{formikValues.type === 'multiselect' ? (
<Checkbox
isChecked={defaultSplit.includes(v)}
onChange={() =>
defaultSplit.includes(v)
? defaultHelpers.setValue(
defaultSplit.filter(d => d !== v).join('\n')
)
: defaultHelpers.setValue(
formikValues.default.concat(`\n${v}`)
)
}
/>
) : (
<Radio
isChecked={defaultSplit.includes(v)}
onChange={defaultHelpers.setValue(`${v}`)}
/>
)}
<TextInput
id={id}
isRequired={isRequired}
validated={isValid ? 'default' : 'error'}
value={v}
type={type}
onChange={value => {
if (value === '') {
setFields(fields.filter((f, index) => i !== index));
} else {
setFields(
fields.map((f, index) => {
if (i === index) {
return value;
}
return f;
})
);
}
defaultHelpers.setValue(
defaultSplit.filter(d => d !== v).join('\n')
);
const newFields = choicesSplit
.map((choice, index) => (i === index ? value : choice))
.join('\n');
return value === ''
? choicesHelpers.setValue(
choicesSplit.filter(d => d !== v).join('\n')
)
: choicesHelpers.setValue(newFields);
}}
/>
</InputWrapper>
))
.concat(
<InputWrapper>
<Checkbox />
{formikValues.type === 'multiselect' ? (
<Checkbox
isChecked={isNewValueChecked}
onChange={setIsNewValueChecked}
/>
) : (
<Radio
isChecked={isNewValueChecked}
onChange={setIsNewValueChecked}
/>
)}
<TextInput
id={id}
isRequired={isRequired}
validated={isValid ? 'default' : 'error'}
value=""
type={type}
onChange={(value, event) => {
setFields([...fields, value]);
choicesHelpers.setValue([...choicesSplit, value].join('\n'));
}}
/>
</InputWrapper>

View File

@ -78,7 +78,6 @@ function SurveyQuestionForm({
return defaultValue => {
let errorMessage;
const found = [...defaultValue].every(dA => {
console.log(dA, '\n', choices);
return choices.indexOf(dA) > -1;
});
@ -211,9 +210,9 @@ function SurveyQuestionForm({
<TextAndCheckboxField
id="question-options"
name="choices"
type="textarea"
type={formik.values.type}
label={t`Multiple Choice Options`}
validate={required(null)}
validate={required()}
tooltip={t`Each answer choice must be on a separate line.`}
isRequired
rows="10"