changed logic when click on Antd Tab REACT - reactjs

I'm using antd version 3 and want to change the outcome onChange when Tab is clicked.
Here the picture of my desire objective enter image description here
My tab title has a checkbox. Outcome should be in this way: when user is stand at Tab1 should have a possibility to change value checkbox of Tab2 or Tab3 without setting active Tab or moving to Tab2 or Tab3. I have read that tab didn't support HOC... I tried a lot of ways to solve but didn't work out with that. event.PreventDefault / event.stopPropagation similarly result. Even if handlers like onTabClick / onChange didn't set active Tab with a new key after checked checkbox active key is old but tab is render with new information.
Here example of custom tab title component with some extract from code
<Tabs
defaultActiveKey={'Tab 1'}
onChange={(key) => onChangeHandler(key)}
onTabClick={(key: string, event: MouseEvent) => onTabClick(key, event)}
>
<TabPane
tab={<Header name={'Tab1'} />}
key={'Tab 1'}
/>
<TabPane
tab={<Header name={'Tab 2'} />}
key={'Tab 2'}
/>
<TabPane
tab={<Header name={'Tab 3'} />}
key={'Tab 3'}
/>
const Header = ({
name,
onChange,
isActive,
id,
}) => {
return (
<div>
<Checkbox
isActive={isActive}
value={id}
checked={isActive}
onChange={onChange}
/>
<Title >{name}</Title>
</div>
)
}

As far as I understand you want the tabs not to change when you click on the check box.
To do this, you can pass the onClick function to the Checkbox and stop the event bubbling there
const Header = ({ name, onChange, isActive, id }) => {
return (
<div>
<Checkbox
isActive={isActive}
value={id}
checked={isActive}
onChange={onChange}
onClick={(e) => {
e.stopPropagation();
}}
/>
<Title>{name}</Title>
</div>
);
};

Related

Unable to make a custom radio control a part of the radio group that already contains dynamic generated radio controls

Here is a link to the code that is referenced below in my question - https://codesandbox.io/s/material-demo-forked-gtztx?file=/demo.tsx
In the code below (or demo.tsx from the codesandbox link above ), I am trying to combine a new FormControlLabel radio button (line 24) into the dynamically generated material UI radio buttons.
I don't want to add this new radio button into the "options" list within "index.tsx", but I still want this new radio button to be within the same "payment" radio group.
Current behavior: After the page renders, this custom radio with the label as Unknown always remains checked.
Expected behavior: Make this custom radio button work as a part of the dynamically created "payment" radio groups without adding it to the "options" list (see index.tsx)
export default function RadioButtonsGroup(props) {
const [value, setValue] = React.useState(null);
console.log(value);
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setValue((event.target as HTMLInputElement).value);
};
return (
<FormControl component="fieldset">
<FormLabel component="legend">Payment</FormLabel>
<RadioGroup
aria-label="payment"
name="payment1"
value={value}
onChange={handleChange}
>
{/* <FormControlLabel value={value} control={<Radio />} label="Unknown" /> */}
{props.options.map(([value, readable], index) => (
<FormControlLabel
key={index}
value={value}
control={<Radio />}
label={readable}
/>
))}
</RadioGroup>
</FormControl>
);
}
This is what you need - https://codesandbox.io/s/material-demo-forked-jgopr
Basically, I added a checked prop to all the radio buttons (the custom one and the ones in the optionList) to correctly show the selected state of radio control for the value in the component state.

Disable "Select By Typing" in Material UI Menu Component

