React Material Updating TextInput Value - reactjs

So i'm trying basically to have a input field which completes a link and refers the user to it.
However when I click on the button I get a page to open properly but with a 'undefined' at the end as if the input field was not updated.
Any insight would be appreciated :)
let workerID;
const workerLink = "html link";
const workerSearch = workerLink + workerID
return (
<div className={classes.root}>
<Grid container spacing={2}>
<Grid item xs={12}>
<Paper className={classes.paper}>
<h1><DashboardOutlinedIcon /> Job Card</h1>
<Moment className={classes.time}interval={1000} format="HH:mm:ss DD/MM/YYYY" />
</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>
<h3><AccessibilityNewRoundedIcon /> NameGet</h3>
<TextField id="standard-basic" label="Name:" variant="outlined" />
</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>
<h3>Fetch</h3>
<TextField id="standard-basic" label="ID:" variant="outlined" value={workerID}/>
<Button variant="contained" color="primary" className={classes.formstyle} value={workerID}>Search</Button><SearchRoundedIcon />
</Paper>
</Grid>
</Grid>
</div>
);

You should add a onChange attribute to the input(TextField in MaterialUI) and you are better using state in React instead of variables because if the variable updates again react will not update the UI with respect to the value of variable but it will update in case of state.
import React, {useState} from 'react'
// Your Rest of code
const [workerID, setWorkerID] = useState('')//initial value of workerID is empty string
return(
//Rest of Code
<TextField id="standard-basic" label="ID:" variant="outlined" value={workerID} onChange={event=>setWorkerID(event.target.value)}/>
//Rest of Code
)

Related

Warning: Received `true` for a non-boolean attribute `item`

This error shows in my dev tools when I load my app. I'm not sure how to go about changing it to fix the problem. I suspect it is a reason why when I deploy it to heroku it looks different that when I view it locally so I'm hoping if I fix this it will fix my problem with heroku as well.
Here is my code:
return (
<Container className="container" maxWidth="xl">
<CssBaseline />
{/* <Box sx={{ marginTop: 10, padding: 7, bgcolor: "#eeeeee", borderRadius: "0.3rem" }}> */}
<Grid className="homeContainer" container spacing={1}>
<Grid className="topGrid">
<Grid className="imageContainer" item xs={4} sx={{ }}>
<img className="cctLogoLarge" src={cctlogo1} alt="outline of horse with parliament buildings in background"></img>
</Grid>
<Grid className="introContainer" item xs={12}>
<p>Welcome to Cap City Trotters! The CCT was created in 2019 by KMAC, Lynch, Bruster, and Damarts. Our Routes include several loops in both Ottawa and Gatineau.
We are always looking for new members so if you want to join you can take a look at some of our main routes and signup if you like what you see!</p>
{/* Test Connect to Strava */}
</Grid>
</Grid>
<Grid >
<Grid className="postList-grid">
{/* {loggedIn && (
<div className="col-12 mb-3">
<PostForm />
</div>
)} */}
<Grid item xs={6} sx={{ bgcolor: ""}} className="recentPosts">
{loading ? (
<div>Loading...</div>
) : (
<PostList posts={posts}/>
)}
</Grid>
{loggedIn && userData ? (
<Grid>
{/* <FriendList
username={userData.me.username}
friendCount={userData.me.friendCount}
friends={userData.me.friends}
/> */}
</Grid>
) : null}
</Grid>
</Grid>
{/* </Box> */}
</Grid>
</Container>
)
I've looked at similar questions and answers but can't figure out how to solve this problem specifically.
Thanks in advance!
Whenever you add a prop without a value to React component, it evaluates to true:
<MyComponent boo />
Here, MyComponent receives a boo = true property. That's what you have in your code with item, while it shouldn't be a boolean value. Just pass some value to item:
<Grid item={somethingElse}>

React-hook-form + React-i18n = translation not working

