|
| 1 | +import React, { useRef } from "react"; |
| 2 | +import { IconButton, InputAdornment, TextField } from "@material-ui/core"; |
| 3 | +import { SelectScopeModal } from "@mov-ai/mov-fe-lib-react"; |
| 4 | +import { Document } from "@mov-ai/mov-fe-lib-core"; |
| 5 | +import CodeIcon from "@material-ui/icons/Code"; |
| 6 | +import PropTypes from "prop-types"; |
| 7 | + |
| 8 | +const ConfigurationSelector = (props) => { |
| 9 | + const [openModal, setOpenModal] = React.useState(false); |
| 10 | + const [selected, setSelected] = React.useState(null); |
| 11 | + const inputTextRef = useRef(); |
| 12 | + const rowProps = props.rowProps; |
| 13 | + const rowData = props.rowProps?.rowData; |
| 14 | + |
| 15 | + const formatConfigurationValue = (configuration) => { |
| 16 | + const document = Document.parsePath(configuration, SCOPES.Configuration); |
| 17 | + // Temporary validation if document is from archive |
| 18 | + // TO BE REMOVED AFTER STANDARDIZATION OF PARSING PROCESS |
| 19 | + if (document.workspace !== "global") { |
| 20 | + props.interface.alert( |
| 21 | + "Please note that only configurations from 'global' workspace are accepted at the moment.", |
| 22 | + props.interface.ALERTS.warning, |
| 23 | + ); |
| 24 | + } |
| 25 | + // Return formatted config name |
| 26 | + return document.name; |
| 27 | + }; |
| 28 | + |
| 29 | + return ( |
| 30 | + <TextField |
| 31 | + style={{ width: "100%" }} |
| 32 | + value={rowData?.value || ""} |
| 33 | + data-testid="selector-text-input" |
| 34 | + onChange={(evt) => rowProps?.onChange(evt.target.value)} |
| 35 | + InputProps={{ |
| 36 | + ref: inputTextRef, |
| 37 | + endAdornment: ( |
| 38 | + <InputAdornment position="end"> |
| 39 | + <IconButton |
| 40 | + data-testid="open-selector-btn" |
| 41 | + aria-label="Select configuration" |
| 42 | + onClick={() => setOpenModal(true)} |
| 43 | + disabled={props.rowProps.disabled} |
| 44 | + onMouseDown={(evt) => evt.preventDefault()} |
| 45 | + > |
| 46 | + <CodeIcon></CodeIcon> |
| 47 | + </IconButton> |
| 48 | + <SelectScopeModal |
| 49 | + open={openModal} |
| 50 | + onCancel={() => setOpenModal(false)} |
| 51 | + onSubmit={(selectedConfiguration) => { |
| 52 | + const formatted = formatConfigurationValue( |
| 53 | + selectedConfiguration, |
| 54 | + ); |
| 55 | + rowProps.onChange(formatted); |
| 56 | + setSelected(selectedConfiguration); |
| 57 | + setOpenModal(false); |
| 58 | + // Set cursor position |
| 59 | + setImmediate(() => { |
| 60 | + if (!inputTextRef.current) return; |
| 61 | + const inputText = inputTextRef.current.querySelector("input"); |
| 62 | + inputText.focus(); |
| 63 | + const endPosition = inputText.value.length; |
| 64 | + inputText.setSelectionRange(endPosition, endPosition); |
| 65 | + }); |
| 66 | + }} |
| 67 | + scopeList={[SCOPES.Configuration]} |
| 68 | + selected={selected} |
| 69 | + allowArchive={false} |
| 70 | + ></SelectScopeModal> |
| 71 | + </InputAdornment> |
| 72 | + ), |
| 73 | + }} |
| 74 | + /> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +const SCOPES = { |
| 79 | + Configuration: "Configuration", |
| 80 | +}; |
| 81 | + |
| 82 | +ConfigurationSelector.defaultProps = {}; |
| 83 | + |
| 84 | +ConfigurationSelector.propTypes = { |
| 85 | + interface: PropTypes.object.isRequired, |
| 86 | +}; |
| 87 | + |
| 88 | +export default ConfigurationSelector; |
0 commit comments