I'm trying to make a pop-up menu that has a couple different filters (buttons, selects, and text fields). I'm using the Material UI Menu component, but have run into an issue when trying to use the text fields. Because the Menu component is acting like a <Select />, when I type something in the text fields, it tries to select different MenuItems instead of staying focused on the text box.
So using the example found in the sandbox below, users wouldn't be able to type anything in the "search for a different food" textbox if it started with an "A", "B", or "C". If you wanted to type in "apricots", the menu would change focus from the textbox to the "Apples" MenuItem.
I don't see any props for this in the Menu API, so I'm curious to know if there is any work arounds, or even a different component that is more suited for this.
Thanks!
Here's a codesandbox: https://codesandbox.io/s/wizardly-mccarthy-zy2b7
import { useState } from "react";
import "./styles.css";
import { Button, Menu, MenuItem, TextField } from "#material-ui/core";
export default function App() {
const [menu, setMenu] = useState(null);
const [text, setText] = useState("");
return (
<div className="App">
<Button variant="outlined" onClick={(e) => setMenu(e.currentTarget)}>
Filters
</Button>
<Menu
anchorEl={menu}
open={Boolean(menu)}
getContentAnchorEl={null}
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
onClose={() => setMenu(null)}
>
<MenuItem>Apples</MenuItem>
<MenuItem>Bananas</MenuItem>
<MenuItem>Carrots</MenuItem>
<MenuItem>
<TextField
value={text}
onChange={(e) => setText(e.target.value)}
variant={"outlined"}
label={"search a different food..."}
/>
</MenuItem>
</Menu>
</div>
);
}
Use e.stopPropagation() in the onKeyDown event for the MenuItem containing the TextField. This will prevent the key down event from propagating to the Menu component.
<MenuItem onKeyDown={(e) => e.stopPropagation()}>
<TextField
value={text}
onChange={(e) => setText(e.target.value)}
variant={"outlined"}
label={"search a different food..."}
/>
</MenuItem>
Reference: How to disable the selection of an item when the first letter of the option is pressed in the Select component?

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.

React Bootstrap Typeahead children onclick

I have been using a Premade React-Bootstrap Typeahead search-bar found here
I am having trouble accessing the child elements from the dropdown. I want to be able to have an Onclick function for the dropdown items, but it doesn't seem like I have access to the children.
Below is the code I have currently, where I use typeahead
<div className="search-bar">
<Typeahead
id="sample"
options= {cities.map((city) => (city))}
labelKey="name"
placeholder="Enter a city"
/>
</div>
How do I get an onclick for these element cities listed?
You can customize menu rendering and behavior by using the renderMenu prop:
<Typeahead
options={options}
renderMenu={(results, menuProps) => (
<Menu {...menuProps}>
{results.map((result, index) => (
<MenuItem
onClick={() => console.log('click!')}
option={result}
position={index}>
{result.label}
</MenuItem>
))}
</Menu>
)}
/>
With the menu items exposed, you're free to add your own onClick handler to each item. Here's a working example.

How to detect if React Material UI Select Field is expanded?

I am trying to determine if SelectField is expanded or not, i.e. if the dropdown and it's MenuItems are visible.
Currently I use roughly following approach:
<SelectField onClick={() => this.setState({ isExpanded: true })} >
<MenuItem primaryText={
<MenuItemContent onHide={() => this.setState({ isExpanded: false })}} />
}>
</SelectField>
and in MenuItemContent I implement
class MenuItemContent extends React.Component {
componentWillUnmount = () => this.props.onHide()
}
This approach has a drawback, that when you click outside the menu, the componentWillUnmount call is not triggered immidiately, but cca after 200ms, despite the MenuItems are no longer visible.
The Select field menu can be expanded by clicking it, and when is expanded, it will hide either when some menu item is clicked, or user clicks outside the menu.
This is how the SelectField looks when is expanded:
And here is collapsed:
For the better handling of SelectField close event you can use property dropDownMenuProps:
/**
* Object that can handle and override any property of component DropDownMenu.
*/
dropDownMenuProps: PropTypes.object,
By using this prop we can pass to DropDownMenu component onClose prop with handler function, which will be fired instantly after DropDownMenu close (caused by ESC, outside click or item select). Unfortunately DropDownMenu component dont provide similar prop to determine the opening so the only way (without
extending the component) is to follow your approach with onClick event handler.
Here is my working test example:
onSelectClose = () => {
console.log("close")
}
onSelectOpen = () => {
console.log("open")
}
render() {
return (
<MuiThemeProvider>
<div className="App">
<SelectField
floatingLabelText="Frequency"
onClick={this.onSelectOpen}
dropDownMenuProps={{
onClose: this.onSelectClose
}}
value={this.state.value}
onChange={this.handleChange}>
<MenuItem value={1} primaryText="Never" />
<MenuItem value={2} primaryText="Every Night" />
<MenuItem value={3} primaryText="Weeknights" />
<MenuItem value={4} primaryText="Weekends" />
<MenuItem value={5} primaryText="Weekly" />
</SelectField>
</div>
</MuiThemeProvider>
);
}
Detecting if React Material UI Select Field is expanded
The answer is available in Materia-UI Select-API documentation.
Link: https://material-ui.com/api/select/

Resources