I hope you are all good,
I have an object {valueA: "a", valueB: "b", ... } with a lot of fields and rather than writing all setter, you know:
const [valueX, setValueX] = useState(data.valueX?data.valueX:"")
I have been looking for some lazy solution so I find react-hook-form so, here is my code
<form onSubmit={handleSubmit(onSubmit)}>
<Grid container spacing={2}>
{Object.keys(data).map((key) => (
<Grid item xs={10} sm={6} md={3} lg={2} key={key}>
<TextField
label={t(`docsInfo.${key}`)}
name={key}
defaultValue={data[key]}
variante="outlined"
{...register(key)}
/>
</Grid>
))}
</Grid>
<Grid mt={2}>
<Button
variant="contained"
endIcon={<UpgradeIcon />}
type="submit"
>
Update
</Button>
</Grid>
</form>
the code works fine it generates all the fields and the label with the first language (en) but when I click on the second one (jp) nothing happens (T_T)
I've checked my code to be sure my issue isn't something I am missing but could find anything wrong.
the translation module is in a context in my app refer to this
do you have any idea how I can solve my issue?
thanks...
are you sure that you have created the variables for the translation?

RaFormInput width always seems to be 256px

I have the below tabbed form and no matter what I set as far as content (grids, boxes, etc) the width of the parent form is always 256px
The html element is MuiContainer-root RaFormInput-input-61 but its always the RaFormInput with this limit.
Is there something obvious I am missing here?
<Edit {...props} title={<ClientTitle />}>
<TabbedForm>
<FormTab label="details">
<Container maxWidth={false}>
<Grid container className={classes.root} spacing={2}>
<Grid container direction="row">
<Typography variant="h6" gutterBottom>
{translate('resources.clients.fieldGroups.identity')}
</Typography>
</Grid>
<Grid container direction="row" justify="flex-start" alignItems="center" spacing={2}>
<Grid item>
<TextInput source="firstname" resource="clients" validate={requiredValidate} fullWidth />
</Grid>
<Grid item>
<TextInput source="lastname" resource="clients" validate={requiredValidate} fullWidth />
</Grid>
</Grid>
</Grid>
</Container>
</FormTab>
<FormTab label="referrals">
<AddNewReferralButton record={props.record} />
<ReferenceManyField reference="referrals" target="clientid" addLabel={false}>
<Datagrid>
<TextField label="Name" source="name" />
<TextField label="Status" source="status" />
<EditButton />
</Datagrid>
</ReferenceManyField>
</FormTab>
</TabbedForm>
</Edit>
Yes, you are.
SimpleForm docs state that:
The <SimpleForm> renders its child components line by line (within
components). It accepts **Input** and **Field** components as
children.
TabbedForm section underneath in the docs explains that it has the same behaviour as the SimpleForm except:
However, the component renders inputs grouped by tab.
The Mui Container / Grid /Box components are not ra-inputs nor ra-fields. What you are trying to do is build a custom form layout which is achieved through the FormWithRedirect component read here or you can do it entirely by your own following react-final-form docs.

set Value of TextField on button click

