|
| 1 | +import React, { Component } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import { RemoteSelectField } from "../../RemoteSelectField"; |
| 4 | +import { Field, getIn } from "formik"; |
| 5 | +import { FieldLabel } from "../../FieldLabel"; |
| 6 | + |
| 7 | +export class SubjectAutocompleteDropdown extends Component { |
| 8 | + serializeSubjects = (subjects) => |
| 9 | + subjects.map((subject) => { |
| 10 | + const scheme = subject.scheme ? `(${subject.scheme}) ` : ""; |
| 11 | + return { |
| 12 | + text: scheme + subject.subject, |
| 13 | + value: subject.subject, |
| 14 | + key: subject.subject, |
| 15 | + ...(subject.id ? { id: subject.id } : {}), |
| 16 | + subject: subject.subject, |
| 17 | + }; |
| 18 | + }); |
| 19 | + |
| 20 | + prepareSuggest = (searchQuery) => { |
| 21 | + const { limitTo } = this.props; |
| 22 | + return limitTo === "" || limitTo === "all" |
| 23 | + ? searchQuery |
| 24 | + : `${limitTo}:${searchQuery}`; |
| 25 | + }; |
| 26 | + |
| 27 | + render() { |
| 28 | + const { |
| 29 | + fieldPath, |
| 30 | + required, |
| 31 | + multiple, |
| 32 | + placeholder, |
| 33 | + clearable, |
| 34 | + label, |
| 35 | + icon, |
| 36 | + width, |
| 37 | + allowAdditions, |
| 38 | + noQueryMessage, |
| 39 | + } = this.props; |
| 40 | + const labelContent = label ? ( |
| 41 | + <FieldLabel htmlFor={fieldPath} icon={icon} label={label} /> |
| 42 | + ) : ( |
| 43 | + <> |
| 44 | + {/* Placeholder label for alignment purposes */} |
| 45 | + <label htmlFor={fieldPath} className="mobile-hidden"> |
| 46 | + |
| 47 | + </label> |
| 48 | + </> |
| 49 | + ); |
| 50 | + |
| 51 | + return ( |
| 52 | + <Field name={fieldPath}> |
| 53 | + {({ form: { values } }) => ( |
| 54 | + <RemoteSelectField |
| 55 | + clearable={clearable} |
| 56 | + fieldPath={fieldPath} |
| 57 | + initialSuggestions={getIn(values, fieldPath, [])} |
| 58 | + multiple={multiple} |
| 59 | + noQueryMessage={noQueryMessage} |
| 60 | + placeholder={placeholder} |
| 61 | + preSearchChange={this.prepareSuggest} |
| 62 | + required={required} |
| 63 | + serializeSuggestions={this.serializeSubjects} |
| 64 | + serializeAddedValue={(value) => ({ |
| 65 | + text: value, |
| 66 | + value: value, |
| 67 | + key: value, |
| 68 | + subject: value, |
| 69 | + })} |
| 70 | + suggestionAPIUrl="/api/subjects" |
| 71 | + onValueChange={({ formikProps }, selectedSuggestions) => { |
| 72 | + formikProps.form.setFieldValue( |
| 73 | + fieldPath, |
| 74 | + selectedSuggestions.map((subject) => ({ |
| 75 | + subject: subject.subject, |
| 76 | + id: subject.id, |
| 77 | + })) |
| 78 | + ); |
| 79 | + }} |
| 80 | + label={labelContent} |
| 81 | + value={getIn(values, fieldPath, []).map((val) => val.subject)} |
| 82 | + allowAdditions={allowAdditions} |
| 83 | + width={width} |
| 84 | + /> |
| 85 | + )} |
| 86 | + </Field> |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +SubjectAutocompleteDropdown.propTypes = { |
| 92 | + fieldPath: PropTypes.string.isRequired, |
| 93 | + limitTo: PropTypes.string, |
| 94 | + label: PropTypes.string, |
| 95 | + icon: PropTypes.string, |
| 96 | + required: PropTypes.bool, |
| 97 | + multiple: PropTypes.bool, |
| 98 | + clearable: PropTypes.bool, |
| 99 | + placeholder: PropTypes.string, |
| 100 | + width: PropTypes.number, |
| 101 | + allowAdditions: PropTypes.bool, |
| 102 | + noQueryMessage: PropTypes.string, |
| 103 | +}; |
| 104 | + |
| 105 | +SubjectAutocompleteDropdown.defaultProps = { |
| 106 | + required: false, |
| 107 | + limitTo: "", |
| 108 | + label: "", |
| 109 | + icon: undefined, |
| 110 | + multiple: true, |
| 111 | + clearable: true, |
| 112 | + placeholder: "Search for a subject by name", |
| 113 | + width: undefined, |
| 114 | + noQueryMessage: "Search or create subjects...", |
| 115 | + allowAdditions: true, |
| 116 | +}; |
0 commit comments