diff --git a/src/lib/forms/widgets/custom_fields/SubjectAutocompleteDropdown.js b/src/lib/forms/widgets/custom_fields/SubjectAutocompleteDropdown.js new file mode 100644 index 00000000..6ed4b012 --- /dev/null +++ b/src/lib/forms/widgets/custom_fields/SubjectAutocompleteDropdown.js @@ -0,0 +1,91 @@ +import React, { Component } from "react"; +import PropTypes from "prop-types"; +import { RemoteSelectField } from "../../RemoteSelectField"; +import { Field, getIn } from "formik"; + +export class SubjectAutocompleteDropdown extends Component { + serializeSubjects = (subjects) => + subjects.map((subject) => { + const scheme = subject.scheme ? `(${subject.scheme}) ` : ""; + // eslint-disable-next-line no-debugger + debugger; + return { + text: scheme + subject.subject, + value: subject.subject, + key: subject.subject, + ...(subject.id ? { id: subject.id } : {}), + subject: subject.subject, + }; + }); + + prepareSuggest = (searchQuery) => { + const { limitTo } = this.props; + // eslint-disable-next-line no-debugger + debugger; + + const prefix = limitTo === "all" ? "" : `${limitTo}:`; + return `${prefix}${searchQuery}`; + }; + + render() { + const { fieldPath, required, multiple, placeholder, clearable, width } = this.props; + + return ( + + {({ form: { values } }) => { + return ( + ({ + text: value, + value: value, + key: value, + subject: value, + })} + suggestionAPIUrl="/api/subjects" + onValueChange={({ formikProps }, selectedSuggestions) => { + formikProps.form.setFieldValue(fieldPath, selectedSuggestions); + }} + value={getIn(values, fieldPath, []).map((val) => val.subject)} + label={ + <> + {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} + + + } /** For alignment purposes */ + allowAdditions + width={width} + /> + ); + }} + + ); + } +} + +SubjectAutocompleteDropdown.propTypes = { + fieldPath: PropTypes.string.isRequired, + limitTo: PropTypes.string, + required: PropTypes.bool, + multiple: PropTypes.bool, + clearable: PropTypes.bool, + placeholder: PropTypes.string, + width: PropTypes.number, +}; + +SubjectAutocompleteDropdown.defaultProps = { + required: false, + limitTo: "", + multiple: true, + clearable: true, + placeholder: "Search for a subject by name", + width: undefined, +}; diff --git a/src/lib/forms/widgets/custom_fields/index.js b/src/lib/forms/widgets/custom_fields/index.js index b57a438d..726f6102 100644 --- a/src/lib/forms/widgets/custom_fields/index.js +++ b/src/lib/forms/widgets/custom_fields/index.js @@ -1 +1,2 @@ export { CustomFields } from "./CustomFields"; +export { SubjectAutocompleteDropdown } from "./SubjectAutocompleteDropdown";