|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import Text from 'terra-text'; |
| 4 | +import Field from 'terra-form-field'; |
| 5 | +import Select from 'react-select'; |
| 6 | + |
| 7 | +import styles from './patient-select.css'; |
| 8 | + |
| 9 | +const propTypes = { |
| 10 | + /** |
| 11 | + * If the modal needs to present the current FHIR server at the top, pass this prop in |
| 12 | + */ |
| 13 | + currentFhirServer: PropTypes.string, |
| 14 | + /** |
| 15 | + * The field label for the Field component (i.e. "Change Patient") |
| 16 | + */ |
| 17 | + formFieldLabel: PropTypes.string.isRequired, |
| 18 | + /** |
| 19 | + * A boolean flag to display an error if needed on the Field component |
| 20 | + */ |
| 21 | + shouldDisplayError: PropTypes.bool.isRequired, |
| 22 | + /** |
| 23 | + * If a error needs to be displayed in the Field component, accompany it with a message |
| 24 | + */ |
| 25 | + errorMessage: PropTypes.string, |
| 26 | + /** |
| 27 | + * If the Input component needs placeholder text (usually to help the user with example values), pass this prop in |
| 28 | + */ |
| 29 | + placeholderText: PropTypes.string, |
| 30 | + /** |
| 31 | + * If the value in the Input component changes (i.e user selects option), pass in a function callback to handle the text |
| 32 | + */ |
| 33 | + inputOnChange: PropTypes.func.isRequired, |
| 34 | + /** |
| 35 | + * The name attribute for the Input component |
| 36 | + */ |
| 37 | + inputName: PropTypes.string, |
| 38 | + /** |
| 39 | + * A list of the Patient identifiers that populate the select options |
| 40 | + */ |
| 41 | + patients: PropTypes.array.isRequired |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * PatientSelect (functional component) serves as the base UI inside modal interactions like "Change Patient". |
| 46 | + * It contains a Field for selecting an associated input (i.e. "Select a Patient"), and an Input for |
| 47 | + * allowing users to input text below its associated Field. Additionally, if relevant, the modal may present Text at the top which |
| 48 | + * displays the current FHIR server in context (useful for "Select a Patient" modals). |
| 49 | + * |
| 50 | + * How to use: Use this component if a modal needs to have some base UI for allowing a user to select an option, given some |
| 51 | + * Field text (i.e. "Select a Patient") |
| 52 | + * |
| 53 | + */ |
| 54 | +const PatientSelect = ({ |
| 55 | + currentFhirServer, formFieldLabel, shouldDisplayError, |
| 56 | + errorMessage, placeholderText, inputOnChange, inputName, |
| 57 | + patients, |
| 58 | +}) => { |
| 59 | + let fhirServerDisplay; |
| 60 | + if (currentFhirServer) { |
| 61 | + fhirServerDisplay = ( |
| 62 | + <div> |
| 63 | + <Text weight={400} fontSize={16}>Current FHIR server</Text> |
| 64 | + <br /> |
| 65 | + <Text weight={200} fontSize={14}>{currentFhirServer}</Text> |
| 66 | + </div> |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className={styles.container}> |
| 72 | + {fhirServerDisplay} |
| 73 | + <div className={styles['vertical-separation']}> |
| 74 | + <Field |
| 75 | + label={formFieldLabel} |
| 76 | + isInvalid={shouldDisplayError} |
| 77 | + error={errorMessage} |
| 78 | + required |
| 79 | + > |
| 80 | + <Select |
| 81 | + placeholder={placeholderText} |
| 82 | + value={placeholderText} |
| 83 | + onChange={inputOnChange} |
| 84 | + options={patients} |
| 85 | + /> |
| 86 | + </Field> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +PatientSelect.propTypes = propTypes; |
| 93 | + |
| 94 | +export default PatientSelect; |
0 commit comments