import FormattedInput from 'cleave.js/react' import { RadioLabel } from 'Components/conversation/Question' import ToggleSwitch from 'Components/ui/ToggleSwitch' import { binaryOptionChoices, buildVariantTree } from 'Engine/getInputComponent' import React, { useCallback, useState } from 'react' import NumberFormat from 'react-number-format' import { useSelector } from 'react-redux' import { RootState } from 'Reducers/rootReducer' import { flatRulesSelector, ruleAnalysisSelector, situationSelector } from 'Selectors/analyseSelectors' import { DottedName } from 'Types/rule' import { debounce } from '../utils' import { dateRegexp } from './date' type FieldProps = { dottedName: DottedName useSwitch?: boolean onChange: (newValue: any) => void } export function Field({ dottedName, useSwitch = false, onChange }: FieldProps) { const situation = useSelector(situationSelector) const [value, setValue] = useState(situation[dottedName]) const onChangeWithDebounce = useCallback(debounce(600, onChange), []) const analysis = useSelector((state: RootState) => { return ruleAnalysisSelector(state, { dottedName }) }) return analysis.unit !== undefined ? ( { setValue(floatValue) onChangeWithDebounce(floatValue || 0) }} value={value} autoComplete="off" className="targetInput" css={` /* color: white; */ /* border-color: white; */ /* padding: 10px; */ `} /> ) : analysis.type === 'date' ? ( { if (value.match(dateRegexp)) { onChange(value) } }} value={value} autoComplete="off" /> ) : analysis.formule?.explanation?.['une possibilité'] === 'oui' ? ( ) : useSwitch ? ( onChange(evt.currentTarget.checked)} /> ) : ( ) } function BinaryOption({ onChange, value: currentValue }) { return ( <> {binaryOptionChoices.map(({ label, value }) => ( ))} ) } function UnionOption({ dottedName, onChange }) { const flatRules = useSelector(flatRulesSelector) const variants = buildVariantTree(flatRules, dottedName).children return ( ) }