-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add imperative delete field api (#93181)
- Loading branch information
Showing
16 changed files
with
496 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/plugins/index_pattern_field_editor/public/components/delete_field_modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiConfirmModal } from '@elastic/eui'; | ||
|
||
const geti18nTexts = (fieldsToDelete?: string[]) => { | ||
let modalTitle = ''; | ||
if (fieldsToDelete) { | ||
const isSingle = fieldsToDelete.length === 1; | ||
|
||
modalTitle = isSingle | ||
? i18n.translate( | ||
'indexPatternFieldEditor.deleteRuntimeField.confirmModal.deleteSingleTitle', | ||
{ | ||
defaultMessage: `Remove field '{name}'?`, | ||
values: { name: fieldsToDelete[0] }, | ||
} | ||
) | ||
: i18n.translate( | ||
'indexPatternFieldEditor.deleteRuntimeField.confirmModal.deleteMultipleTitle', | ||
{ | ||
defaultMessage: `Remove {count} fields?`, | ||
values: { count: fieldsToDelete.length }, | ||
} | ||
); | ||
} | ||
|
||
return { | ||
modalTitle, | ||
confirmButtonText: i18n.translate( | ||
'indexPatternFieldEditor.deleteRuntimeField.confirmationModal.removeButtonLabel', | ||
{ | ||
defaultMessage: 'Remove', | ||
} | ||
), | ||
cancelButtonText: i18n.translate( | ||
'indexPatternFieldEditor.deleteRuntimeField.confirmationModal.cancelButtonLabel', | ||
{ | ||
defaultMessage: 'Cancel', | ||
} | ||
), | ||
warningMultipleFields: i18n.translate( | ||
'indexPatternFieldEditor.deleteRuntimeField.confirmModal.multipleDeletionDescription', | ||
{ | ||
defaultMessage: 'You are about to remove these runtime fields:', | ||
} | ||
), | ||
}; | ||
}; | ||
|
||
export interface Props { | ||
fieldsToDelete: string[]; | ||
closeModal: () => void; | ||
confirmDelete: () => void; | ||
} | ||
|
||
export function DeleteFieldModal({ fieldsToDelete, closeModal, confirmDelete }: Props) { | ||
const i18nTexts = geti18nTexts(fieldsToDelete); | ||
const { modalTitle, confirmButtonText, cancelButtonText, warningMultipleFields } = i18nTexts; | ||
const isMultiple = Boolean(fieldsToDelete.length > 1); | ||
return ( | ||
<EuiConfirmModal | ||
title={modalTitle} | ||
data-test-subj="runtimeFieldDeleteConfirmModal" | ||
onCancel={closeModal} | ||
onConfirm={confirmDelete} | ||
cancelButtonText={cancelButtonText} | ||
buttonColor="danger" | ||
confirmButtonText={confirmButtonText} | ||
> | ||
{isMultiple && ( | ||
<> | ||
<p>{warningMultipleFields}</p> | ||
<ul> | ||
{fieldsToDelete.map((fieldName) => ( | ||
<li key={fieldName}>{fieldName}</li> | ||
))} | ||
</ul> | ||
</> | ||
)} | ||
</EuiConfirmModal> | ||
); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/plugins/index_pattern_field_editor/public/components/delete_field_provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { useCallback, useRef, useEffect } from 'react'; | ||
|
||
import { IndexPattern } from '../shared_imports'; | ||
import { OpenFieldDeleteModalOptions } from '../open_delete_modal'; | ||
import { CloseEditor } from '../types'; | ||
|
||
type DeleteFieldFunc = (fieldName: string | string[]) => void; | ||
export interface Props { | ||
children: (deleteFieldHandler: DeleteFieldFunc) => React.ReactNode; | ||
indexPattern: IndexPattern; | ||
onDelete?: (fieldNames: string[]) => void; | ||
} | ||
|
||
export const getDeleteFieldProvider = ( | ||
modalOpener: (options: OpenFieldDeleteModalOptions) => CloseEditor | ||
): React.FunctionComponent<Props> => { | ||
return React.memo(({ indexPattern, children, onDelete }: Props) => { | ||
const closeModal = useRef<CloseEditor | null>(null); | ||
const deleteFields = useCallback( | ||
async (fieldName: string | string[]) => { | ||
if (closeModal.current) { | ||
closeModal.current(); | ||
} | ||
closeModal.current = modalOpener({ | ||
ctx: { | ||
indexPattern, | ||
}, | ||
fieldName, | ||
onDelete, | ||
}); | ||
}, | ||
[onDelete, indexPattern] | ||
); | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (closeModal.current) { | ||
closeModal.current(); | ||
} | ||
}; | ||
}, []); | ||
|
||
return <>{children(deleteFields)}</>; | ||
}); | ||
}; |
Oops, something went wrong.