|
| 1 | +import React, { useContext } from 'react'; |
| 2 | +import FormIdContext from "../context/FormIdContext"; |
| 3 | +import { connect } from "frontity"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Input Component. |
| 7 | + * |
| 8 | + * @param {Object} state Frontity State. |
| 9 | + * @param {Object} actions Actions methods. |
| 10 | + * @param {Object} inputProps Input props. |
| 11 | + * @return {*} |
| 12 | + */ |
| 13 | +const Input = ( { state, actions, inputProps } ) => { |
| 14 | + |
| 15 | + // Context is used so that we can pass the form id to different components. |
| 16 | + const id = useContext( FormIdContext ); |
| 17 | + const inputName = inputProps.name; |
| 18 | + const placeholder = inputProps.placeholder; |
| 19 | + |
| 20 | + if ( 'undefined' === typeof ( state.gf.forms[ id ].inputVals[ inputName ] ) ) { |
| 21 | + actions.gf.changeInputValue( { id, inputName, value: inputProps.value } ); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * OnChange handler for input. |
| 26 | + * |
| 27 | + * @param {Object} event Event. |
| 28 | + * |
| 29 | + * @return {void} |
| 30 | + */ |
| 31 | + const onChange = ( event ) => { |
| 32 | + |
| 33 | + actions.gf.changeInputValue( { id, inputName, value: event.target.value } ); |
| 34 | + |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <input |
| 39 | + name={ inputProps.name } |
| 40 | + className={ inputProps.className } |
| 41 | + id={ inputProps.id } |
| 42 | + aria-invalid={ inputProps.ariaInvalid } |
| 43 | + aria-required={ inputProps.ariaRequired } |
| 44 | + size={ inputProps.size } |
| 45 | + type={ inputProps.type } |
| 46 | + value={ state.gf.forms[ id ].inputVals[ inputName ] } |
| 47 | + onChange={ onChange } |
| 48 | + placeholder={ placeholder } |
| 49 | + /> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +export default connect( Input ); |
0 commit comments