import React from 'react'; import { Label, TextInput, css, cx, spacing, palette, useDarkMode, } from '@mongodb-js/compass-components'; import type { Listenable } from 'reflux'; import { OptionEditor } from './option-editor'; import { OPTION_DEFINITION } from '../constants/query-option-definition'; import type { QueryOption as QueryOptionType } from '../constants/query-option-definition'; import type { CompletionWithServerInfo } from '@mongodb-js/compass-editor'; const queryOptionStyles = css({ display: 'flex', position: 'relative', alignItems: 'flex-start', }); const documentEditorOptionContainerStyles = css({ flexGrow: 1, }); const queryOptionLabelStyles = css({ marginRight: spacing[2], }); const documentEditorQueryOptionLabelStyles = cx( queryOptionLabelStyles, css({ minWidth: spacing[5] * 3, }) ); const documentEditorOptionStyles = css({ minWidth: spacing[7], display: 'flex', flexGrow: 1, }); const numericTextInputLightStyles = css({ input: { borderColor: 'transparent', }, }); const numericTextInputDarkStyles = css({ input: { borderColor: 'transparent', }, }); const optionInputWithErrorStyles = css({ input: { borderColor: palette.red.base, }, }); const queryOptionLabelContainerStyles = css({ // Hardcoded height as we want the label not to vertically // center on the input area when it's expanded. height: spacing[4] + spacing[1], textTransform: 'capitalize', display: 'flex', alignItems: 'center', }); export const documentEditorLabelContainerStyles = cx( queryOptionLabelContainerStyles, css({ minWidth: spacing[5] * 3, }) ); type QueryOptionProps = { hasError: boolean; id: string; onChange: (value: string) => void; onApply: () => void; placeholder?: string; queryOption: QueryOptionType; refreshEditorAction: Listenable; schemaFields: CompletionWithServerInfo[]; serverVersion: string; value?: string; }; // Helper component to allow flexible computation of extra props for the TextInput // component if the query option definition suggests it. In particular, // using a separate component allows those extra props to use React hooks in their definition. const WithOptionDefinitionTextInputProps: React.FunctionComponent<{ definition: typeof OPTION_DEFINITION[QueryOptionType]; children: ({ props, }: { props: Partial>; }) => JSX.Element; }> = ({ definition, children }) => { let props: Partial> = {}; if (definition.type === 'numeric') { props.inputMode = 'numeric'; props.pattern = '[0-9]*'; } props = { ...props, ...definition.extraTextInputProps?.() }; return <>{children({ props })}; }; const QueryOption: React.FunctionComponent = ({ hasError, onApply, onChange, id, placeholder = '', queryOption, refreshEditorAction, schemaFields = [], serverVersion, value = '', }) => { const darkMode = useDarkMode(); const optionDefinition = OPTION_DEFINITION[queryOption]; const isDocumentEditor = optionDefinition.type === 'document'; placeholder ||= optionDefinition.placeholder; return (
{/* The filter label is shown by the query bar. */} {queryOption !== 'filter' && (
)}
{isDocumentEditor ? ( ) : ( {({ props }) => ( ) => onChange(evt.target.value) } placeholder={placeholder} {...props} /> )} )}
); }; export { QueryOption };