UNPKG

2.39 kBJavaScriptView Raw
1import { __rest } from "tslib";
2import * as React from 'react';
3import styles from '@patternfly/react-styles/css/components/FormControl/form-control';
4import { css } from '@patternfly/react-styles';
5import { ValidatedOptions } from '../../helpers/constants';
6import { getOUIAProps, getDefaultOUIAId } from '../../helpers';
7export class FormSelect extends React.Component {
8 constructor(props) {
9 super(props);
10 this.handleChange = (event) => {
11 this.props.onChange(event.currentTarget.value, event);
12 };
13 if (!props.id && !props['aria-label']) {
14 // eslint-disable-next-line no-console
15 console.error('FormSelect requires either an id or aria-label to be specified');
16 }
17 this.state = {
18 ouiaStateId: getDefaultOUIAId(FormSelect.displayName, props.validated)
19 };
20 }
21 render() {
22 const _a = this.props, { children, className, value, validated, isDisabled, isRequired, isIconSprite, ouiaId, ouiaSafe } = _a, props = __rest(_a, ["children", "className", "value", "validated", "isDisabled", "isRequired", "isIconSprite", "ouiaId", "ouiaSafe"]);
23 /* find selected option and get placeholder flag */
24 const selectedOption = React.Children.toArray(children).find((option) => option.props.value === value);
25 const isSelectedPlaceholder = selectedOption && selectedOption.props.isPlaceholder;
26 return (React.createElement("select", Object.assign({}, props, { className: css(styles.formControl, isIconSprite && styles.modifiers.iconSprite, className, validated === ValidatedOptions.success && styles.modifiers.success, validated === ValidatedOptions.warning && styles.modifiers.warning, isSelectedPlaceholder && styles.modifiers.placeholder), "aria-invalid": validated === ValidatedOptions.error }, getOUIAProps(FormSelect.displayName, ouiaId !== undefined ? ouiaId : this.state.ouiaStateId, ouiaSafe), { onChange: this.handleChange, disabled: isDisabled, required: isRequired, value: value }), children));
27 }
28}
29FormSelect.displayName = 'FormSelect';
30FormSelect.defaultProps = {
31 className: '',
32 value: '',
33 validated: 'default',
34 isDisabled: false,
35 isRequired: false,
36 isIconSprite: false,
37 onBlur: () => undefined,
38 onFocus: () => undefined,
39 onChange: () => undefined,
40 ouiaSafe: true
41};
42//# sourceMappingURL=FormSelect.js.map
\No newline at end of file