// External imports import * as React from "react" import { advancedDataConstructor, Varchar, DateTime, StupidDate, Float, Integer, Boolean, cryptoHelper } from "fawkes-server/build/support" // Internal imports import * as ce from "../../../../helpers/componentEnhancer" import Icon from "../../icon" export interface ParentProps { changeField: (newValue: CompatibleData) => void value: CompatibleData options: Array<{ value: string; label: string }> includeEmptyOption: boolean label?: string placeholder?: string infoIcon?: string isRequired?: boolean } interface StateProps {} interface DispatchProps {} interface LocalState {} export type CompatibleData = | Varchar | DateTime | StupidDate | Float | Integer | Boolean class AdvancedFormSelect extends React.Component< ParentProps & StateProps & DispatchProps & ce.EnhancedPropsPrivate, LocalState > { controlClasses() { const classes = ["control"] if (this.props.infoIcon) classes.push("has-icons-left") return classes.join(" ") } infoIcon() { if (!this.props.infoIcon) return null return ( ) } optionsIncludingEmpty(): Array<{ value: string; label: string }> { let result: Array<{ value: string; label: string }> = [] if (this.props.includeEmptyOption) result.push({ value: "", label: "" }) result = [...result, ...this.props.options] return result } currentValueUnavailable(): boolean { return !this.optionsIncludingEmpty().find( option => option.value === this.props.value.toSelect() ) } resetValueIfUnavailable() { if (this.currentValueUnavailable()) { this.processChange(this.optionsIncludingEmpty()[0].value) } } processChange(value: string) { this.props.changeField(advancedDataConstructor( this.props.value.type ).fromSelect(value, this.props.value.options) as CompatibleData) } componentDidUpdate(prevProps, prevState) { this.resetValueIfUnavailable() } render() { const selectId = cryptoHelper.createGuid() return (
{this.props.label ? ( ) : null}
{this.infoIcon()}
) } } const stateMappings: ce.StateMappings = (s, props) => ({}) const dispatchMappings: ce.DispatchMappings = (d, props) => ({}) export default ((): React.ComponentType => ce.enhance(AdvancedFormSelect, { stateMappings, dispatchMappings }))()