11import PropTypes from 'prop-types' ;
2- import React , { useContext } from 'react' ;
2+ import React , {
3+ useContext ,
4+ useState ,
5+ } from 'react' ;
36import { withGlobalProps } from '../../providers/globalProps' ;
47import { classNames } from '../../utils/classNames' ;
58import { transferProps } from '../../utils/transferProps' ;
9+ import { TranslationsContext } from '../../providers/translations' ;
10+ import { getRootSizeClassName } from '../_helpers/getRootSizeClassName' ;
611import { getRootValidationStateClassName } from '../_helpers/getRootValidationStateClassName' ;
712import { resolveContextOrProp } from '../_helpers/resolveContextOrProp' ;
13+ import { InputGroupContext } from '../InputGroup' ;
14+ import { Text } from '../Text' ;
815import { FormLayoutContext } from '../FormLayout' ;
916import styles from './FileInputField.module.scss' ;
1017
@@ -18,24 +25,39 @@ export const FileInputField = React.forwardRef((props, ref) => {
1825 label,
1926 layout,
2027 required,
28+ size,
2129 validationState,
2230 validationText,
2331 ...restProps
2432 } = props ;
2533
26- const context = useContext ( FormLayoutContext ) ;
34+ const formLayoutContext = useContext ( FormLayoutContext ) ;
35+ const inputGroupContext = useContext ( InputGroupContext ) ;
36+ const translations = useContext ( TranslationsContext ) ;
37+
38+ const [ selectedFileName , setSelectedFileName ] = useState ( '' ) ;
39+
40+ const handleFileChange = ( event ) => {
41+ const file = event . target . files [ 0 ] ;
42+ setSelectedFileName ( file . name ) ;
43+ } ;
2744
2845 return (
2946 < label
3047 className = { classNames (
3148 styles . root ,
3249 fullWidth && styles . isRootFullWidth ,
33- context && styles . isRootInFormLayout ,
34- resolveContextOrProp ( context && context . layout , layout ) === 'horizontal'
50+ formLayoutContext && styles . isRootInFormLayout ,
51+ resolveContextOrProp ( formLayoutContext && formLayoutContext . layout , layout ) === 'horizontal'
3552 ? styles . isRootLayoutHorizontal
3653 : styles . isRootLayoutVertical ,
37- disabled && styles . isRootDisabled ,
54+ resolveContextOrProp ( inputGroupContext && inputGroupContext . disabled , disabled ) && styles . isRootDisabled ,
55+ inputGroupContext && styles . isRootGrouped ,
3856 required && styles . isRootRequired ,
57+ getRootSizeClassName (
58+ resolveContextOrProp ( inputGroupContext && inputGroupContext . size , size ) ,
59+ styles ,
60+ ) ,
3961 getRootValidationStateClassName ( validationState , styles ) ,
4062 ) }
4163 htmlFor = { id }
@@ -44,7 +66,7 @@ export const FileInputField = React.forwardRef((props, ref) => {
4466 < div
4567 className = { classNames (
4668 styles . label ,
47- ! isLabelVisible && styles . isLabelHidden ,
69+ ( ! isLabelVisible || inputGroupContext ) && styles . isLabelHidden ,
4870 ) }
4971 id = { id && `${ id } __labelText` }
5072 >
@@ -54,12 +76,26 @@ export const FileInputField = React.forwardRef((props, ref) => {
5476 < div className = { styles . inputContainer } >
5577 < input
5678 { ...transferProps ( restProps ) }
57- disabled = { disabled }
79+ className = { styles . input }
80+ disabled = { resolveContextOrProp ( inputGroupContext && inputGroupContext . disabled , disabled ) }
5881 id = { id }
82+ onChange = { handleFileChange }
5983 ref = { ref }
6084 required = { required }
6185 type = "file"
6286 />
87+ < div className = { styles . dropZone } >
88+ { selectedFileName && (
89+ < Text lines = { 1 } > { selectedFileName } </ Text >
90+ ) }
91+ { ! selectedFileName && (
92+ < >
93+ { translations . FileInputField . drop }
94+ { ' ' }
95+ < span className = { styles . dropZoneLink } > { translations . FileInputField . browse } </ span >
96+ </ >
97+ ) }
98+ </ div >
6399 </ div >
64100 { helpText && (
65101 < div
@@ -90,6 +126,7 @@ FileInputField.defaultProps = {
90126 isLabelVisible : true ,
91127 layout : 'vertical' ,
92128 required : false ,
129+ size : 'medium' ,
93130 validationState : null ,
94131 validationText : null ,
95132} ;
@@ -138,6 +175,12 @@ FileInputField.propTypes = {
138175 * If `true`, the input will be required.
139176 */
140177 required : PropTypes . bool ,
178+ /**
179+ * Size of the field.
180+ *
181+ * Ignored if the component is rendered within `InputGroup` component as the value is inherited in such case.
182+ */
183+ size : PropTypes . oneOf ( [ 'small' , 'medium' , 'large' ] ) ,
141184 /**
142185 * Alter the field to provide feedback based on validation result.
143186 */
0 commit comments