get checked value of ToggleButton (react-bootstrap) using refs in react - reactjs

I have used a toggle button from react- bootstrap (https://react-bootstrap.github.io/components.html#btn-groups-checkbox-radio) in one of my form in react application. I want to get the checked value but it is always showing me undefined. Other form controls are getting easily. Please, help. Below is the code:
<Row>
<Col xs={12} sm={8} md={6}>
<FormGroup controlId="journeyType" validationState={ this.props.validation }>
<ButtonToolbar>
<ToggleButtonGroup type="radio" name="journeyType" defaultValue={"cheapest"} ref="journeyType">
<ToggleButton value={"cheapest"} bsStyle="primary">Cheapest</ToggleButton>
<ToggleButton value={"fastest"} bsStyle="primary">Fatest</ToggleButton>
</ToggleButtonGroup>
</ButtonToolbar>
<FormControl.Feedback />
</FormGroup>
</Col>
</Row>
and in my react function, I am using below:
const sortQuery = {
departure: findDOMNode(this.refs.departure).value,
arrival: findDOMNode(this.refs.arrival).value,
journeyType: findDOMNode(this.refs.journeyType).value
};
console.log(sortQuery);
this.props.handleSort(sortQuery);
In above code, departure and arrival have values from the user but the journeyType always have undefined. It is because, react-dom is rendering the ToggleButtonGroup as radio inside divs. Even if used, journeyType: findDOMNode(this.refs.journeyType.checked).value , also not working.
Hope to hear from you, all. Thanks

You can call your function in the onChange function of the ToggleButtonGroup.
If your onChange={handleChange}, then,
handleChange = (slectedButtonNumber) =>{
//slectedButtonNumber= the 'value' of the selected radio button
}

I think with radio buttons we use .checked instead of .value. So there is no need for the .value.
Try journeyType: findDOMNode(this.refs.journeyType).checked.

Related

React js Material Ui TextField default value doesn't change

I want to change default value of TextField when state change but it doesn't work. I guess it is doesn't re-render.
<TextField
multiline={true}
rows={15}
disabled
id="outlined-basic" label="" variant="outlined"
defaultValue={!isEn?data.data[0].description:data.data[0].descriptionLocalization.en}
/>
<Button style={{position:"absolute",right:"20px",bottom:"5px"}} onClick={changeStateIsEn}>Save</Button>}
Default value is not meant to be changed with state.
You should set the value for reflecting the default value
<TextField
multiline
rows={15}
disabled
id="outlined-basic"
label=""
variant="outlined"
defaultValue="Something that will stay there initially only"
value={!isEn ? data.data[0].description : data.data[0].descriptionLocalization.en}
/>
There are two parts missing to your TextField component that is required to tie the input to state.
As user Mohammad Fasial said, the defaultValue prop is only for the default value the component will have. The correct prop you're looking for is value. State will need to equal value.
To listen for changes when the user inputs new information into the TextField input, you'll need to use the onChange prop. The onChange prop (on change listener) let's you provide a function as an argument to run when the input value changes. In this case we want to set onChange to run setExampleState to set the state to the value of the input field by using the event.target.value property.
function ExampleComponent(){
const [exampleState, setExampleState] = useState([]);
...
return (
<>
<TextField
multiline={true}
rows={15}
disabled
id="outlined-basic" label="" variant="outlined"
defaultValue={!isEn?data.data[0].description:data.data[0].descriptionLocalization.en}
value={exampleState}
onChange={(event) => setExampleState(event.target.value)}
/>
<Button style={{position:"absolute",right:"20px",bottom:"5px"}} onClick={changeStateIsEn}>Save</Button>
</>
);
}
To learn more about the different properties TextField has, you can look at the TextField API Documentation. There are also a lot of TextField code examples that can be expanded on the Material-UI site as well.
Update the key of Element or Container after Default value Change

How to style a ButtonGroup so it doesn't change the form's width with the addition more names in React?

