Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EuiSelect: converted to Typescript #2694

Merged
merged 5 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added `nested` glyph to `EuiIcon` ([#2707](https://github.com/elastic/eui/pull/2707))
- Added `tableLayout` prop to `EuiTable`, `EuiBasicTable` and `EuiInMemoryTable` to provide the option of auto layout ([#2697](https://github.com/elastic/eui/pull/2697))
- Converted `EuiErrorBoundary` to Typescript ([#2690](https://github.com/elastic/eui/pull/2690))
- Converted `EuiSelect` to Typescript ([#2694](https://github.com/elastic/eui/pull/2694))

**Bug fixes**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,6 @@ exports[`EuiSelect props options are rendered 1`] = `
</eui-form-control-layout>
`;

exports[`EuiSelect props readOnly is rendered 1`] = `
<eui-form-control-layout
compressed="false"
fullwidth="false"
icon="[object Object]"
isloading="false"
readonly="true"
>
<eui-validatable-control>
<select
class="euiSelect"
readonly=""
/>
</eui-validatable-control>
</eui-form-control-layout>
`;

exports[`EuiSelect props value option is rendered 1`] = `
<eui-form-control-layout
compressed="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ describe('EuiSelect', () => {
expect(component).toMatchSnapshot();
});

test('readOnly is rendered', () => {
const component = render(<EuiSelect readOnly />);

expect(component).toMatchSnapshot();
});

test('disabled options are rendered', () => {
const component = render(
<EuiSelect
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
import React from 'react';
import PropTypes from 'prop-types';
import React, { SelectHTMLAttributes, Ref, FunctionComponent } from 'react';
import { CommonProps } from '../../common';
import classNames from 'classnames';

import { EuiFormControlLayout } from '../form_control_layout';

import { EuiValidatableControl } from '../validatable_control';
import { EuiFormControlLayoutIconsProps } from '../form_control_layout/form_control_layout_icons';

interface Option {
ffknob marked this conversation as resolved.
Show resolved Hide resolved
value?: string;
text?: string;
ffknob marked this conversation as resolved.
Show resolved Hide resolved
disabled?: boolean;
}

export type EuiSelectProps = SelectHTMLAttributes<HTMLSelectElement> &
CommonProps & {
options?: Option[];
isInvalid?: boolean;
fullWidth?: boolean;
isLoading?: boolean;

export const EuiSelect = ({
/**
* Simulates no selection by creating an empty, selected, hidden first option
*/
hasNoInitialSelection?: boolean;
inputRef?: Ref<HTMLSelectElement>;

/**
* when `true` creates a shorter height input
*/
compressed?: boolean;

/**
* Creates an input group with element(s) coming before select
*/
prepend?: JSX.Element | JSX.Element[];
ffknob marked this conversation as resolved.
Show resolved Hide resolved
/**
* Creates an input group with element(s) coming after select
*/
append?: JSX.Element | JSX.Element[];
};

export const EuiSelect: FunctionComponent<EuiSelectProps> = ({
className,
options,
options = [],
id,
name,
inputRef,
isInvalid,
fullWidth,
isLoading,
hasNoInitialSelection,
fullWidth = false,
isLoading = false,
hasNoInitialSelection = false,
defaultValue,
compressed,
compressed = false,
value,
prepend,
append,
onMouseUp,
readOnly,
...rest
}) => {
const handleMouseUp = e => {
const handleMouseUp = (e: any) => {
ffknob marked this conversation as resolved.
Show resolved Hide resolved
// Normalizes cross-browser mouse eventing by preventing propagation,
// notably for use in conjunction with EuiOutsideClickDetector.
// See https://github.com/elastic/eui/pull/1926 for full discussion on
Expand Down Expand Up @@ -61,7 +93,7 @@ export const EuiSelect = ({
selectDefaultValue = defaultValue || '';
}

const icon = {
const icon: EuiFormControlLayoutIconsProps['icon'] = {
type: 'arrowDown',
side: 'right',
};
Expand All @@ -72,7 +104,6 @@ export const EuiSelect = ({
fullWidth={fullWidth}
isLoading={isLoading}
compressed={compressed}
readOnly={readOnly}
prepend={prepend}
append={append}
inputId={id}>
Expand All @@ -84,7 +115,6 @@ export const EuiSelect = ({
ref={inputRef}
defaultValue={selectDefaultValue}
value={value}
readOnly={readOnly}
onMouseUp={handleMouseUp}
{...rest}>
{emptyOptionNode}
Expand All @@ -101,49 +131,3 @@ export const EuiSelect = ({
</EuiFormControlLayout>
);
};

EuiSelect.propTypes = {
name: PropTypes.string,
id: PropTypes.string,
options: PropTypes.arrayOf(
PropTypes.shape({
text: PropTypes.node.isRequired,
})
).isRequired,
isInvalid: PropTypes.bool,
fullWidth: PropTypes.bool,
isLoading: PropTypes.bool,

/**
* Simulates no selection by creating an empty, selected, hidden first option
*/
hasNoInitialSelection: PropTypes.bool,
inputRef: PropTypes.func,
/**
* when `true` creates a shorter height input
*/
compressed: PropTypes.bool,
readOnly: PropTypes.bool,
/**
* Creates an input group with element(s) coming before select
*/
prepend: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
/**
* Creates an input group with element(s) coming after select
*/
append: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
};

EuiSelect.defaultProps = {
options: [],
fullWidth: false,
isLoading: false,
hasNoInitialSelection: false,
compressed: false,
};