diff --git a/x-pack/plugins/ml/public/components/form_label/__snapshots__/form_label.test.js.snap b/x-pack/plugins/ml/public/components/form_label/__snapshots__/form_label.test.js.snap new file mode 100644 index 000000000000..057ed54b10a0 --- /dev/null +++ b/x-pack/plugins/ml/public/components/form_label/__snapshots__/form_label.test.js.snap @@ -0,0 +1,28 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`FormLabel Basic initialization 1`] = ` + + +`; + +exports[`FormLabel Full initialization 1`] = ` + + + + +`; diff --git a/x-pack/plugins/ml/public/components/form_label/form_label.js b/x-pack/plugins/ml/public/components/form_label/form_label.js index f549259b4e68..efdbf6b39265 100644 --- a/x-pack/plugins/ml/public/components/form_label/form_label.js +++ b/x-pack/plugins/ml/public/components/form_label/form_label.js @@ -4,34 +4,46 @@ * you may not use this file except in compliance with the Elastic License. */ +import './styles/main.less'; +import PropTypes from 'prop-types'; +import React, { Component } from 'react'; -import { uiModules } from 'ui/modules'; -const module = uiModules.get('apps/ml'); +import { JsonTooltip } from '../json_tooltip/json_tooltip'; -// directive for creating a form label including a hoverable icon -// to provide additional information in a tooltip. label and tooltip +// Component for creating a form label including a hoverable icon +// to provide additional information in a tooltip. Label and tooltip // text elements get unique ids based on label-id so they can be // referenced by attributes, for example: // -// Label Text +// Label Text // -module.directive('mlFormLabel', function () { - return { - scope: { - labelId: '@', - tooltipAppendToBody: '@' - }, - restrict: 'E', - replace: false, - transclude: true, - template: ` - - - ` - }; -}); +// +// Writing this as a class based component because stateless components +// cannot use ref(). Once angular is completely gone this can be rewritten +// as a function stateless component. +export class FormLabel extends Component { + constructor(props) { + super(props); + this.labelRef = React.createRef(); + } + render() { + // labelClassName is used so we can override the class with 'kuiFormLabel' + // when used in an angular context. Once the component is no longer used from + // within angular, this prop can be removed and the className can be hardcoded. + const { labelId, labelClassName = 'euiFormLabel', children } = this.props; + return ( + + + + + ); + } +} +FormLabel.propTypes = { + labelId: PropTypes.string +}; diff --git a/x-pack/plugins/ml/public/components/form_label/form_label.test.js b/x-pack/plugins/ml/public/components/form_label/form_label.test.js new file mode 100644 index 000000000000..70b6983209c9 --- /dev/null +++ b/x-pack/plugins/ml/public/components/form_label/form_label.test.js @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { shallow } from 'enzyme'; +import React from 'react'; + +import { FormLabel } from './form_label'; + +describe('FormLabel', () => { + + test('Basic initialization', () => { + const wrapper = shallow(); + const props = wrapper.props(); + expect(props.labelId).toBeUndefined(); + expect(wrapper.find('label').text()).toBe(''); + expect(wrapper).toMatchSnapshot(); + }); + + test('Full initialization', () => { + const labelId = 'uid'; + const labelText = 'Label Text'; + const wrapper = shallow({labelText}); + + const labelElement = wrapper.find('label'); + expect(labelElement.props().id).toBe(`ml_aria_label_${labelId}`); + expect(labelElement.text()).toBe(labelText); + expect(wrapper).toMatchSnapshot(); + }); +}); diff --git a/x-pack/plugins/ml/public/components/form_label/form_label_directive.js b/x-pack/plugins/ml/public/components/form_label/form_label_directive.js new file mode 100644 index 000000000000..85e004d4759a --- /dev/null +++ b/x-pack/plugins/ml/public/components/form_label/form_label_directive.js @@ -0,0 +1,52 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + + + +import React from 'react'; +import ReactDOM from 'react-dom'; + +import angular from 'angular'; +import { uiModules } from 'ui/modules'; +const module = uiModules.get('apps/ml', ['react']); + +import { FormLabel } from './form_label'; + +// directive for creating a form label including a hoverable icon +// to provide additional information in a tooltip. label and tooltip +// text elements get unique ids based on label-id so they can be +// referenced by attributes, for example: +// +// Label Text +// +module.directive('mlFormLabel', function () { + return { + scope: { + labelId: '@' + }, + restrict: 'E', + replace: false, + transclude: true, + link: (scope, element, attrs, ctrl, transclude) => { + const props = { + labelId: scope.labelId, + labelClassName: 'kuiFormLabel', + // transclude the label text/elements from the angular template + // to the labelRef from the react component. + ref: c => angular.element(c.labelRef.current).append(transclude()) + }; + + ReactDOM.render( + React.createElement(FormLabel, props), + element[0] + ); + } + }; +}); diff --git a/x-pack/plugins/ml/public/components/form_label/index.js b/x-pack/plugins/ml/public/components/form_label/index.js index 38f165d16c17..f850e7b7a2fd 100644 --- a/x-pack/plugins/ml/public/components/form_label/index.js +++ b/x-pack/plugins/ml/public/components/form_label/index.js @@ -6,5 +6,5 @@ -import './form_label'; +import './form_label_directive'; import './styles/main.less'; diff --git a/x-pack/plugins/ml/public/components/json_tooltip/__snapshots__/json_tooltip.test.js.snap b/x-pack/plugins/ml/public/components/json_tooltip/__snapshots__/json_tooltip.test.js.snap new file mode 100644 index 000000000000..03270b82e927 --- /dev/null +++ b/x-pack/plugins/ml/public/components/json_tooltip/__snapshots__/json_tooltip.test.js.snap @@ -0,0 +1,54 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`JsonTooltip Initialization with a non-existing tooltip attribute doesn't throw an error 1`] = ` +