import React, { useCallback, useMemo } from 'react'; import { hasColumnstoreIndexesSupport } from '../utils/has-columnstore-indexes-support'; import { Combobox, ComboboxOption, Select, Option, spacing, css, ListEditor, } from '@mongodb-js/compass-components'; import type { IndexField } from '../modules/create-index/fields'; /** * Current allowed types for indexes. */ const INDEX_TYPES = ['1 (asc)', '-1 (desc)', '2dsphere', 'text', 'columnstore']; /** * Default values for field name and type as presented in the UI. */ const DEFAULT_FIELD = { name: 'Select or type a field name', type: 'Select a type', }; const createIndexFieldsStyles = css({ display: 'flex', gap: spacing[2], }); const createIndexFieldsNameStyles = css({ flexGrow: 1, textTransform: 'none', whiteSpace: 'nowrap', }); const createIndexFieldsTypeStyles = css({ width: spacing[6] * 2, textTransform: 'none', whiteSpace: 'nowrap', }); export type CreateIndexFieldsProps = { fields: IndexField[]; schemaFields: string[]; serverVersion: string; isRemovable: boolean; newIndexField: string | null; addField: () => void; createNewIndexField: (newField: string) => void; removeField: (idx: number) => void; updateFieldName: (idx: number, name: string) => void; updateFieldType: (idx: number, fType: string) => void; }; function CreateIndexFields({ fields, newIndexField, serverVersion, schemaFields, addField, createNewIndexField, removeField, updateFieldName, updateFieldType, }: CreateIndexFieldsProps): React.ReactElement { const serverHasColumnstoreIndexesSupport = useMemo( () => hasColumnstoreIndexesSupport(serverVersion), [serverVersion] ); const onSelectFieldName = useCallback( (index: number, name: string | null) => { if (name !== null) { updateFieldName(index, name); } }, [updateFieldName] ); const comboboxOptions = useMemo(() => { const options = schemaFields.map((value, index) => ( )); if (newIndexField && !schemaFields.includes(newIndexField)) { options.push( ); } return options; }, [schemaFields, newIndexField]); return ( (
onSelectFieldName(index, fieldName) } clearable={false} > {comboboxOptions}
)} onAddItem={addField} onRemoveItem={removeField} addButtonTestId="add-index-field-button" removeButtonTestId="remove-index-field-button" /> ); } export { CreateIndexFields };