1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 23:51:09 +03:00

Adds Survey Preview Functionality

This commit is contained in:
Alex Corey 2020-03-25 13:52:06 -04:00
parent b6c272e946
commit 6eef0b82bd
4 changed files with 150 additions and 3 deletions

View File

@ -14,7 +14,7 @@ import {
import { EyeIcon, EyeSlashIcon } from '@patternfly/react-icons'; import { EyeIcon, EyeSlashIcon } from '@patternfly/react-icons';
function PasswordField(props) { function PasswordField(props) {
const { id, name, label, validate, isRequired, i18n } = props; const { id, name, label, validate, isRequired, isDisabled, i18n } = props;
const [inputType, setInputType] = useState('password'); const [inputType, setInputType] = useState('password');
const [field, meta] = useField({ name, validate }); const [field, meta] = useField({ name, validate });
@ -40,6 +40,7 @@ function PasswordField(props) {
variant={ButtonVariant.control} variant={ButtonVariant.control}
aria-label={i18n._(t`Toggle Password`)} aria-label={i18n._(t`Toggle Password`)}
onClick={handlePasswordToggle} onClick={handlePasswordToggle}
isDisabled={isDisabled}
> >
{inputType === 'password' && <EyeSlashIcon />} {inputType === 'password' && <EyeSlashIcon />}
{inputType === 'text' && <EyeIcon />} {inputType === 'text' && <EyeIcon />}
@ -50,6 +51,7 @@ function PasswordField(props) {
placeholder={field.value === '$encrypted$' ? 'ENCRYPTED' : undefined} placeholder={field.value === '$encrypted$' ? 'ENCRYPTED' : undefined}
{...field} {...field}
value={field.value === '$encrypted$' ? '' : field.value} value={field.value === '$encrypted$' ? '' : field.value}
isDisabled={isDisabled}
isRequired={isRequired} isRequired={isRequired}
isValid={isValid} isValid={isValid}
type={inputType} type={inputType}
@ -68,11 +70,13 @@ PasswordField.propTypes = {
label: PropTypes.string.isRequired, label: PropTypes.string.isRequired,
validate: PropTypes.func, validate: PropTypes.func,
isRequired: PropTypes.bool, isRequired: PropTypes.bool,
isDisabled: PropTypes.bool,
}; };
PasswordField.defaultProps = { PasswordField.defaultProps = {
validate: () => {}, validate: () => {},
isRequired: false, isRequired: false,
isDisabled: false,
}; };
export default withI18n()(PasswordField); export default withI18n()(PasswordField);

View File

@ -1,12 +1,19 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { DataList, Button } from '@patternfly/react-core'; import { DataList, Button as _Button } from '@patternfly/react-core';
import ContentLoading from '@components/ContentLoading'; import ContentLoading from '@components/ContentLoading';
import ContentEmpty from '@components/ContentEmpty'; import ContentEmpty from '@components/ContentEmpty';
import AlertModal from '@components/AlertModal'; import AlertModal from '@components/AlertModal';
import styled from 'styled-components';
import SurveyListItem from './SurveyListItem'; import SurveyListItem from './SurveyListItem';
import SurveyToolbar from './SurveyToolbar'; import SurveyToolbar from './SurveyToolbar';
import SurveyPreviewModal from './SurveyPreviewModal';
const Button = styled(_Button)`
margin: 20px;
`;
function SurveyList({ function SurveyList({
isLoading, isLoading,
@ -20,7 +27,7 @@ function SurveyList({
const questions = survey?.spec || []; const questions = survey?.spec || [];
const [selected, setSelected] = useState([]); const [selected, setSelected] = useState([]);
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [isPreviewModalOpen, setIsPreviewModalOpen] = useState(false);
const isAllSelected = const isAllSelected =
selected.length === questions?.length && selected.length > 0; selected.length === questions?.length && selected.length > 0;
@ -92,6 +99,21 @@ function SurveyList({
onMoveDown={moveDown} onMoveDown={moveDown}
/> />
))} ))}
{isPreviewModalOpen && (
<SurveyPreviewModal
isPreviewModalOpen={isPreviewModalOpen}
onToggleModalOpen={() => setIsPreviewModalOpen(false)}
questions={questions}
/>
)}
<Button
onClick={() => setIsPreviewModalOpen(true)}
variant="primary"
aria-label={i18n._(t`Preview`)}
>
Preview
</Button>
</DataList> </DataList>
); );
} }

View File

@ -0,0 +1,120 @@
import React from 'react';
import { withI18n } from '@lingui/react';
import styled from 'styled-components';
import { t } from '@lingui/macro';
import { PasswordField } from '@components/FormField';
import { Formik } from 'formik';
import {
Chip,
Form,
FormGroup,
Modal,
TextInput,
TextArea,
Select,
SelectVariant,
} from '@patternfly/react-core';
// const Chip = styled(_Chip)`
// margin-right: 5px;
// `;
function SurveyPreviewModal({
questions,
isPreviewModalOpen,
onToggleModalOpen,
i18n,
}) {
const initialValues = {};
questions.forEach(q => {
initialValues[q.variable] = q.default;
return initialValues;
});
return (
<Modal
title={i18n._(t`Survey Preview`)}
isOpen={isPreviewModalOpen}
onClose={() => onToggleModalOpen(false)}
isSmall
>
<Formik initialValues={initialValues}>
{() => (
<Form>
{questions.map(q => (
<div key={q.variable}>
{['text', 'integer', 'float'].includes(q.type) && (
<FormGroup
fieldId={`survey-preview-text-${q.variable}`}
label={q.question_name}
>
<TextInput
id={`survey-preview-text-${q.variable}`}
value={q.default}
isDisabled
aria-label={i18n._(t`Text`)}
/>
</FormGroup>
)}
{['textarea'].includes(q.type) && (
<FormGroup
fieldId={`survey-preview-textArea-${q.variable}`}
label={q.question_name}
>
<TextArea
id={`survey-preview-textArea-${q.variable}`}
type={`survey-preview-textArea-${q.variable}`}
value={q.default}
aria-label={i18n._(t`Text Area`)}
disabled
/>
</FormGroup>
)}
{['password'].includes(q.type) && (
<PasswordField
id={`survey-preview-password-${q.variable}`}
label={q.question_name}
name={q.variable}
isDisabled
/>
)}
{['multiplechoice'].includes(q.type) && (
<FormGroup
fieldId={`survey-preview-multipleChoice-${q.variable}`}
label={q.question_name}
>
<Select
id={`survey-preview-multipleChoice-${q.variable}`}
isDisabled
aria-label={i18n._(t`Multiple Choice`)}
placeholderText={q.default}
onToggle={() => {}}
/>
</FormGroup>
)}
{['multiselect'].includes(q.type) && (
<FormGroup
fieldId={`survey-preview-multiSelect-${q.variable}`}
label={q.question_name}
>
<Select
isDisabled
isReadOnly
variant={SelectVariant.typeaheadMulti}
isExpanded={false}
selections={q.default.length > 0 && q.default.split('\n')}
onToggle={() => {}}
aria-label={i18n._(t`Multi-Select`)}
id={`survey-preview-multiSelect-${q.variable}`}
/>
</FormGroup>
)}
</div>
))}
</Form>
)}
</Formik>
</Modal>
);
}
export default withI18n()(SurveyPreviewModal);

View File

@ -1,3 +1,4 @@
export { default as SurveyList } from './SurveyList'; export { default as SurveyList } from './SurveyList';
export { default as SurveyQuestionAdd } from './SurveyQuestionAdd'; export { default as SurveyQuestionAdd } from './SurveyQuestionAdd';
export { default as SurveyQuestionEdit } from './SurveyQuestionEdit'; export { default as SurveyQuestionEdit } from './SurveyQuestionEdit';
export { default as SurveyPreviewModal } from './SurveyPreviewModal';