How to make Select Directory using React? - html

I need to upload all files from a folder to server.
I'm trying to implement Select Directory window, and not Select file.
The normal way like:
<input type="file" webkitdirectory directory/>
Did not work for me, and showed Select File window.
But when I created empty regular html file with this input tag, it was working fine.
Does anybody know how to implement the solution using React?
Thank you!

try bheptinh.
<input directory="" webkitdirectory="" type="file" />

In React 17 with Typescript, if you are using useRef Hook, the best bet is to extend React's HTMLAttributes (in the same file of your input) and then simply add directory and webkitdirectory attributes in input tag as
import * as React from "react";
export const ImportForm: React.FunctionComponent<FormProps> = (props) => {
const folderInput= React.useRef(null);
return (
<>
<div className="form-group row">
<div className="col-lg-6">
<label>Select Folder</label>
</div>
<div className="col-lg-6">
<input
type="file"
directory=""
webkitdirectory=""
className="form-control"
ref={folderInput}
/>
</div>
</div>
</>)
};
declare module 'react' {
interface HTMLAttributes<T> extends AriaAttributes, DOMAttributes<T> {
// extends React's HTMLAttributes
directory?: string; // remember to make these attributes optional....
webkitdirectory?: string;
}
}

Related

How to make a textarea take markdown properties (HTML)

How do I use markdown properties in a HTML text area, a little like stack overflow's body area?
Of course I have my standard text area made out like this:
<textarea className="form__input form__group"
value={body}
onChange={e => setBody(e.target.value)} />
But I would like to be able to use markup/markdown tags in my text area to then get an output for when the post is made.
for example:
# Hello there,
> I want to be able to make mytext area be able to take this and then output markdown style body.
Hello there,
I want to be able to make mytext area be able to take this and then output markdown style body.
I would suggest using a library to assist with the markdown. I have used this one before and it works quite well: https://uiwjs.github.io/react-md-editor/
install: npm i #uiw/react-md-editor
Import into react: import MEditor from "#uiw/react-md-editor";
Then just use the MEditor and MEditor.markdown components to input and show your markdown.
There is way more info in the docs so just read https://uiwjs.github.io/react-md-editor/
I also use #uiw/react-md-editor as Croc suggested.
I would also recommend taking care of malicious scripts that untrusted bad actors/users may abuse.
Unfortunately, that's not well documented in the library documentation so here's how you may want to do it:
In edit mode:
<MDEditor
value={value}
autoFocus={false}
onChange={handleChange}
previewOptions={{ skipHtml: true, escapeHtml: true, transformLinkUri: null, linkTarget: '_blank' }}
/>
In preview/read-only mode:
<MDEditor.Markdown source={value} escapeHtml={true} skipHtml={true} transformLinkUri={null} />
Please try this code, To How to make a textarea take markdown properties (HTML).
<form>
<input name="title" type="text" placeholder="Title?" />
<textarea name="content" data-provide="markdown" rows="10"></textarea>
<label class="checkbox">
<input name="publish" type="checkbox"> Publish
</label>
<hr/>
<button type="submit" class="btn">Submit</button>
</form>
I hope this code will be useful to you.
Thank you.
I use react-markdown-editor-lite in most of my projects.
// import react, react-markdown-editor-lite, and a markdown parser you like
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import MarkdownIt from 'markdown-it'
import MdEditor from 'react-markdown-editor-lite'
// import style manually
import 'react-markdown-editor-lite/lib/index.css';
// Register plugins if required
// MdEditor.use(YOUR_PLUGINS_HERE);
// Initialize a markdown parser
const mdParser = new MarkdownIt(/* Markdown-it options */);
// Finish!
function handleEditorChange({html, text}) {
console.log('handleEditorChange', html, text)
//You can use the text props to save it to your database
}
export default (props) => {
return (
<MdEditor
style={{ height: "500px" }}
renderHTML={(text) => mdParser.render(text)}
onChange={handleEditorChange}
/>
)
}

