mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 01:47:35 -02:30
some refactoring, and checking and unchecking boxes
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
import React, { useState } from 'react';
|
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 styled from 'styled-components';
|
||||||
import Popover from '../Popover';
|
import Popover from '../Popover';
|
||||||
|
|
||||||
const InputWrapper = styled.span`
|
const InputWrapper = styled.span`
|
||||||
&& {
|
&& {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
padding-bottom: 20px;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
function TextAndCheckboxField({
|
function TextAndCheckboxField({
|
||||||
@@ -18,62 +19,96 @@ function TextAndCheckboxField({
|
|||||||
isValid,
|
isValid,
|
||||||
tooltip,
|
tooltip,
|
||||||
name,
|
name,
|
||||||
type,
|
|
||||||
rows,
|
rows,
|
||||||
...rest
|
...rest
|
||||||
}) {
|
}) {
|
||||||
const [field, meta] = useField({ name });
|
const { values: formikValues } = useFormikContext();
|
||||||
const [fields, setFields] = useState(field.value.split('\n'));
|
const [choicesField, choicesMeta, choicesHelpers] = useField('choices');
|
||||||
let array = field.value.split('\n');
|
// 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 (
|
return (
|
||||||
<FormGroup
|
<FormGroup
|
||||||
fieldId={id}
|
fieldId={id}
|
||||||
helperText={helperText}
|
helperText={helperText}
|
||||||
helperTextInvalid={meta.error}
|
helperTextInvalid={choicesMeta.error}
|
||||||
isRequired={isRequired}
|
isRequired={isRequired}
|
||||||
validated={isValid ? 'default' : 'error'}
|
validated={isValid ? 'default' : 'error'}
|
||||||
label={label}
|
label={label}
|
||||||
labelIcon={<Popover content={tooltip} />}
|
labelIcon={<Popover content={tooltip} />}
|
||||||
>
|
>
|
||||||
{fields
|
{choicesSplit
|
||||||
.map((v, i) => (
|
.map((v, i) => (
|
||||||
<InputWrapper>
|
<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
|
<TextInput
|
||||||
id={id}
|
id={id}
|
||||||
isRequired={isRequired}
|
isRequired={isRequired}
|
||||||
validated={isValid ? 'default' : 'error'}
|
validated={isValid ? 'default' : 'error'}
|
||||||
value={v}
|
value={v}
|
||||||
type={type}
|
|
||||||
onChange={value => {
|
onChange={value => {
|
||||||
if (value === '') {
|
defaultHelpers.setValue(
|
||||||
setFields(fields.filter((f, index) => i !== index));
|
defaultSplit.filter(d => d !== v).join('\n')
|
||||||
} else {
|
);
|
||||||
setFields(
|
|
||||||
fields.map((f, index) => {
|
const newFields = choicesSplit
|
||||||
if (i === index) {
|
.map((choice, index) => (i === index ? value : choice))
|
||||||
return value;
|
.join('\n');
|
||||||
}
|
|
||||||
return f;
|
return value === ''
|
||||||
})
|
? choicesHelpers.setValue(
|
||||||
);
|
choicesSplit.filter(d => d !== v).join('\n')
|
||||||
}
|
)
|
||||||
|
: choicesHelpers.setValue(newFields);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</InputWrapper>
|
</InputWrapper>
|
||||||
))
|
))
|
||||||
.concat(
|
.concat(
|
||||||
<InputWrapper>
|
<InputWrapper>
|
||||||
<Checkbox />
|
{formikValues.type === 'multiselect' ? (
|
||||||
|
<Checkbox
|
||||||
|
isChecked={isNewValueChecked}
|
||||||
|
onChange={setIsNewValueChecked}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<Radio
|
||||||
|
isChecked={isNewValueChecked}
|
||||||
|
onChange={setIsNewValueChecked}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<TextInput
|
<TextInput
|
||||||
id={id}
|
id={id}
|
||||||
isRequired={isRequired}
|
isRequired={isRequired}
|
||||||
validated={isValid ? 'default' : 'error'}
|
validated={isValid ? 'default' : 'error'}
|
||||||
value=""
|
value=""
|
||||||
type={type}
|
|
||||||
onChange={(value, event) => {
|
onChange={(value, event) => {
|
||||||
setFields([...fields, value]);
|
choicesHelpers.setValue([...choicesSplit, value].join('\n'));
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</InputWrapper>
|
</InputWrapper>
|
||||||
|
|||||||
@@ -78,7 +78,6 @@ function SurveyQuestionForm({
|
|||||||
return defaultValue => {
|
return defaultValue => {
|
||||||
let errorMessage;
|
let errorMessage;
|
||||||
const found = [...defaultValue].every(dA => {
|
const found = [...defaultValue].every(dA => {
|
||||||
console.log(dA, '\n', choices);
|
|
||||||
return choices.indexOf(dA) > -1;
|
return choices.indexOf(dA) > -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -211,9 +210,9 @@ function SurveyQuestionForm({
|
|||||||
<TextAndCheckboxField
|
<TextAndCheckboxField
|
||||||
id="question-options"
|
id="question-options"
|
||||||
name="choices"
|
name="choices"
|
||||||
type="textarea"
|
type={formik.values.type}
|
||||||
label={t`Multiple Choice Options`}
|
label={t`Multiple Choice Options`}
|
||||||
validate={required(null)}
|
validate={required()}
|
||||||
tooltip={t`Each answer choice must be on a separate line.`}
|
tooltip={t`Each answer choice must be on a separate line.`}
|
||||||
isRequired
|
isRequired
|
||||||
rows="10"
|
rows="10"
|
||||||
|
|||||||
Reference in New Issue
Block a user