I am trying to set the value of TextField when a user clicks on a button.
The error I am getting is:
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
I think it has to do with the Button as I have a onclick function for each one. maybe there a better solution?
const [txtValue, setTxtValue] = useState({})
function campaignAmount(value){
// alert(value)
setTxtValue(value);
}
return (
<Grid container>
<Grid item lg={6}>
<ButtonGroup color="primary" aria-label="outlined primary button group">
<Button onclick={campaignAmount(300)}>$300</Button>
<Button onclick={campaignAmount(500)}>$500</Button>
<Button onclick={campaignAmount(1000)}>$1000</Button>
</ButtonGroup>
</Grid>
<Grid item lg={3}>
<TextField
id="campaignAmount"
name="campaignAmount"
margin="none"
fullWidth
value={`${txtValue[0]}`=== "undefined" ? '0' : `${txtValue[0]}`}
variant="outlined"
required={true}
/>
</Grid>
</Grid>
);
Update:
I have made some changes and now I am getting
Line 164:9: 'setTextValue' is not defined no-undef
Search for the keywords to learn more about each error.
New Code
const [txtValue, setTxtValue] = useState("0")
function schemaTypeSelectionHandle(event) {
// console.log('key: ', $(event.target).data('key'));
setTextValue(event.target.attributes.getNamedItem('data-key').value);
console.log('key: ', event.target.attributes.getNamedItem('data-key').value);
}
return (
<Grid container>
<Grid item lg={6}>
<ButtonGroup color="primary" onClick={this.schemaTypeSelectionHandle.bind(this)} aria-label="outlined primary button group">
<Button data-key='1'>$300</Button>
<Button data-key='1'>$500</Button>
<Button data-key='1'>>$1000</Button>
</ButtonGroup>
</Grid>
<Grid item lg={3}>
<TextField
id="campaignAmount"
name="campaignAmount"
margin="none"
fullWidth
value={`${txtValue[0]}`=== "undefined" ? '0' : `${txtValue[0]}`}
variant="outlined"
required={true}
/>
</Grid>
</Grid>
);
The edited code seems to be working fine. But the only issue is the little typo you have. It is "setTxtValue" not "setTextValue".
Your defined hook => const [txtValue, setTxtValue] = useState("0")

Material-UI InputLabel covers text when Input value set by another Input field

I am creating a form with Material UI. The form has two sections. One is the user's shipping address, and the other is the billing address. In many circumstances the latter is identical to the former, so I provide a checkbox to automatically fill out the billing address with the shipping address. It does so by calling a function that sets the to-be-updated field's value equal to the other field's value, which is saved in the React state.
The MUI Input component handles the fields themselves. The InputLabel component is also used to label them. Normally, when values are entered in a field the InputLabel text moves above the field like this:
However, when the checkbox is checked and the value is populated in the billing address field by typing in the shipping address field, the label fails to get out of the way:
I have been unable to solve the problem and no similar questions on Stack Overflow seem to resolve my issue. How do I fix this?
EDIT:
Here is the code from the screenshot:
<Grid container spacing={40} className={classes.formContainer}>
<Grid item xs>
</Grid>
<Grid item xs>
<FormControlLabel
control={
<Checkbox
checked={this.state.sameAddressScreen2}
onChange={this.handleSameAddressScreen2}
value="sameAddressScreen2"
/>
}
label="Same as Location Address"
/>
</Grid>
</Grid>
<Grid container spacing={40} className={classes.formContainer}>
<Grid item xs={6}>
<FormControl margin="normal" required fullWidth>
<InputLabel htmlFor="locationStreetAddress1">Street Address 1</InputLabel>
<Input id="locationStreetAddress1" name="locationStreetAddress1" autoFocus value={this.state.locationStreetAddress1} onChange={this.handleChange} />
</FormControl>
</Grid>
<Grid item xs={6}>
<FormControl margin="normal" required fullWidth>
<InputLabel htmlFor="mailingStreetAddress1">Street Address 1</InputLabel>
<Input id="mailingStreetAddress1" name="mailingStreetAddress1" autoFocus value={sameValue('mailingStreetAddress1')} onChange={this.handleChange} />
</FormControl>
</Grid>
</Grid>
This is the state and the sameValue function that code invokes:
state = {
locationStreetAddress1: '',
mailingStreetAddress1: '',
sameAddressScreen2: false,
};
sameValue = (field) => {
if (this.state.sameAddressScreen2 === true) {
let stateKey = 'location'.concat(field.slice(7));
return this.state[stateKey];
} else {
return this.state[field];
}
}
The InputLabel component has a property called "shrink" - set this to true when the Input component has a value and you will get the desired behavior.
This problem is specified in the documentation itself.
https://mui.com/material-ui/react-text-field/#shrink
Use This
<TextField InputLabelProps={{ shrink: true }} />
Or
<InputLabel shrink>Count</InputLabel>

Resources