|
| 1 | +import React, { useContext, useEffect, useState } from "react"; |
| 2 | +import { FieldContext } from "../contexts/field-context"; |
| 3 | + |
| 4 | +export interface FieldValues { |
| 5 | + defaultValue?: string; |
| 6 | + initialValue?: string; |
| 7 | + currentValue?: string; |
| 8 | +} |
| 9 | + |
| 10 | +export interface TextFieldProps extends FieldValues { |
| 11 | + name: string; |
| 12 | +} |
| 13 | + |
| 14 | +const useFieldValues = (values: FieldValues) => { |
| 15 | + console.info("useFieldValues"); |
| 16 | + const [defaultValue, setDefaultValue] = useState(values.defaultValue || ""); |
| 17 | + let [initialValue, setInitialValue] = useState(values.initialValue); |
| 18 | + if (initialValue == null) { |
| 19 | + initialValue = defaultValue; |
| 20 | + } |
| 21 | + |
| 22 | + const [currentValue, setCurrentValue] = useState(initialValue); |
| 23 | + |
| 24 | + return { currentValue, setCurrentValue }; |
| 25 | +}; |
| 26 | + |
| 27 | +export const TextField = (props: TextFieldProps) => { |
| 28 | + const { currentValue, setCurrentValue } = useFieldValues({ |
| 29 | + defaultValue: props.defaultValue, |
| 30 | + initialValue: props.initialValue, |
| 31 | + currentValue: props.currentValue, |
| 32 | + }); |
| 33 | + |
| 34 | + const onChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 35 | + // store.setCurrentValue(event.target.value); |
| 36 | + setCurrentValue(event.target.value); |
| 37 | + }; |
| 38 | + |
| 39 | + const onFocus = (event: React.FocusEvent<HTMLInputElement>) => { |
| 40 | + console.debug(event.target); |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <input |
| 45 | + type="text" |
| 46 | + value={currentValue} |
| 47 | + onChange={onChange} |
| 48 | + onFocus={onFocus} |
| 49 | + /> |
| 50 | + ); |
| 51 | +}; |
0 commit comments