-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FileInput): Add FileInput component (#1021)
- v117.3.0
- v117.2.2
- v117.2.1
- v117.2.0
- v117.1.0
- v117.0.0
- v116.0.0
- v115.1.0
- v115.0.2
- v115.0.1
- v115.0.0
- v114.0.1
- v114.0.0
- v113.9.0
- v113.8.0
- v113.7.1
- v113.7.0
- v113.6.0
- v113.5.0
- v113.4.0
- v113.3.0
- v113.2.0
- v113.1.0
- v113.0.0
- v112.3.0
- v112.2.0
- v112.1.0
- v112.0.0
- v111.21.0
- v111.20.0
- v111.19.0
- v111.18.1
- v111.18.0
- v111.17.0
- v111.16.0
- v111.15.2
- v111.15.1
- v111.15.0
- v111.14.0
- v111.13.0
- v111.12.0
- v111.11.0
- v111.10.0
- v111.9.0
- v111.8.1
- v111.8.0
- v111.7.0
- v111.6.0
- v111.5.0
- v111.4.0
- v111.3.1
- v111.3.0
- v111.2.0
- v111.1.1
- v111.1.0
- v111.0.0
- v110.8.2
- v110.8.1
- v110.8.0
- v110.7.1
- v110.7.0
- v110.6.0
- v110.5.0
- v110.4.0
- v110.3.0
- v110.2.0
- v110.1.1
- v110.1.0
- v110.0.1
- v110.0.0
- v109.2.0
- v109.1.1
- v109.1.0
- v109.0.2
- v109.0.1
- v109.0.0
- v108.1.0
- v108.0.0
- v107.4.1
- v107.4.0
- v107.3.0
- v107.2.0
- v107.1.0
- v107.0.0
- v106.9.0
- v106.8.0
- v106.7.0
- v106.6.0
- v106.5.0
- v106.4.0
- v106.3.0
- v106.2.0
- v106.1.1
- v106.1.0
- v106.0.0
- v105.12.0
- v105.11.0
- v105.10.2
- v105.10.1
- v105.10.0
- v105.9.1
- v105.9.0
- v105.8.0
- v105.7.0
- v105.6.0
- v105.5.0
- v105.4.0
- v105.3.1
- v105.3.0
- v105.2.1
- v105.2.0
- v105.1.0
- v105.0.2
- v105.0.1
- v105.0.0
- v104.3.0
- v104.2.0
- v104.1.0
- v104.0.0
- v103.12.0
- v103.11.1
- v103.11.0
- v103.10.0
- v103.9.0
- v103.8.2
- v103.8.1
- v103.8.0
- v103.7.0
- v103.6.2
- v103.6.1
- v103.6.0
- v103.5.0
- v103.4.0
- v103.3.1
- v103.3.0
- v103.2.0
- v103.1.1
- v103.1.0
- v103.0.0
- v102.2.1
- v102.2.0
- v102.1.1
- v102.1.0
- v102.0.0
- v101.2.0
- v101.1.1
- v101.1.0
- v101.0.0
- v100.2.0
- v100.1.0
- v100.0.0
- v99.0.4
- v99.0.3
- v99.0.2
- v99.0.1
- v99.0.0
- v98.2.0
- v98.1.1
- v98.1.0
- v98.0.0
- v97.2.1
- v97.2.0
- v97.1.0
- v97.0.0
- v96.2.0
- v96.1.1
- v96.1.0
- v96.0.0
- v95.11.1
- v95.11.0
- v95.10.0
- v95.9.0
- v95.8.0
- v95.7.0
- v95.6.0
- v95.5.0
- v95.4.0
- v95.3.0
- v95.2.0
- v95.1.0
- v95.0.0
- v94.1.0
- v94.0.0
- v93.5.0
- v93.4.0
- v93.3.0
- v93.2.0
- v93.1.1
- v93.1.0
- v93.0.1
- v93.0.0
- v92.4.0
- v92.3.0
- v92.2.0
- v92.1.0
- v92.0.1
- v92.0.0
- v91.2.0
- v91.1.1
- v91.1.0
- v91.0.0
- v90.7.1
- v90.7.0
- v90.6.0
- v90.5.0
- v90.4.0
- v90.3.0
- v90.2.0
- v90.1.0
- v90.0.0
- v89.1.0
- v89.0.0
- v88.6.0
- v88.5.0
- v88.4.0
- v88.3.0
- v88.2.1
- v88.2.0
- v88.1.1
- v88.1.0
- v88.0.0
- v87.0.0
- v86.3.0
- v86.2.0
- v86.1.0
- v86.0.0
- v85.9.0
- v85.8.1
- v85.8.0
- v85.7.0
- v85.6.0
- v85.5.0
- v85.4.0
- v85.3.1
- v85.3.0
- v85.2.0
- v85.1.0
- v85.0.2
- v85.0.1
- v85.0.0
- v84.3.0
- v84.2.0
- v84.1.6
- v84.1.5
- v84.1.4
- v84.1.3
- v84.1.2
- v84.1.1
- v84.1.0
- v84.0.0
- v83.4.0
- v83.3.0
- v83.2.0
- v83.1.1
- v83.1.0
- v83.0.0
- v82.14.0
- v82.13.0
- v82.12.0
- v82.11.0
- v82.10.0
- v82.9.0
- v82.8.0
- v82.7.0
- v82.6.1
- v82.6.0
- v82.5.1
- v82.5.0
- v82.4.0
- v82.3.0
- v82.2.0
- v82.1.2
- v82.1.1
- v82.1.0
- v82.0.0
- v81.9.0
- v81.8.0
- v81.7.0
- v81.7.0-muiv5
- v81.6.1
- v81.6.0
- v81.5.0
- v81.4.0
- v81.3.0
- v81.2.1
- v81.2.0
- v81.1.0
- v81.0.0
- v80.3.0
- v80.2.1
- v80.2.0
- v80.1.1
- v80.1.0
- v80.0.2
- v80.0.1
- v80.0.0
- v79.5.0
- v79.4.0
- v79.3.0
- v79.2.3
- v79.2.2
- v79.2.1
- v79.2.0
- v79.1.0
- v79.0.0
- v78.1.1
- v78.1.0
- v78.0.0
- v77.10.0
- v77.9.2
- v77.9.1
- v77.9.0
- v77.8.0
- v77.7.1
- v77.7.0
- v77.6.0
- v77.5.0
- v77.4.0
- v77.3.0
- v77.2.0
- v77.1.0
- v77.0.0
- v76.2.0
- v76.1.0
- v76.0.0
- v75.6.1
- v75.6.0
- v75.5.0
- v75.4.1
- v75.4.0
- v75.3.0
- v75.2.0
- v75.1.0
- v75.0.1
- v75.0.0
- v74.7.0
- v74.6.1
- v74.6.0
- v74.5.0
- v74.4.2
- v74.4.1
- v74.4.0
- v74.3.0
- v74.2.0
- v74.1.0
- v74.0.0
- v73.3.0
- v73.2.3
- v73.2.2
- v73.2.1
- v73.2.0
- v73.1.0
- v73.0.1
- v73.0.0
- v72.1.0
- v72.0.0
- v71.0.0
- v70.7.0
- v70.6.2
- v70.6.1
- v70.6.0
- v70.5.1
- v70.5.0
- v70.4.2
- v70.4.1
- v70.4.0
- v70.3.0
- v70.2.4
- v70.2.3
- v70.2.2
- v70.2.1
- v70.2.0
- v70.1.0
- v70.0.0
- v69.4.1
- v69.4.0
- v69.3.0
- v69.2.0
- v69.1.0
- v69.0.0
- v68.9.1
- v68.9.0
- v68.8.0
- v68.7.0
- v68.6.0
- v68.5.1
- v68.5.0
- v68.4.0
- v68.3.1
- v68.3.0
- v68.2.0
- v68.1.1
- v68.1.0
- v68.0.1
- v68.0.0
- v67.0.4
- v67.0.3
- v67.0.2
- v67.0.1
- v67.0.0
- v66.2.4
- v66.2.3
- v66.2.2
- v66.2.1
- v66.2.0
- v66.1.0
- v66.0.0
- v65.1.3
- v65.1.2
- v65.1.1
- v65.1.0
- v65.0.1
- v65.0.0
- v64.0.0
- v63.0.0
- v62.12.0
- v62.11.0
- v62.10.0
- v62.9.1
- v62.9.0
- v62.8.0
- v62.7.0
- v62.6.0
- v62.5.3
- v62.5.2
- v62.5.1
- v62.5.0
- v62.4.3
- v62.4.2
- v62.4.1
- v62.4.0
- v62.3.0
- v62.2.0
- v62.1.4
- v62.1.3
- v62.1.2
- v62.1.1
- v62.1.0
- v62.0.4
- v62.0.3
- v62.0.2
- v62.0.1
- v62.0.0
- v61.1.1
- v61.1.0
- v61.0.0
- v60.12.0
- v60.11.0
- v60.10.1
- v60.10.0
- v60.9.1
- v60.9.0
- v60.8.2
- v60.8.1
- v60.8.0
- v60.7.0
- v60.6.1
- v60.6.0
- v60.5.0
- v60.4.0
- v60.3.0
- v60.2.0
- v60.1.0
- v60.0.0
- v59.3.0
- v59.2.0
- v59.1.0
- v59.0.0
- v58.5.1
- v58.5.0
- v58.4.1
- v58.4.0
- v58.3.0
- v58.2.0
- v58.1.2
- v58.1.1
- v58.1.0
- v58.0.0
- v57.10.0
- v57.9.2
- v57.9.1
- v57.9.0
- v57.8.0
- v57.7.0
- v57.6.0
- v57.5.5
- v57.5.4
- v57.5.3
- v57.5.2
- v57.5.1
- v57.5.0
- v57.4.0
- v57.3.1
- v57.3.0
- v57.2.1
- v57.2.0
- v57.1.0
- v57.0.0
- v56.0.1
- v56.0.0
- v55.0.0
- v54.1.1
- v54.1.0
- v54.0.0
- v53.0.0
- v52.0.1
- v52.0.0
- v51.12.0
- v51.11.0
- v51.10.0
- v51.9.1
- v51.9.0
- v51.8.0
- v51.7.1
- v51.7.0
- v51.6.0
- v51.5.2
- v51.5.1
- v51.5.0
- v51.4.0
- v51.3.1
- v51.3.0
- v51.2.1
- v51.2.0
- v51.1.0
- v51.0.2
- v51.0.1
- v51.0.0
- v50.9.3
- v50.9.2
- v50.9.1
- v50.9.0
- v50.8.0
- v50.7.0
- v50.6.0
- v50.5.0
- v50.4.0
- v50.3.0
- v50.2.0
- v50.1.2
- v50.1.1
- v50.1.0
- v50.0.0
- v49.5.0
- v49.4.0
- v49.3.0
- v49.2.0
- v49.1.1
- v49.1.0
- v49.0.4
- v49.0.3
- v49.0.2
- v49.0.1
- v49.0.0
- v48.0.0
- v47.6.0
- v47.5.1
- v47.5.0
- v47.4.0
- v47.3.0
- v47.2.0
- v47.1.0
- v47.0.1
- v47.0.0
- v46.1.0
- v46.0.0
- v45.5.0
- v45.4.1
- v45.4.0
- v45.3.0
- v45.2.0
- v45.1.0
- v45.0.0
- v44.14.0
- v44.13.0
- v44.12.0
- v44.11.0
- v44.10.1
- v44.10.0
- v44.9.0
- v44.8.0
- v44.7.0
- v44.6.0
- v44.5.0
- v44.4.0
- v44.3.0
- v44.2.0
- v44.1.3
- v44.1.2
- v44.1.1
- v44.1.0
- v44.0.5
- v44.0.4
- v44.0.3
- v44.0.2
- v44.0.1
- v44.0.0
- v43.6.0
- v43.5.0
- v43.4.0
- v43.3.0
- v43.2.0
- v43.1.0
- v43.0.0
- v42.4.1
- v42.4.0
- v42.3.0
- v42.2.0
- v42.1.1
- v42.1.0
- v42.0.1
- v42.0.0
- v41.4.1
- v41.4.0
- v41.3.1
- v41.3.0
- v41.2.0
- v41.1.0
- v41.0.0
- v40.10.0
- v40.9.1
- v40.9.0
- v40.8.0
- v40.7.2
- v40.7.1
- v40.7.0
- v40.6.1
- v40.6.0
- v40.5.0
- v40.4.2
- v40.4.1
- v40.4.0
- v40.3.0
- v40.2.1
- v40.2.0
- v40.1.1
- v40.1.0
- v40.0.0
- v39.2.0
- v39.1.0
- v39.0.0
- v38.3.1
- v38.3.0
- v38.2.0
- v38.1.0
- v38.0.1
- v38.0.0
- v37.9.0
- v37.8.0
- v37.7.0
- v37.6.0
- v37.5.0
- v37.4.0
- v37.3.2
- v37.3.1
- v37.3.0
- v37.2.1
- v37.2.0
- v37.1.0
- v37.0.1
- v37.0.0
- v36.6.2
- v36.6.1
- v36.6.0
- v36.5.0
- v36.4.0
- v36.3.1
- v36.3.0
- v36.2.0
- v36.1.2
- v36.1.1
- v36.1.0
- v36.0.0
- v35.46.0
- v35.45.0
- v35.44.0
- v35.43.0
- v35.42.0
- v35.41.0
- v35.40.2
- v35.40.1
- v35.40.0
- v35.39.0
- v35.38.0
- v35.38.0-beta.1
- v35.37.0
- v35.36.1
- v35.36.0
- v35.35.0
- v35.34.0
- v35.33.3
- v35.33.2
- v35.33.1
- v35.33.0
- v35.33.0-beta.1
- v35.32.1
- v35.32.0
- v35.31.0
- v35.30.0
- v35.29.1
- v35.29.0
- v35.28.0
- v35.27.0
- v35.26.0
- v35.25.0
- v35.24.0
- v35.23.0
- v35.22.0
- v35.21.0
- v35.20.0
- v35.19.0
- v35.18.0
- v35.18.0-beta.2
- v35.18.0-beta.1
- v35.17.0
- v35.16.0
- v35.15.0
- v35.14.0
- v35.14.0-beta.2
- v35.14.0-beta.1
- v35.13.0
- v35.12.0
- v35.11.0
- v35.10.0
- v35.9.0
- v35.8.0
- v35.7.3
- v35.7.2
- v35.7.1
- v35.7.0
- v35.6.0
- v35.5.0
- v35.4.0
- v35.3.1
- v35.3.0
- v35.2.0
- v35.1.0
- v35.0.1
- v35.0.0
- v34.2.0
- v34.1.0
- v34.0.0
- v33.7.0
- v33.6.0
- v33.5.0
- v33.4.0
- v33.3.1
- v33.3.0
- v33.2.1
- v33.2.0
- v33.1.3
- v33.1.2
- v33.1.1
- v33.1.0
- v33.0.0
- v32.3.1
- v32.3.0
- v32.2.1
- v32.2.0
- v32.1.1
- v32.1.0
- v32.0.1
- v32.0.0
- v31.0.0
- v30.8.2
- v30.8.1
- v30.8.0
- v30.7.0
- v30.6.0
- v30.5.1
- v30.5.0
- v30.4.1
- v30.4.0
- v30.3.0
- v30.2.0
- v30.1.0
- v30.0.0
- v29.9.1
- v29.9.0
- v29.8.0
- v29.7.0
- v29.6.0
- v29.5.0
- v29.4.0
- v29.3.1
- v29.3.0
- v29.2.2
- v29.2.1
- v29.2.0
- v29.1.2
- v29.1.1
- v29.1.0
- v29.0.0
- v28.13.0
- v28.12.0
- v28.11.2
- v28.11.1
- v28.11.0
- v28.10.0
- v28.9.1
- v28.9.0
- v28.8.0
- v28.7.0
- v28.6.0
- v28.5.0
- v28.4.1
- v28.4.0
- v28.3.0
- v28.2.0
- v28.1.0
- v28.0.0
- v27.14.0
- v27.13.0
- v27.12.1
- v27.12.0
- v27.11.2
- v27.11.1
- v27.11.0
- v27.10.1
- v27.10.0
- v27.9.0
- v27.8.0
- v27.7.0
- v27.6.2
- v27.6.1
- v27.6.0
- v27.5.1
- v27.5.0
- v27.4.2
- v27.4.1
- v27.4.0
- v27.3.0
- v27.2.0
- v27.1.0
- v27.0.0
- v26.9.0
- v26.8.1
- v26.8.0
- v26.7.2
- v26.7.1
- v26.7.0
- v26.6.0
- v26.5.0
- v26.4.1
- v26.4.0
- v26.3.1
- v26.3.0
- v26.2.0
- v26.1.0
- v26.0.0
- v25.13.1
- v25.13.0
- v25.12.0
- v25.11.0
- v25.10.2
- v25.10.1
- v25.10.0
- v25.9.0
- v25.8.0
- v25.7.0
- v25.6.1
- v25.6.0
- v25.5.0
- v25.4.1
- v25.4.0
- v25.3.2
- v25.3.1
- v25.3.0
- v25.2.0
- v25.1.0
- v25.0.0
- v24.8.0
- v24.7.0
- v24.6.0
- v24.5.0
- v24.4.2
- v24.4.1
- v24.4.0
- v24.3.0
- v24.2.2
- v24.2.1
- v24.2.0
- v24.1.1
- v24.1.0
- v24.0.1
- v24.0.0
- v23.5.0-beta.1
- v23.4.0
- v23.3.0
- v23.2.0
- v23.1.0
- v23.0.0
- v22.14.1
- v22.14.0
- v22.13.1
- v22.13.0
- v22.12.3
- v22.12.2
- v22.12.1
- v22.12.0
- v22.11.0
- v22.10.0
- v22.9.1
- v22.9.0
- v22.8.0
- v22.7.2
- v22.7.1
- v22.7.0
- v22.6.0
- v22.5.0
- v22.4.0
- v22.3.2
- v22.3.1
- v22.3.0
- v22.2.0
- v22.1.2
- v22.1.1
- v22.1.0
- v22.0.0
- v21.7.0
- v21.6.0
- v21.5.1
- v21.5.0
- v21.4.4
- v21.4.3
- v21.4.2
- v21.4.1
- v21.4.0
- v21.3.1
- v21.3.0
- v21.2.3
- v21.2.2
- v21.2.1
- v21.2.0
- v21.1.1
- v21.1.0
- v21.0.0
- v20.24.0
- v20.23.0
- v20.22.0
- v20.21.0
- v20.20.0
- v20.19.0
- v20.18.0
- v20.17.0
- v20.16.5
- v20.16.4
- v20.16.3
- v20.16.2
- v20.16.1
- v20.16.0
- v20.15.0
- v20.14.0
- v20.13.0
- v20.12.2
- v20.12.1
- v20.12.0
- v20.11.0
- v20.10.1
- v20.10.0
- v20.9.0
- v20.8.0
1 parent
ab3f100
commit 90e07e7
Showing
7 changed files
with
288 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
This component is used to display a customizable input file. It's just a wrapper of input[type=file] and props will be passed to it (so you can use `accept`, `multiple` and others to alter its behavior). | ||
|
||
### Default | ||
|
||
```jsx | ||
<FileInput className="file-selector" onChange={console.log}> | ||
<span role="button">Click me to choose file</span> | ||
</FileInput> | ||
``` | ||
|
||
You render what you want: | ||
|
||
```jsx | ||
<FileInput onChange={console.log}> | ||
<Icon icon="file" role="button" /> | ||
</FileInput> | ||
``` | ||
|
||
### Multiple files | ||
|
||
```jsx | ||
<FileInput multiple onChange={console.log}> | ||
<span role="button">Click me to choose files</span> | ||
</FileInput> | ||
``` | ||
|
||
### Only accept images | ||
|
||
```jsx | ||
<FileInput accept="image/*" multiple onChange={console.log}> | ||
<span>Click me to choose an image</span> | ||
</FileInput> | ||
``` | ||
|
||
### Simple input file | ||
|
||
If you want a classic input file, just set `hidden` prop to `false`: | ||
|
||
```jsx | ||
<FileInput hidden={false} onChange={console.log} /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`FileInput component should pass props to the input 1`] = ` | ||
<label | ||
className="styles__c-file-input___YNZSh" | ||
disabled={false} | ||
> | ||
<span | ||
aria-controls="file-input-123" | ||
onKeyPress={[Function]} | ||
role="button" | ||
tabIndex={0} | ||
> | ||
Click me | ||
</span> | ||
<input | ||
accept="image/*" | ||
hidden={true} | ||
id="file-input-123" | ||
multiple={false} | ||
onChange={[Function]} | ||
type="file" | ||
/> | ||
</label> | ||
`; | ||
|
||
exports[`FileInput component should render a default file selector 1`] = ` | ||
<label | ||
className="styles__c-file-input___YNZSh" | ||
disabled={false} | ||
> | ||
<input | ||
hidden={false} | ||
id="file-input-123" | ||
multiple={false} | ||
onChange={[Function]} | ||
type="file" | ||
/> | ||
</label> | ||
`; | ||
|
||
exports[`FileInput component should render a disabled file selector 1`] = ` | ||
<label | ||
className="styles__c-file-input___YNZSh" | ||
disabled={true} | ||
> | ||
<span | ||
aria-controls="file-input-123" | ||
onKeyPress={[Function]} | ||
role="button" | ||
tabIndex={0} | ||
> | ||
Click me | ||
</span> | ||
<input | ||
hidden={true} | ||
id="file-input-123" | ||
multiple={false} | ||
onChange={[Function]} | ||
type="file" | ||
/> | ||
</label> | ||
`; | ||
|
||
exports[`FileInput component should render a file selector 1`] = ` | ||
<label | ||
className="styles__c-file-input___YNZSh" | ||
disabled={false} | ||
> | ||
<span | ||
aria-controls="file-input-123" | ||
onKeyPress={[Function]} | ||
role="button" | ||
tabIndex={0} | ||
> | ||
Click me | ||
</span> | ||
<input | ||
hidden={true} | ||
id="file-input-123" | ||
multiple={false} | ||
onChange={[Function]} | ||
type="file" | ||
/> | ||
</label> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import cx from 'classnames' | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import uniqueId from 'lodash/uniqueId' | ||
|
||
import styles from './styles.styl' | ||
|
||
const FileInput = ({ | ||
children, | ||
className, | ||
disabled, | ||
hidden, | ||
multiple, | ||
onChange, | ||
...inputProps | ||
}) => { | ||
const fileinputId = uniqueId('file-input') | ||
const fileinputRef = React.createRef() | ||
return ( | ||
<label | ||
disabled={disabled} | ||
className={cx(styles['c-file-input'], className)} | ||
> | ||
{React.Children.map(children, child => | ||
React.cloneElement(child, { | ||
'aria-controls': fileinputId, | ||
role: 'button', | ||
tabIndex: 0, | ||
onKeyPress: event => { | ||
if (event.key === 'Enter' || event.key === ' ') { | ||
event.preventDefault() | ||
fileinputRef.current.click() | ||
} | ||
} | ||
}) | ||
)} | ||
<input | ||
ref={fileinputRef} | ||
id={fileinputId} | ||
hidden={hidden} | ||
type="file" | ||
multiple={multiple} | ||
onChange={e => { | ||
if (multiple) { | ||
onChange(Array.from(e.target.files)) | ||
} else { | ||
onChange(Array.from(e.target.files)[0]) | ||
} | ||
}} | ||
{...inputProps} | ||
/> | ||
</label> | ||
) | ||
} | ||
|
||
FileInput.propTypes = { | ||
children: PropTypes.node, | ||
className: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
hidden: PropTypes.bool, | ||
multiple: PropTypes.bool, | ||
onChange: PropTypes.func.isRequired | ||
} | ||
|
||
FileInput.defaultProps = { | ||
disabled: false, | ||
hidden: true, | ||
multiple: false | ||
} | ||
|
||
export default FileInput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from 'react' | ||
import uniqueId from 'lodash/uniqueId' | ||
|
||
import FileInput from './index' | ||
|
||
jest.mock('lodash/uniqueId') | ||
|
||
describe('FileInput component', () => { | ||
const onChangeSpy = jest.fn() | ||
const pic1 = new File(['aaaa'], 'pic1.jpg') | ||
const pic2 = new File(['bbbb'], 'pic1.jpg') | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
uniqueId.mockReturnValue('file-input-123') | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('should render a file selector', () => { | ||
const component = shallow( | ||
<FileInput onChange={onChangeSpy}> | ||
<span>Click me</span> | ||
</FileInput> | ||
).getElement() | ||
expect(component).toMatchSnapshot() | ||
}) | ||
|
||
it('should render a default file selector', () => { | ||
const component = shallow( | ||
<FileInput hidden={false} onChange={onChangeSpy} /> | ||
).getElement() | ||
expect(component).toMatchSnapshot() | ||
}) | ||
|
||
it('should render a disabled file selector', () => { | ||
const component = shallow( | ||
<FileInput disabled onChange={onChangeSpy}> | ||
<span>Click me</span> | ||
</FileInput> | ||
).getElement() | ||
expect(component).toMatchSnapshot() | ||
}) | ||
|
||
it('should pass props to the input', () => { | ||
const component = shallow( | ||
<FileInput accept="image/*" onChange={onChangeSpy}> | ||
<span>Click me</span> | ||
</FileInput> | ||
).getElement() | ||
expect(component).toMatchSnapshot() | ||
}) | ||
|
||
it('should process selected file on change', () => { | ||
const filelist = [pic1] | ||
const component = shallow( | ||
<FileInput accept="image/*" onChange={onChangeSpy}> | ||
<span>Click me</span> | ||
</FileInput> | ||
) | ||
component.find('input').simulate('change', { | ||
target: { | ||
files: filelist | ||
} | ||
}) | ||
expect(onChangeSpy).toHaveBeenCalledWith(pic1) | ||
}) | ||
|
||
it('should process selected files on change if it is multiple', () => { | ||
const filelist = [pic1, pic2] | ||
const component = shallow( | ||
<FileInput accept="image/*" multiple onChange={onChangeSpy}> | ||
<span>Click me</span> | ||
</FileInput> | ||
) | ||
component.find('input').simulate('change', { | ||
target: { | ||
files: filelist | ||
} | ||
}) | ||
expect(onChangeSpy).toHaveBeenCalledWith([pic1, pic2]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.c-file-input | ||
cursor pointer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters