File size: 2,080 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// @flow

import * as React from "react";
import cn from "classnames";

import type {
  FormEvents,
  FocusEvents,
  MouseEvents,
  PointerEvents,
} from "../../";

type Props = {|
  ...FormEvents,
  ...FocusEvents,
  ...MouseEvents,
  ...PointerEvents,
  +className?: string,
  +value?: string | number | boolean,
  +name?: string,
  +label?: string,
  +disabled?: boolean,
  +readOnly?: boolean,
  +accept?: string,
|};

type State = {| fileName: string |};

class FormFileInput extends React.Component<Props, State> {
  state = {
    fileName: "",
  };

  _handleOnChange = (event: SyntheticInputEvent<HTMLInputElement>): void => {
    this.setState({ fileName: event.target.files[0].name });
    if (this.props.onChange) {
      this.props.onChange(event);
    }
  };

  render(): React.Node {
    const {
      className,
      value,
      name,
      label: labelFromProps = "Choose file",
      disabled,
      readOnly,
      onClick,
      onMouseEnter,
      onMouseLeave,
      onPointerEnter,
      onPointerLeave,
      onFocus,
      onBlur,
      accept,
    } = this.props;

    const classes = cn("custom-file", className);
    const label = this.state.fileName || labelFromProps;
    return (
      <div className={classes}>
        <input
          type="file"
          className="custom-file-input"
          name={name}
          value={value}
          disabled={disabled}
          readOnly={readOnly}
          onChange={this._handleOnChange}
          onClick={onClick}
          onMouseEnter={onMouseEnter}
          onMouseLeave={onMouseLeave}
          onPointerEnter={onPointerEnter}
          onPointerLeave={onPointerLeave}
          onFocus={onFocus}
          onBlur={onBlur}
          accept={accept}
        />
        <label
          className="custom-file-label"
          style={{
            whiteSpace: "nowrap",
            display: "block",
            overflow: "hidden",
          }}
        >
          {label}
        </label>
      </div>
    );
  }
}

FormFileInput.displayName = "Form.FileInput";

export default FormFileInput;