kibana/x-pack/plugins/lens/public/app_plugin/tags_saved_object_save_modal_origin_wrapper.tsx
Brandon Kobel 4584a8b570
Elastic License 2.0 (#90099)
* Updating everything except the license headers themselves

* Applying ESLint rules

* Manually replacing the stragglers
2021-02-03 18:12:39 -08:00

80 lines
2.1 KiB
TypeScript

/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { FC, useState, useMemo, useCallback } from 'react';
import {
OriginSaveModalProps,
SavedObjectSaveModalOrigin,
OnSaveProps,
SaveModalState,
} from '../../../../../src/plugins/saved_objects/public';
import { SavedObjectTaggingPluginStart } from '../../../saved_objects_tagging/public';
export type OriginSaveProps = OnSaveProps & { returnToOrigin: boolean; newTags?: string[] };
export type TagEnhancedSavedObjectSaveModalOriginProps = Omit<OriginSaveModalProps, 'onSave'> & {
initialTags: string[];
savedObjectsTagging?: SavedObjectTaggingPluginStart;
onSave: (props: OriginSaveProps) => void;
};
export const TagEnhancedSavedObjectSaveModalOrigin: FC<TagEnhancedSavedObjectSaveModalOriginProps> = ({
initialTags,
onSave,
savedObjectsTagging,
options,
...otherProps
}) => {
const [selectedTags, setSelectedTags] = useState(initialTags);
const tagSelectorOption = useMemo(
() =>
savedObjectsTagging ? (
<savedObjectsTagging.ui.components.SavedObjectSaveModalTagSelector
initialSelection={initialTags}
onTagsSelected={setSelectedTags}
/>
) : undefined,
[savedObjectsTagging, initialTags]
);
const tagEnhancedOptions =
typeof options === 'function' ? (
(state: SaveModalState) => {
return (
<>
{tagSelectorOption}
{options(state)}
</>
);
}
) : (
<>
{tagSelectorOption}
{options}
</>
);
const tagEnhancedOnSave: OriginSaveModalProps['onSave'] = useCallback(
(saveOptions) => {
onSave({
...saveOptions,
newTags: selectedTags,
});
},
[onSave, selectedTags]
);
return (
<SavedObjectSaveModalOrigin
{...otherProps}
onSave={tagEnhancedOnSave}
options={tagEnhancedOptions}
/>
);
};