Skip to content

Commit 5178374

Browse files
Daniilkibanamachine
Daniil
andauthored
[Input Control] Custom renderer (elastic#84423) (elastic#84730)
* Create custom renderer * Reduce initial bundle size * Fix tests * Add unit test * Remove injectI18n usage Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Kibana Machine <[email protected]>
1 parent d61e53d commit 5178374

28 files changed

+663
-436
lines changed

src/plugins/input_control_vis/public/__snapshots__/input_control_fn.test.ts.snap

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/input_control_vis/public/__snapshots__/to_ast.test.ts.snap

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/input_control_vis/public/components/editor/_index.scss

-1
This file was deleted.

src/plugins/input_control_vis/public/components/editor/control_editor.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import { getTitle, ControlParams, CONTROL_TYPES, ControlParamsOptions } from '..
3636
import { IIndexPattern } from '../../../../data/public';
3737
import { InputControlVisDependencies } from '../../plugin';
3838

39+
import './control_editor.scss';
40+
3941
interface ControlEditorUiProps {
4042
controlIndex: number;
4143
controlParams: ControlParams;

src/plugins/input_control_vis/public/components/editor/controls_tab.test.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import React from 'react';
2121
import { shallowWithIntl, mountWithIntl } from '@kbn/test/jest';
2222
import { findTestSubject } from '@elastic/eui/lib/test';
2323
import { getDepsMock, getIndexPatternMock } from '../../test_utils';
24-
import { ControlsTab, ControlsTabUiProps } from './controls_tab';
24+
import ControlsTab, { ControlsTabProps } from './controls_tab';
2525
import { Vis } from '../../../../visualizations/public';
2626

2727
const indexPatternsMock = {
2828
get: getIndexPatternMock,
2929
};
30-
let props: ControlsTabUiProps;
30+
let props: ControlsTabProps;
3131

3232
beforeEach(() => {
33-
props = {
33+
props = ({
3434
deps: getDepsMock(),
3535
vis: ({
3636
API: {
@@ -78,18 +78,18 @@ beforeEach(() => {
7878
},
7979
setValue: jest.fn(),
8080
intl: null as any,
81-
};
81+
} as unknown) as ControlsTabProps;
8282
});
8383

8484
test('renders ControlsTab', () => {
85-
const component = shallowWithIntl(<ControlsTab.WrappedComponent {...props} />);
85+
const component = shallowWithIntl(<ControlsTab {...props} />);
8686

8787
expect(component).toMatchSnapshot();
8888
});
8989

9090
describe('behavior', () => {
9191
test('add control button', () => {
92-
const component = mountWithIntl(<ControlsTab.WrappedComponent {...props} />);
92+
const component = mountWithIntl(<ControlsTab {...props} />);
9393

9494
findTestSubject(component, 'inputControlEditorAddBtn').simulate('click');
9595

@@ -102,7 +102,7 @@ describe('behavior', () => {
102102
});
103103

104104
test('remove control button', () => {
105-
const component = mountWithIntl(<ControlsTab.WrappedComponent {...props} />);
105+
const component = mountWithIntl(<ControlsTab {...props} />);
106106
findTestSubject(component, 'inputControlEditorRemoveControl0').simulate('click');
107107
const expectedParams = [
108108
'controls',
@@ -125,7 +125,7 @@ describe('behavior', () => {
125125
});
126126

127127
test('move down control button', () => {
128-
const component = mountWithIntl(<ControlsTab.WrappedComponent {...props} />);
128+
const component = mountWithIntl(<ControlsTab {...props} />);
129129
findTestSubject(component, 'inputControlEditorMoveDownControl0').simulate('click');
130130
const expectedParams = [
131131
'controls',
@@ -162,7 +162,7 @@ describe('behavior', () => {
162162
});
163163

164164
test('move up control button', () => {
165-
const component = mountWithIntl(<ControlsTab.WrappedComponent {...props} />);
165+
const component = mountWithIntl(<ControlsTab {...props} />);
166166
findTestSubject(component, 'inputControlEditorMoveUpControl1').simulate('click');
167167
const expectedParams = [
168168
'controls',

src/plugins/input_control_vis/public/components/editor/controls_tab.tsx

+34-34
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*/
1919

2020
import React, { PureComponent } from 'react';
21-
import { injectI18n, FormattedMessage, InjectedIntlProps } from '@kbn/i18n/react';
21+
import { FormattedMessage } from '@kbn/i18n/react';
22+
import { i18n } from '@kbn/i18n';
2223

2324
import {
2425
EuiButton,
@@ -44,22 +45,17 @@ import {
4445
} from '../../editor_utils';
4546
import { getLineageMap, getParentCandidates } from '../../lineage';
4647
import { InputControlVisDependencies } from '../../plugin';
48+
import { InputControlVisParams } from '../../types';
4749

4850
interface ControlsTabUiState {
4951
type: CONTROL_TYPES;
5052
}
5153

52-
interface ControlsTabUiParams {
53-
controls: ControlParams[];
54-
}
55-
type ControlsTabUiInjectedProps = InjectedIntlProps &
56-
Pick<VisOptionsProps<ControlsTabUiParams>, 'vis' | 'stateParams' | 'setValue'> & {
57-
deps: InputControlVisDependencies;
58-
};
54+
export type ControlsTabProps = VisOptionsProps<InputControlVisParams> & {
55+
deps: InputControlVisDependencies;
56+
};
5957

60-
export type ControlsTabUiProps = ControlsTabUiInjectedProps;
61-
62-
class ControlsTabUi extends PureComponent<ControlsTabUiProps, ControlsTabUiState> {
58+
class ControlsTab extends PureComponent<ControlsTabProps, ControlsTabUiState> {
6359
state = {
6460
type: CONTROL_TYPES.LIST,
6561
};
@@ -161,8 +157,6 @@ class ControlsTabUi extends PureComponent<ControlsTabUiProps, ControlsTabUiState
161157
}
162158

163159
render() {
164-
const { intl } = this.props;
165-
166160
return (
167161
<div>
168162
{this.renderControls()}
@@ -176,25 +170,31 @@ class ControlsTabUi extends PureComponent<ControlsTabUiProps, ControlsTabUiState
176170
options={[
177171
{
178172
value: CONTROL_TYPES.RANGE,
179-
text: intl.formatMessage({
180-
id: 'inputControl.editor.controlsTab.select.rangeDropDownOptionLabel',
181-
defaultMessage: 'Range slider',
182-
}),
173+
text: i18n.translate(
174+
'inputControl.editor.controlsTab.select.rangeDropDownOptionLabel',
175+
{
176+
defaultMessage: 'Range slider',
177+
}
178+
),
183179
},
184180
{
185181
value: CONTROL_TYPES.LIST,
186-
text: intl.formatMessage({
187-
id: 'inputControl.editor.controlsTab.select.listDropDownOptionLabel',
188-
defaultMessage: 'Options list',
189-
}),
182+
text: i18n.translate(
183+
'inputControl.editor.controlsTab.select.listDropDownOptionLabel',
184+
{
185+
defaultMessage: 'Options list',
186+
}
187+
),
190188
},
191189
]}
192190
value={this.state.type}
193191
onChange={(event) => this.setState({ type: event.target.value as CONTROL_TYPES })}
194-
aria-label={intl.formatMessage({
195-
id: 'inputControl.editor.controlsTab.select.controlTypeAriaLabel',
196-
defaultMessage: 'Select control type',
197-
})}
192+
aria-label={i18n.translate(
193+
'inputControl.editor.controlsTab.select.controlTypeAriaLabel',
194+
{
195+
defaultMessage: 'Select control type',
196+
}
197+
)}
198198
/>
199199
</EuiFormRow>
200200
</EuiFlexItem>
@@ -205,10 +205,12 @@ class ControlsTabUi extends PureComponent<ControlsTabUiProps, ControlsTabUiState
205205
onClick={this.handleAddControl}
206206
iconType="plusInCircle"
207207
data-test-subj="inputControlEditorAddBtn"
208-
aria-label={intl.formatMessage({
209-
id: 'inputControl.editor.controlsTab.select.addControlAriaLabel',
210-
defaultMessage: 'Add control',
211-
})}
208+
aria-label={i18n.translate(
209+
'inputControl.editor.controlsTab.select.addControlAriaLabel',
210+
{
211+
defaultMessage: 'Add control',
212+
}
213+
)}
212214
>
213215
<FormattedMessage
214216
id="inputControl.editor.controlsTab.addButtonLabel"
@@ -224,8 +226,6 @@ class ControlsTabUi extends PureComponent<ControlsTabUiProps, ControlsTabUiState
224226
}
225227
}
226228

227-
export const ControlsTab = injectI18n(ControlsTabUi);
228-
229-
export const getControlsTab = (deps: InputControlVisDependencies) => (
230-
props: Omit<ControlsTabUiProps, 'core'>
231-
) => <ControlsTab {...props} deps={deps} />;
229+
// default export required for React.Lazy
230+
// eslint-disable-next-line import/no-default-export
231+
export { ControlsTab as default };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import React, { lazy } from 'react';
21+
import { VisOptionsProps } from 'src/plugins/vis_default_editor/public';
22+
import { InputControlVisDependencies } from '../../plugin';
23+
import { InputControlVisParams } from '../../types';
24+
25+
const ControlsTab = lazy(() => import('./controls_tab'));
26+
const OptionsTab = lazy(() => import('./options_tab'));
27+
28+
export const getControlsTab = (deps: InputControlVisDependencies) => (
29+
props: VisOptionsProps<InputControlVisParams>
30+
) => <ControlsTab {...props} deps={deps} />;
31+
32+
export const OptionsTabLazy = (props: VisOptionsProps<InputControlVisParams>) => (
33+
<OptionsTab {...props} />
34+
);

src/plugins/input_control_vis/public/components/editor/options_tab.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ import { shallow } from 'enzyme';
2222
import { mountWithIntl } from '@kbn/test/jest';
2323

2424
import { Vis } from '../../../../visualizations/public';
25-
import { OptionsTab, OptionsTabProps } from './options_tab';
25+
import OptionsTab, { OptionsTabProps } from './options_tab';
2626

2727
describe('OptionsTab', () => {
2828
let props: OptionsTabProps;
2929

3030
beforeEach(() => {
31-
props = {
31+
props = ({
3232
vis: {} as Vis,
3333
stateParams: {
3434
updateFiltersOnChange: false,
3535
useTimeFilter: false,
3636
pinFilters: false,
3737
},
3838
setValue: jest.fn(),
39-
};
39+
} as unknown) as OptionsTabProps;
4040
});
4141

4242
it('should renders OptionsTab', () => {

src/plugins/input_control_vis/public/components/editor/options_tab.tsx

+6-12
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,11 @@ import { FormattedMessage } from '@kbn/i18n/react';
2424
import { EuiSwitchEvent } from '@elastic/eui';
2525

2626
import { VisOptionsProps } from 'src/plugins/vis_default_editor/public';
27+
import { InputControlVisParams } from '../../types';
2728

28-
interface OptionsTabParams {
29-
updateFiltersOnChange: boolean;
30-
useTimeFilter: boolean;
31-
pinFilters: boolean;
32-
}
33-
type OptionsTabInjectedProps = Pick<
34-
VisOptionsProps<OptionsTabParams>,
35-
'vis' | 'setValue' | 'stateParams'
36-
>;
37-
38-
export type OptionsTabProps = OptionsTabInjectedProps;
29+
export type OptionsTabProps = VisOptionsProps<InputControlVisParams>;
3930

40-
export class OptionsTab extends PureComponent<OptionsTabProps> {
31+
class OptionsTab extends PureComponent<OptionsTabProps> {
4132
handleUpdateFiltersChange = (event: EuiSwitchEvent) => {
4233
this.props.setValue('updateFiltersOnChange', event.target.checked);
4334
};
@@ -98,3 +89,6 @@ export class OptionsTab extends PureComponent<OptionsTabProps> {
9889
);
9990
}
10091
}
92+
// default export required for React.Lazy
93+
// eslint-disable-next-line import/no-default-export
94+
export { OptionsTab as default };

0 commit comments

Comments
 (0)