import React from 'react'; import PropTypes from 'prop-types'; import { FormGroup, FormSelect, FormSelectOption, DatePicker, TimePicker, } from '@patternfly/react-core'; import { ExclamationCircleIcon } from '@patternfly/react-icons'; const wrapFieldProps = fieldProps => { const { onChange } = fieldProps; // modify onChange args to correctly wire formik with pf4 input handlers const wrappedOnChange = (value, event) => { onChange(event); }; return { ...fieldProps, onChange: wrappedOnChange }; }; const wrapPickerProps = fieldProps => { const { onChange } = fieldProps; // because pf4 does not provide consistent handlers for its inputs const wrappedOnChange = value => { onChange({ target: { name: fieldProps.name, value } }); }; return { ...fieldProps, onChange: wrappedOnChange }; }; const shouldValidate = (form, fieldName) => { if (form.touched[fieldName]) { return form.errors[fieldName] ? 'error' : 'success'; } return 'noval'; }; export const SelectField = ({ selectItems, field, form, label, isRequired, blankLabel, }) => { const fieldProps = wrapFieldProps(field); const valid = shouldValidate(form, field.name); return ( } validated={valid} > {selectItems.map((item, idx) => ( ))} ); }; SelectField.propTypes = { selectItems: PropTypes.array, field: PropTypes.object.isRequired, form: PropTypes.object.isRequired, label: PropTypes.string.isRequired, blankLabel: PropTypes.string, isRequired: PropTypes.bool, }; SelectField.defaultProps = { selectItems: [], isRequired: false, blankLabel: '', }; const pickerWithHandlers = ComponentType => { const Subcomponent = ({ form, field, isRequired, label, ...rest }) => { const { onChange, onBlur } = wrapPickerProps(field); const valid = shouldValidate(form, field.name); const Component = ComponentType === 'date' ? DatePicker : TimePicker; return ( } validated={valid} > { // datepicker: onChange (event: React.FormEvent, value: string, date?: Date) => void // timepicker: onChange (time: string, hour?: number, minute?: number, seconds?: number, isValid?: boolean ) => void ComponentType === 'date' ? onChange(b, a) : onChange(a); }} onBlur={onBlur} {...rest} validated={valid} isDisabled={form.isSubmitting} /> ); }; Subcomponent.propTypes = { form: PropTypes.object.isRequired, field: PropTypes.object.isRequired, isRequired: PropTypes.bool, label: PropTypes.string.isRequired, }; Subcomponent.defaultProps = { isRequired: false, }; return Subcomponent; }; export const DatePickerField = pickerWithHandlers('date'); export const TimePickerField = pickerWithHandlers('time');