I currently have a component that takes in some data(names), loops through the names and returns a ButtonGroup with a checkbox next to each one. The problems is that if I pass in more names, it makes the form component that it's in wider since it keeps rendering the check boxes one after the other one. So I was wondering how I can make that ButtonGroup a fixed width so it doesn't change the forms width and renders the check boxes on a new row ones it reaches a width.
export default function SharedCheckboxGroup(props) {
return (
<ButtonGroup>
{props.names.map((item) => {
return (
<React.Fragment>
<label>
<input
type="checkbox"
name={item}
value={item}
/>
{item}
</label>
<br />
</React.Fragment>
);
})}
</ButtonGroup>
);
Try out the following inline style :
<ButtonGroup style={{display:'flex', flexWrap:'wrap'}}>

In Next.js, how to scroll to top of window after setting search value on a search bar?

Imagine I have a button somewhere. When clicked, the text on it, now goes to my search bar. How do I make the window scroll to the search bar too after the value is set in the Input element shown below?
<Flex
flexDirection="column"
justifyContent="center"
alignItems="center"
maxWidth="90%"
mt={4}
>
{!filteredBlogPosts.length && 'No posts found.'}
{filteredBlogPosts.map((frontMatter) => (
<>
<BlogPost
key={frontMatter.title + frontMatter.lastPublishedOn}
handleSearch={(anyKey) => setSearchValue(anyKey)}
// Insert your code here, how to scroll after setting the key to search?
{...frontMatter}
/>
</>
))}
</Flex>
And, here is the <Input> field
<Input
aria-label="Search"
borderColor="blue.500"
onChange={(e) => setSearchValue(e.target.value)}
placeholder="Search"
value={searchValue}
//or should I do scroll here? How?
autoFocus
onFocus={e => e.currentTarget.select()}
/>
Is this something easy to do? Please present code examples if possible.
Thanks.
If anyone faces this issue in the future, I solved it using this simple function.
const scrollSearch = myKey => {
window.scrollTo(0, 0);
frontMatter.handleSearch(myKey)
};
And passed the scrollSearch function in button onClick.

Checkbox doesn't toggle when rendered from a function

This is a little bizarre of a problem and I haven't been able to track down the issue. I have a checkbox / input combo I created using Semantic-UI React (Form.Checkbox). The checkbox portion is added to the input, think addon, for a little combo input situation. Desired behavior is when the checkbox is checked it allows the user to type input into the input portion of the combo input. The checkbox's state is managed in the component's local state.
If I just include this code:
<Form.Field className="field">
<label>{field.label}</label>
<Input
{...field.input}
label={{ basic: true, content: <Checkbox
onChange={() => this.setState({sendEmail: !this.state.sendEmail})}
checked={this.state.sendEmail} />,
style: {'borderRight': '0px'}
}}
disabled={!this.state.sendEmail}
labelPosition='left'
placeholder='Email Address'
/>
</Form.Field>
in the jsx (render function) for the component, the toggling actually happens, state is managed properly etc. But when I put the same code into a function to render back the same input like so:
renderCheckboxInputField(field){
return(
<div>
<Form.Field className="field">
<label>{field.label}</label>
<Input
{...field.input}
label={{ basic: true, content: <Checkbox
onChange={() => this.setState({sendEmail: !this.state.sendEmail})}
checked={this.state.sendEmail} />,
style: {'borderRight': '0px'}
}}
disabled={!this.state.sendEmail}
labelPosition='left'
placeholder='Email Address'
/>
</Form.Field>
</div>
);
}
and call it from here:
<Grid.Column>
<Field
name="email"
label="Send Email"
component={this.renderCheckboxInputField}
/>
</Grid.Column>
the checkbox doesn't actually visually toggle, though the state is being updated properly. You might ask why not just keep it in the first location where it already works, but a) for reusability I placed it within the function, and b) I wanted to include it this way inside of the <Field /> component at the bottom because I am using Redux Form for validation on the input portion of the checkbox / input combo I made.
So for some clarification the <Field /> component comes from Redux Form. The <Form.Field /> pertains to a Semantic-UI React component. And all of the hacky stuff inside of the <Input /> component's label field is me just customizing the input to look as I desire.
You are calling this.renderCheckboxInputField without any params, but in function definition you pass 'field' and using it inside

How to restore dropdown to default value in Semantic-UI-React

In default semantic-ui we can do this option: https://github.com/Semantic-Org/Semantic-UI/commit/b4ae26d24f75886c3d5f6fc4f00e176f09705a13
But, how to do it in semantic-ui-react? Google nothing say about it, please, I need help.
What I'm trying to achieve:
I'm using redux-form. In my form present semantic component <Form.Select multiple ....> and after success submit - call to redux-form method reset. All is fine, form is clear... but not dropdown/select field. Any ideas?
I solved the problem as follows:
Semantic-ui-react props value set to the current value of the field.
<Form.Select
name={input.name}
options={options}
label={label}
placeholder={placeholder}
search
multiple
selectOnBlur={selectOnBlur}
onFocus={::this.handleFocus}
onBlur={::this.handleBlur}
onChange={::this.handleChange}
defaultValue={defaultValue || []}
value={input.value}
loading={loading} />

Resources