Debian Codegraph @

node/@patternfly/react-core/src/components/FormSelect/FormSelect.tsx

raw
1 import { Children, Component } from 'react';
2 import styles from '@patternfly/react-styles/css/components/FormControl/form-control';
3 import { css } from '@patternfly/react-styles';
4 import { PickOptional } from '../../helpers/typeUtils';
5 import { ValidatedOptions } from '../../helpers/constants';
6 import { FormControlIcon } from '../FormControl/FormControlIcon';
7 import { getOUIAProps, OUIAProps } from '../../helpers';
8 import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds';
9 import RhMicronsCaretDownFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-fill-icon';
10
11 export interface FormSelectProps
12 extends Omit<React.HTMLProps<HTMLSelectElement>, 'onChange' | 'onBlur' | 'onFocus' | 'disabled'>, OUIAProps {
13 /** content rendered inside the FormSelect */
14 children: React.ReactNode;
15 /** additional classes added to the FormSelect control */
16 className?: string;
17 /** value of selected option */
18 value?: any;
19 /** Value to indicate if the select is modified to show that validation state.
20 * If set to success, select will be modified to indicate valid state.
21 * If set to error, select will be modified to indicate error state.
22 */
23 validated?: 'success' | 'warning' | 'error' | 'default';
24 /** Flag indicating the FormSelect is disabled */
25 isDisabled?: boolean;
26 /** Sets the FormSelect required. */
27 isRequired?: boolean;
28 /** Optional callback for updating when selection loses focus */
29 onBlur?: (event: React.FormEvent<HTMLSelectElement>) => void;
30 /** Optional callback for updating when selection gets focus */
31 onFocus?: (event: React.FormEvent<HTMLSelectElement>) => void;
32 /** Optional callback for updating when selection changes */
33 onChange?: (event: React.FormEvent<HTMLSelectElement>, value: string) => void;
34 /** Custom flag to show that the FormSelect requires an associated id or aria-label. */
35 'aria-label'?: string;
36 /** Value to overwrite the randomly generated data-ouia-component-id.*/
37 ouiaId?: number | string;
38 /** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
39 ouiaSafe?: boolean;
40 }
41
42 class FormSelect extends Component<FormSelectProps> {
43 static displayName = 'FormSelect';
44 constructor(props: FormSelectProps) {
45 super(props);
46 if (!props.id && !props['aria-label']) {
47 // eslint-disable-next-line no-console
48 console.error('FormSelect requires either an id or aria-label to be specified');
49 }
50 }
51
52 static defaultProps: PickOptional<FormSelectProps> = {
53 className: '',
54 value: '',
55 validated: 'default',
56 isDisabled: false,
57 isRequired: false,
58 onBlur: (): any => undefined,
59 onFocus: (): any => undefined,
60 onChange: (): any => undefined,
61 ouiaSafe: true
62 };
63
64 handleChange = (event: any) => {
65 this.props.onChange(event, event.currentTarget.value);
66 };
67
68 render() {
69 const { children, className, value, validated, isDisabled, isRequired, ouiaId, ouiaSafe, ...props } = this.props;
70 /* find selected option and get placeholder flag */
71 const selectedOption = Children.toArray(children).find((option: any) => option.props.value === value) as any;
72 const isSelectedPlaceholder = selectedOption && selectedOption.props.isPlaceholder;
73 const hasStatusIcon = ['success', 'error', 'warning'].includes(validated);
74
75 return (
76 <SSRSafeIds prefix="pf-" ouiaComponentType={FormSelect.displayName}>
77 {(_, generatedOuiaId) => (
78 <span
79 className={css(
80 styles.formControl,
81 isDisabled && styles.modifiers.disabled,
82 isSelectedPlaceholder && styles.modifiers.placeholder,
83 hasStatusIcon && styles.modifiers[validated as 'success' | 'warning' | 'error'],
84 className
85 )}
86 >
87 <select
88 {...props}
89 aria-invalid={validated === ValidatedOptions.error}
90 {...getOUIAProps(FormSelect.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)}
91 onChange={this.handleChange}
92 disabled={isDisabled}
93 required={isRequired}
94 value={value}
95 >
96 {children}
97 </select>
98 <span className={css(styles.formControlUtilities)}>
99 {hasStatusIcon && <FormControlIcon status={validated as 'success' | 'error' | 'warning'} />}
100 <span className={css(styles.formControlToggleIcon)}>
101 <RhMicronsCaretDownFillIcon />
102 </span>
103 </span>
104 </span>
105 )}
106 </SSRSafeIds>
107 );
108 }
109 }
110
111 export { FormSelect };