|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React from 'react'; |
| 7 | +import { |
| 8 | + EuiFormRow, |
| 9 | + EuiSuperSelect, |
| 10 | + EuiSuperSelectOption, |
| 11 | + EuiText, |
| 12 | +} from '@elastic/eui'; |
| 13 | +import { Field, FieldProps, useFormikContext } from 'formik'; |
| 14 | +import { |
| 15 | + IComponentField, |
| 16 | + WorkspaceFormValues, |
| 17 | + getInitialValue, |
| 18 | + isFieldInvalid, |
| 19 | +} from '../../../../../common'; |
| 20 | + |
| 21 | +// TODO: Should be fetched from global state. |
| 22 | +// Need to have a way to determine where to fetch this dynamic data. |
| 23 | +const existingIndices = [ |
| 24 | + { |
| 25 | + value: 'index-1', |
| 26 | + inputDisplay: <EuiText>my-index-1</EuiText>, |
| 27 | + disabled: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + value: 'index-2', |
| 31 | + inputDisplay: <EuiText>my-index-2</EuiText>, |
| 32 | + disabled: false, |
| 33 | + }, |
| 34 | +] as Array<EuiSuperSelectOption<string>>; |
| 35 | + |
| 36 | +interface SelectFieldProps { |
| 37 | + field: IComponentField; |
| 38 | + componentId: string; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * An input field for a component where users select from a list of available |
| 43 | + * options. |
| 44 | + */ |
| 45 | +export function SelectField(props: SelectFieldProps) { |
| 46 | + const options = existingIndices; |
| 47 | + const formField = `${props.componentId}.${props.field.name}`; |
| 48 | + const { errors, touched } = useFormikContext<WorkspaceFormValues>(); |
| 49 | + |
| 50 | + return ( |
| 51 | + <Field name={formField}> |
| 52 | + {({ field, form }: FieldProps) => { |
| 53 | + return ( |
| 54 | + <EuiFormRow label={props.field.label}> |
| 55 | + <EuiSuperSelect |
| 56 | + options={options} |
| 57 | + valueOfSelected={field.value || getInitialValue(props.field.type)} |
| 58 | + onChange={(option) => { |
| 59 | + field.onChange(option); |
| 60 | + form.setFieldValue(formField, option); |
| 61 | + }} |
| 62 | + isInvalid={isFieldInvalid( |
| 63 | + props.componentId, |
| 64 | + props.field.name, |
| 65 | + errors, |
| 66 | + touched |
| 67 | + )} |
| 68 | + /> |
| 69 | + </EuiFormRow> |
| 70 | + ); |
| 71 | + }} |
| 72 | + </Field> |
| 73 | + ); |
| 74 | +} |
0 commit comments