How to instantiate a variable in Nuxt

I've tried a couple of different ways to instantiate a variable in Nuxt but neither way seems to work. I have read around the subject and suspect that perhaps what I'm trying to do is not compatible with Webpack but I'm not sure how.
Here is a jsFiddle of the code: jsfiddle.net/tutmoses/z2365g49/4
First in the script section I export dataSize:
<script>
export default {
data(){
return {
page_name: "Run Model",
dataSize: 1296
}
}
</script>
Then in the HTML above I'm trying to import it but nothing renders:
<div class="setting">
<span class="setting-label">Training Size:</span>
<input id="trainingSize" :value="dataSize"></input>
</div>
I've also tried this:
<div class="setting">
<span class="setting-label">Training Size:</span>
<input id="trainingSize" :value= {{ dataSize }}></input>
</div>
...but the value instantiates as
{{
I've tried both of the above options without binding the value but that didn't work either.
Another way I've tried is this in a separate file:
export const nnSettings = {
dataSize: 1296
}
And then importing it with this:
import nnSettings from '~/components/testindex.js'
Again, zip.
The reason why I'm importing the value is because other values will be calculated from it. What would be the standard, best way to do it?
Nuxt (Vue) uses v-model to bind to form input. Have a look here for more info on form bindings
<div class="setting">
<span class="setting-label">Training Size:</span>
<input id="trainingSize" v-model="dataSize"></input>
</div>

How to make data accessible to a React Component when data exists outside of states scope

. .
I am currently using React VR for a school project, but incorporating vanilla javascript into the index.html to create a static HTML UI. In this case I am using an HTML5 form to post data to my back end, yet still, I would like to optimistically render the data using a React component rather than have to retrieve the data from the server.
My data is created in the HTML element, and it needs to make its way into a React Component.
I don't think refs will work, as the research I have done shows that they are for accessing data which is being generated by a React component, and in my case, I am looking for the data to flow from the HTML to the React Component.
~ JAVASCRIPT & HTML ~
//Accepts the Input
<form id='idea-form'>
<input type="text" id="hiddenInput" maxlength=95 autocomplete=off>
</form>
// Initialize the React VR application.
ReactVR.init(
'../index.vr.bundle?platform=vr&dev=true',
document.body
);
I've included my component here, minus imports and exports.
The component maps an array of strings, converts them to JSX, then renders components with the array elements as the text props for the component.
In my case,
//========== IdeaContainer Component ==========
class IdeaContainer extends Component {
//---------- Other Methods ----------
mapIdeasContentToJSX(ideasObjArr) {
newIdeas = [...ideasObjArr]enter code here
ideasJSX = newIdeas.map((idea) => {return (<IdeaText
text={idea.content}
y={Math.random() * 30}
z={Math.random() * -80}
/>)})
return ideasJSX
}
//---------- Lifecycle Methods ----------
componentDidMount(){
this.props.preLoadIdeas()
}
render(){
return(
<View>
{this.mapIdeasContentToJSX(this.props.ideaList)}
</View>
)
}
}
This is the code which lives in the index.html, which exists outside the scope of my React components, meaning I cannot pass it as props or set it to state, as far as I know.
The element with id of 'container' is where the data is being entered, which then needs to make its way to the React Component
<div>
<div id='logoDIV'class="relative" >
<img id='logoIMG' src='./logo.png' draggable="false">
</div>
</div>
<div id="container" style='position: fixed; z-index: 2; bottom: 1%; right: 22%;'>
<div id="input"></div>
<form id='idea-form'>
<input type="text" id="hiddenInput" maxlength=95 autocomplete=off>
</form>
</div>
<canvas id="cloudCover"></canvas>
<!-- When you're ready to deploy your app, update this line to point to your compiled client.bundle.js -->
<script src="./client.bundle?platform=vr"></script>
<script src="./input.js"></script>
<script src='./clouds.js'></script>
<script>
ReactVR.init(
// When you're ready to deploy your app, update this line to point to
// your compiled index.bundle.js
'../index.vr.bundle?platform=vr&dev=true',
document.body
);
</script>
<script>
let x = document.body.querySelector('body > div:nth-child(8) > div > a:nth-child(2)')
x.style.bottom = '100px'
</script>
You are looking for refs.
<input
type="text"
id="hiddenInput"
maxlength=95
autocomplete=off
ref={(input) => { this.textInput = input; }} />
Then you can access the input value like this:
this.textInput.value
https://reactjs.org/docs/refs-and-the-dom.html

React Router v4 <Link> for Form

How can I use <Link> with a <form>? I am trying to avoid programatic routing as I see many warnings against it.
Form has two fields:
<form>
<input type="text" id="what"/>
<input type="text" id="where"/>
<Link><button type="submit"></Link>
</form>
My goal is to send the page to '/' + document.getElementById('where').value + '/' + document.getElementById('what').value, is it possible with v4 router?
With v4, the <Link> is only used to create <a> elements.
I think that most warnings against programmatic navigation come from people not really understanding how history works, especially when attempting to do it outside of components. The withRouter HOC provides an easy way to add routing to a component.
Additionally, I would do the routing from within a form component instead of from the button. Then, you just need to make sure that the button's type is submit (which is the default).
You are also using DOM functions to access the values in your code. I would recommend instead using controlled inputs, but that is obviously up to you.
class NavForm extends React.Component {
constructor(props) {
super(props)
this.state = {
what: '',
where: ''
}
this.submitHandler = this.submitHandler.bind(this)
this.handleInput = this.handleInput.bind(this)
}
handleInput(event) {
const target = event.target
this.setState({
[target.name]: target.value
})
}
submitHandler(event) {
event.preventDefault()
// do some sort of verification here if you need to
this.props.push(`${this.state.where}/${this.state.what}`)
}
render() {
return (
<form onSubmit={this.submitHandler}>
<input
type='text'
name='what'
value={this.state.what}
onChange={this.handleInput} />
<input
type='text'
name='where'
value={this.state.where}
onChange={this.handleInput} />
<button>Submit</button>
</form>
)
}
}
export default withRouter(NavForm)

Angular2 Form works in Index.html but does not work in component.html

Why is this Upload File form example working in Index.html and does not work in any component.html file?
If i move it to any component.html clicking Add File does not even open the browse dialog.
<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
<div id="drop">
<a>Add File</a> Drag and Drop
<input type="file" name="upl" />
</div>
<ul id="uploadList"></ul>
</form>
Edit: I must mention that i import the js files only in Index.html, and i'm not importing also in System.js or angular-cli.build.js
In Index.html one of the references is:
<script src="assets/js/uploadScript.js"></script>
And this is the code from uploadScript.js that should open the browser dialog:
$('#drop a').click(function(){
$(this).parent().find('input').click();
});
Your code in uploadScript.js is (I guess) initialized when the DOM is ready, thats basic how JQuery initialization works. But since your component is not yet in the DOM, nothing is found.
You need to put the initialization code directly into your component, so its called at the correct time, when the form is actually rendered in DOM.
Try to change your to component something like this:
import { Component, ElementRef } from '#angular/core';
// reference to jQuery
declare var $: any;
#Component({
selector: 'my-component',
template: `
<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
<div id="drop">
<a (click)="addFile()">Add File</a> Drag and Drop
<input type="file" name="upl" />
</div>
<ul id="uploadList"></ul>
</form>
`
})
export class JQueryComponent
{
// get the ElementRef
constructor(private _elRef: ElementRef)
{
}
public addFile(): void
{
$(this._elRef.nativeElement).find('#drop a').parent().find('input').click();
}
}
The important part is to use Angular 2 (click) bound directly to the Add File anchor. ElementRef provides a reference to your html DOM element, which can be used to simulate actual click event on the input type="file"