|
| 1 | +import React, { useContext } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import { DIALOG_FIELD_TYPES } from './constants'; |
| 5 | +import CheckboxField from './dialogFieldItems/CheckboxField'; |
| 6 | +import DateField from './dialogFieldItems/DateField'; |
| 7 | +import DateTimeField from './dialogFieldItems/DateTimeField'; |
| 8 | +import DropDownField from './dialogFieldItems/DropDownField'; |
| 9 | +import RadioField from './dialogFieldItems/RadioField'; |
| 10 | +import RefreshField from './RefreshField'; |
| 11 | +import ServiceContext from './ServiceContext'; |
| 12 | +import TagField from './dialogFieldItems/TagField'; |
| 13 | +import TextInputField from './dialogFieldItems/TextInputField'; |
| 14 | +import TextAreaField from './dialogFieldItems/TextAreaField'; |
| 15 | + |
| 16 | +/** Function to render fields based on type */ |
| 17 | +const renderFieldContent = (field) => { |
| 18 | + switch (field.type) { |
| 19 | + case DIALOG_FIELD_TYPES.checkBox: |
| 20 | + return <CheckboxField field={field} />; |
| 21 | + case DIALOG_FIELD_TYPES.date: |
| 22 | + return <DateField field={field} />; |
| 23 | + case DIALOG_FIELD_TYPES.dateTime: |
| 24 | + return <DateTimeField field={field} />; |
| 25 | + case DIALOG_FIELD_TYPES.dropDown: |
| 26 | + return <DropDownField field={field} />; |
| 27 | + case DIALOG_FIELD_TYPES.radio: |
| 28 | + return <RadioField field={field} />; |
| 29 | + case DIALOG_FIELD_TYPES.tag: |
| 30 | + return <TagField field={field} />; |
| 31 | + case DIALOG_FIELD_TYPES.textBox: |
| 32 | + return <TextInputField field={field} />; |
| 33 | + case DIALOG_FIELD_TYPES.textArea: |
| 34 | + return <TextAreaField field={field} />; |
| 35 | + default: |
| 36 | + return <>{__('Field not supported')}</>; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +/** Function to render a field. */ |
| 41 | +const renderFieldItem = (field, data) => { |
| 42 | + const isRefreshing = data.fieldsToRefresh.includes(field.name); |
| 43 | + return ( |
| 44 | + <div |
| 45 | + className={classNames('section-field-row', isRefreshing && 'field-refresh-in-progress')} |
| 46 | + key={field.id.toString()} |
| 47 | + id={`section-field-row-${field.name}`} |
| 48 | + > |
| 49 | + <div className="field-item"> |
| 50 | + { |
| 51 | + renderFieldContent(field) |
| 52 | + } |
| 53 | + </div> |
| 54 | + <RefreshField field={field} /> |
| 55 | + </div> |
| 56 | + ); |
| 57 | +}; |
| 58 | + |
| 59 | +/** Component to render the Fields in the Service/DialogTabs/DialogGroups component */ |
| 60 | +const DialogFields = ({ dialogFields }) => { |
| 61 | + const { data } = useContext(ServiceContext); |
| 62 | + return ( |
| 63 | + <> |
| 64 | + { |
| 65 | + dialogFields.map((field) => ( |
| 66 | + field.visible ? renderFieldItem(field, data) : <span key={field.id.toString()} /> |
| 67 | + )) |
| 68 | + } |
| 69 | + </> |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +DialogFields.propTypes = { |
| 74 | + dialogFields: PropTypes.arrayOf(PropTypes.any).isRequired, |
| 75 | +}; |
| 76 | + |
| 77 | +export default DialogFields; |
0 commit comments