Skip to content

Commit

Permalink
fix(streams): update form typing
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyghiani committed Jan 20, 2025
1 parent 66c3b82 commit 060d24d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import { CodeEditor } from '@kbn/code-editor';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { useKibana } from '../../../../hooks/use_kibana';
import { ProcessorFormState } from '../../types';

export const DissectPatternDefinition = () => {
const { core } = useKibana();
const esDocUrl = core.docLinks.links.ingest.dissectKeyModifiers;

const { field, fieldState } = useController({
const { field, fieldState } = useController<ProcessorFormState, 'pattern'>({
name: 'pattern',
rules: {
required: i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import { useController } from 'react-hook-form';
import { EuiFormRow } from '@elastic/eui';
import { CodeEditor } from '@kbn/code-editor';
import { i18n } from '@kbn/i18n';
import { ProcessorFormState } from '../../types';

export const GrokPatternDefinition = () => {
const { field, fieldState } = useController({ name: 'pattern_definitions' });
const { field, fieldState } = useController<ProcessorFormState, 'pattern_definitions'>({
name: 'pattern_definitions',
});

return (
<EuiFormRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ const DraggablePatternInput = ({
color="transparent"
paddingSize="xs"
{...provided.dragHandleProps}
aria-label="Drag Handle"
aria-label={i18n.translate(
'xpack.streams.streamDetailView.managementTab.enrichment.processorFlyout.grokEditor.dragHandleLabel',
{ defaultMessage: 'Drag Handle' }
)}
>
<EuiIcon type="grab" />
</EuiPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import React from 'react';
import { useController } from 'react-hook-form';
import { ConditionEditor } from '../../condition_editor';
import { ProcessorFormState } from '../types';

export const ProcessorConditionEditor = () => {
const { field } = useController({ name: 'condition' });
const { field } = useController<ProcessorFormState, 'condition'>({ name: 'condition' });

return <ConditionEditor condition={field.value} onConditionChange={field.onChange} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { EuiFormRow, EuiFieldText } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import React from 'react';
import { useController } from 'react-hook-form';
import { ProcessorFormState } from '../types';

export const ProcessorFieldSelector = () => {
const { field, fieldState } = useController({
const { field, fieldState } = useController<ProcessorFormState, 'field'>({
name: 'field',
rules: {
required: i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
ReadStreamDefinition,
isWiredReadStream,
} from '@kbn/streams-schema';
import { useController, useFieldArray } from 'react-hook-form';
import { UseControllerProps, useController, useFieldArray } from 'react-hook-form';
import { css } from '@emotion/react';
import { flattenObject } from '@kbn/object-utils';
import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public';
Expand Down Expand Up @@ -287,17 +287,19 @@ const DetectedFields = ({ detectedFields }: { detectedFields: DetectedField[] })
>
<EuiFlexGroup gutterSize="s" wrap>
{fields.map((field, id) => (
<DetectedFieldSelector key={field.name} selectorId={`detected_fields.${id}`} />
<DetectedFieldSelector key={field.name} name={`detected_fields.${id}`} />
))}
</EuiFlexGroup>
</EuiFormRow>
);
};

const DetectedFieldSelector = ({ selectorId }: { selectorId: string }) => {
const { field } = useController({ name: selectorId });
const DetectedFieldSelector = (
props: UseControllerProps<ProcessorFormState, `detected_fields.${number}`>
) => {
const { field } = useController(props);

const options = useMemo(() => getDetectedFieldSelectOptions(field.value), [field]);
const options = useMemo(() => getDetectedFieldSelectOptions(field.value), [field.value]);

return (
<EuiSuperSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useController, useFormContext, useWatch } from 'react-hook-form';
import { ProcessorType } from '@kbn/streams-schema';
import { useKibana } from '../../../hooks/use_kibana';
import { getDefaultFormState } from '../utils';
import { ProcessorFormState } from '../types';

interface TAvailableProcessor {
value: ProcessorType;
Expand All @@ -28,8 +29,11 @@ export const ProcessorTypeSelector = ({
const { core } = useKibana();
const esDocUrl = core.docLinks.links.elasticsearch.docsBase;

const { control, reset } = useFormContext();
const { field, fieldState } = useController({ name: 'type', control, rules: { required: true } });
const { reset } = useFormContext();
const { field, fieldState } = useController<ProcessorFormState, 'type'>({
name: 'type',
rules: { required: true },
});

const processorType = useWatch<{ type: ProcessorType }>({ name: 'type' });

Expand Down Expand Up @@ -74,7 +78,9 @@ const availableProcessors: TAvailableProcessors = {
values={{
dissectLink: (
<EuiLink external target="_blank" href={esDocUrl + 'dissect-processor.html'}>
dissect
{i18n.translate('xpack.streams.availableProcessors.dissectLinkLabel', {
defaultMessage: 'dissect',
})}
</EuiLink>
),
}}
Expand All @@ -91,7 +97,9 @@ const availableProcessors: TAvailableProcessors = {
values={{
grokLink: (
<EuiLink external target="_blank" href={esDocUrl + 'grok-processor.html'}>
grok
{i18n.translate('xpack.streams.availableProcessors.grokLinkLabel', {
defaultMessage: 'grok',
})}
</EuiLink>
),
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import React from 'react';
import { useController } from 'react-hook-form';
import { EuiFormRow, EuiFormRowProps, EuiSwitch, htmlIdGenerator } from '@elastic/eui';
import { ProcessorFormState } from '../types';

type ExtractBooleanFields<TInput> = NonNullable<
{
[K in keyof TInput]: boolean extends TInput[K] ? K : never;
}[keyof TInput]
>;

interface ToggleFieldProps {
helpText?: EuiFormRowProps['helpText'];
id?: string;
label: string;
name: string;
name: ExtractBooleanFields<ProcessorFormState>;
}

export const ToggleField = ({
Expand All @@ -23,14 +30,16 @@ export const ToggleField = ({
name,
...rest
}: ToggleFieldProps) => {
const { field } = useController({ name });
const { field } = useController<ProcessorFormState, ToggleFieldProps['name']>({
name,
});

return (
<EuiFormRow helpText={helpText} fullWidth describedByIds={id ? [id] : undefined} {...rest}>
<EuiSwitch
id={id}
label={label}
checked={field.value}
checked={field.value ?? false}
onChange={(e) => field.onChange(e.target.checked)}
/>
</EuiFormRow>
Expand Down

0 comments on commit 060d24d

Please sign in to comment.