How can I transform this DataPicker into DataRangePicker of Material UI? - reactjs

I've tried some imports but doesn't have much idea how to implement
<form className={classes.formContainer} noValidate autoComplete="off">
<div className={classes.formContent}>
<div className={classes.fieldsContent}>
<TextField
id="search-name-enrollment"
placeholder="Buscar por nome ou número de matrícula"
className={clsx(classes.field, classes.fieldNameEnroll)}
value={nameEnrollment}
onChange={(event) => handleNameEnrollmentChange(event.target.value)}
InputProps={{
disableUnderline: true,
classes: { input: classes.placeholder },
startAdornment: (
<InputAdornment position="start">
<SearchIcon color="primary" />
</InputAdornment>
)
}}
/>
</div>
<Link
component="button"
type="button"
variant="body2"
color="primary"
underline="none"
className={classes.linkFiltros}
onClick={() => {
setOpenFilters(!openFilters)
}}
>
Mais filtros
{openFilters ? (
<KeyboardArrowUpIcon fontSize="small" />
) : (
<KeyboardArrowDownIcon fontSize="small" />
)}
</Link>
<Collapse
in={openFilters}
classes={{
wrapper: classes.wrapper
}}
className={classes.fieldsContent}
timeout="auto"
unmountOnExit
>
<div className={classes.secundaryFieldContent}>
<div className={classes.secundaryField}>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="dd/MM/yyyy"
margin="normal"
id="date-picker-inline"
placeholder="Data de matrícula"
value={dateEnrollment}
onChange={handleDateEnrollmentChange}
className={clsx(classes.field, classes.dates)}
invalidDateMessage=""
InputProps={{
disableUnderline: true,
classes: { input: classes.placeholder }
}}
KeyboardButtonProps={{
'aria-label': 'change date'
}}
/>
</MuiPickersUtilsProvider>
</div>
<div
className={clsx(
classes.secundaryField,
classes.secundaryFieldMarginL
)}
>
<Autocomplete
multiple
limitTags={1}
id="checkboxes-tags-demo"
options={options}
disableCloseOnSelect
value={status}
onChange={handleStatusChange}
getOptionLabel={(option) => option.label}
renderOption={(option, { selected }) => (
<React.Fragment>
<Checkbox
icon={icon}
checkedIcon={checkedIcon}
style={{ marginRight: 8 }}
checked={selected}
/>
{option.label}
</React.Fragment>
)}
renderInput={(params) => (
<TextField
{...params}
className={clsx(classes.field, classes.status)}
placeholder="Status de matrícula"
classes={{ root: classes.underline }}
/>
)}
/>
</div>
</div>
</Collapse>
</div>
</form>
I would like to know how to transform into to DataRangePicker, you guys can help me?
I've tried already some imports and similiar ones, but at all not having a full idea how to implement it. This model that I have already developed just is one date, I would like to have a calendar that I can choose a range as well.

Related

Search Input with side button on Tailwind CSS

So i try to display and search bar with button on the right side of it but i keep geeting this view even though i have display it as flex and justify center, and even when i change it into row it still show up as image below
here is my code:
return (
<div className="p-2">
<div className="text-left pl-4 pb-4 font-bold text-3xl">
<h2>Detail SKU</h2>
</div>
<div className="p-2 justify-center mt-4 flex">
<form
onSubmit={(e) => {
e.preventDefault();
}}
>
<Autocomplete
style={{ width: "100%" }}
id="grouped-demo"
options={options.sort(
(a, b, index) => -b.firstLetter.localeCompare(a.firstLetter)
)}
groupBy={(option) => option.firstLetter}
getOptionLabel={(option) => option.title}
sx={{ width: 300 }}
renderInput={(params) => {
params.inputProps.onKeyDown = handleKeyDown;
return <TextField {...params} label="Search SKU" />;
}}
renderGroup={(params) => (
<li key={params.key}>
<GroupHeader>{params.group}</GroupHeader>
<GroupItems>{params.children}</GroupItems>
</li>
)}
/>
<Button variant="contained">Search</Button>
</form>
</div>
<Box sx={{ width: "100%", typography: "body1" }}>
<TabContext value={value} index={0} classes={{ root: useStyles.tab }}>
<Box sx={{ borderColor: "divider", p: 0 }}>
<TabList
index={0}
classes={{ root: useStyles.tab }}
onChange={handleChange}
variant="scrollable"
scrollButtons="auto"
aria-label="scrollable auto tabs example"
>
<Tab label="BOM" value="1" />
<Tab label="Routing" value="2" />
<Tab label="Depre" value="3" />
<Tab label="OMC" value="4" />
</TabList>
</Box>
<TabPanel style={{ padding: 10 }} value="1">
<div className="m-0 p-0" style={{ height: 400, width: "100%" }}>
<DataGrid {...data} components={{ Toolbar: GridToolbar }} />
</div>
</TabPanel>
<TabPanel value="2">Routing</TabPanel>
<TabPanel value="3">Depre</TabPanel>
<TabPanel value="4">OMC</TabPanel>
</TabContext>
</Box>
</div>
);
};
any help on it??, i've been figure it out but it seems dont help at all or where did do wrong here actually??
try removing display classname flex
<div className="p-2 justify-center mt-4">

Avatar not wooking when i use with Mui selectTag

I use avatar in MUI to display images in tagSelect, but it isn't working.
<FormControl sx={{ width: "27.5rem" }}>
<InputLabel id="demo-multiple-checkbox-label">Tag</InputLabel>
<Select
labelId="demo-multiple-checkbox-label"
id="demo-multiple-checkbox"
multiple={true}
value={valueTag}
onChange={(e) => handleChange(e)}
input={<OutlinedInput label="Tag" />}
renderValue={(selected) => selected.join(", ")}
MenuProps={MenuProps}
>
{listFoodSelectbox.map((item) => (
<MenuItem key={item.id} value={item.name}>
<Stack direction="row" spacing={2}>
{/* <Avatar url="/static/logo7.png" alt="food" /> */}
<Avatar url={item.image} alt="food" />
</Stack>
<Checkbox checked={valueTag.indexOf(item.name) > -1} />
<ListItemText primary={item.name} />
</MenuItem>
))}
</Select>
</FormControl>
<Avatar url={item.image} alt="food" />
to <Avatar src={item.image} alt="food" />
tôi đã nhầm lẫn src thành url
sorry for my stupid

MUI Autocomplete endAdornment blowing away default clearIcon

I am using an MUI Autocomplete and have added an endAdornment loader to the Textfield while the options are being fetched (from an external source), however this is overriding MUI's clearIcon (presumably because it is also an endAdornment). How do ensure that the default clearIcon remains? Here is my code -
<Autocomplete
style={{ margin: 'auto' }}
options={itemOptions}
getOptionLabel={(option: Item) => option? option.name:''}
value={selectedItem}
inputValue={filterInput}
onInputChange={(e, v) => handleInputChange(v)}
onChange={(e, val) => {
if (val) {
handleItemSelect(val)
}
}}
renderInput={(params) => (
<TextField
{...params}
label="Items"
variant="outlined"
style={{ width: '300px' }}
InputProps={{
...params.InputProps,
endAdornment: (
<>
{loading ? <CircularProgress color="inherit" size={25} /> : null}
</>
),
}}
type="text"
/>
)}
/>
I figured it out, just need to add:
{params.InputProps.endAdornment}
to my endAdornment like so:
endAdornment: (
<>
{loading ? <CircularProgress color="inherit" size={25} /> : null}
{params.InputProps.endAdornment}
</>
),

TypeError: Cannot read property 'tipodecontato' of undefined

Good afternoon folks I'm having problems executing the action of displaying my modal follows the error
https://ibb.co/WtdvbCC
Below is my code
const of modal
const [contatoAtualizado, setContatoAtualizado] = useState<IContatos>(dados as IContatos);
useEffect(() => {
if(dados !== undefined){
setContatoAtualizado(dados)
}
}, [dados, contatoAtualizado]);
const para abrir a modal
const handleOpen = () => {
setOpen(true);
};
button open a modal
<Button
color="primary"
startIcon={<Icon fontSize="small">add_circle</Icon>}
onClick={handleOpen}
>
Adicionar registro
</Button>
modal
<Dialog fullWidth open={true}>
<DialogTitle>Adicionar profissão</DialogTitle>
<DialogContent>
<Grid container spacing={2}>
<Grid item lg={6}>
<Box my={1}>
<FormLabel
htmlFor="tipodecontato"
children={"Tipo de contato"}
required={true}
/>
<Select
fullWidth
id="tipodecontato"
name="tipodecontato"
variant="outlined"
value={contatoAtualizado.tipodecontato}
onChange={(e) =>
setContatoAtualizado({
...contatoAtualizado,
tipodecontato: e.target.value as string,
})
}
>
{tipodeContato.map((item, index) => (
<MenuItem key={index} value={item.name}>
{item.name}
</MenuItem>
))}
</Select>
</Box>
</Grid>
<Grid item lg={6}>
<Box my={1}>
<FormLabel
htmlFor="informacaoProfissional"
children={"Informação profissional"}
/>
<Select
fullWidth
id="informacaoProfissional"
name="informacaoProfissional"
variant="outlined"
value={contatoAtualizado.informacaoProfissional}
onChange={(e) =>
setContatoAtualizado({
...contatoAtualizado,
informacaoProfissional: e.target.value as string,
})
}
>
{informacaoProfissional.map((item, index) => (
<MenuItem key={index} value={item.name}>
{item.name}
</MenuItem>
))}
</Select>
</Box>
</Grid>
</Grid>
<Grid container>
<Grid item lg={12}>
<Box my={1}>
<FormLabel
htmlFor="contato"
children={"Contato"}
required={true}
/>
<TextField
required
fullWidth
id="contato"
name="contato"
placeholder="Digite aqui"
variant="outlined"
margin="none"
value={contatoAtualizado.contato}
onChange={(e) => {
setContatoAtualizado({
...contatoAtualizado,
contato: e.target.value,
});
}}
/>
</Box>
</Grid>
</Grid>
<Grid container>
<Grid item lg={12}>
<Box my={1}>
<FormLabel htmlFor="observacao" children={"Observação"} />
<TextField
required
fullWidth
placeholder="Digite aqui"
id="observacao"
name="observacao"
variant="outlined"
margin="none"
value={contatoAtualizado.observacao}
onChange={(e) => {
setContatoAtualizado({
...contatoAtualizado,
observacao: e.target.value,
});
}}
/>
</Box>
</Grid>
</Grid>
</DialogContent>
<DialogActions>
<Button variant="outlined" color="primary" onClick={onClose}>
Cancelar
</Button>
<Button
variant="contained"
color="primary"
onClick={() => {
onSave(contatoAtualizado);
//console.log(contatoAtualizado)
}}
>
Confirmar
</Button>
</DialogActions>
</Dialog>
in this case I'm importing the modal to another component and opening it through the other component

How to update state of a component which uses two context consumers

I have a class component which uses two contexts with the value generated from API calls to a REST API.
What I want to do is get the context values and use them to update my component state.
I'm passing the context values like so
<TextContext.Consumer>
{(textContext) => (
<UserContext.Consumer>
{(userConsumer) => {
const text = textContext.text;
const user = userConsumer.user;
if(text != null && user != null){
return (
<div className="md:flex max-w-2xl">
<div className="flex flex-col flex-1 md:pr-32">
<FuseAnimateGroup
enter={{
animation: "transition.slideUpBigIn"
}}
>
<div style={{paddingRight:"8px"}}>
<Typography variant="h4" >{text.TITLE_PAGE_PROFILE}</Typography>
<TextField
id="outlined-full-width"
style={{ margin: 8 }}
placeholder={text.PROFILE_EMAIL_PLACEHOLDER}
value = {user.email}
disabled
fullWidth
margin="normal"
variant="outlined"
InputProps={{
endAdornment: (
<InputAdornment>
<IconButton>
<EmailIcon/>
</IconButton>
</InputAdornment>
)
}}
/>
</div>
<div style={{paddingRight:"8px"}}>
<form className={classes.container} noValidate autoComplete="off">
<TextField
id="outlined-full-width"
style={{ margin: 8 }}
placeholder={text.PROFILE_NAME}
value={user.name_user}
fullWidth
margin="normal"
variant="outlined"
InputProps={{
endAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
)
}}
/>
</form>
</div>
<div style={{paddingRight:"8px"}}>
<TextField
id="outlined-full-width"
style={{ margin: 8 }}
value={user.address_user}
placeholder={text.PROFILE_ADDRESS_PLACEHOLDER}
fullWidth
margin="normal"
variant="outlined"
InputLabelProps={{
shrink: true,
}}
/>
</div>
<div style={{paddingRight:"8px"}}>
<form className={classes.container} noValidate autoComplete="off">
<TextField
id="outlined-full-width"
style={{ margin: 8 }}
value={user.city_user}
label={text.PROFILE_CITY_PLACEHOLDER}
className={classes.textField}
fullWidth
margin="normal"
variant="outlined"
InputProps={{
endAdornment: (
<InputAdornment position="start">
<LocationCityIcon/>
</InputAdornment>
)
}}
/>
</form>
</div>
<div>
<TextField
id="outlined-select-currency"
select
value={user.country_user}
label={text.PROFILE_COUNTRY_PLACEHOLDER}
InputProps={{
endAdornment: (
<InputAdornment>
<IconButton>
<FlagIcon/>
</IconButton>
</InputAdornment>
)
}}
fullWidth
style={{ margin: 8, paddingRight: 8}}
SelectProps={{
MenuProps: {
className: classes.menu,
},
}}
margin="normal"
variant="outlined"
/>
</div>
<div style={{padding:"10px"}}>
<Fab variant="contained" aria-label="delete" className={classes.fab}>
{text.PROFILE_CHANGE_PASSWORD_BUTTON_PLACEHOLDER}
</Fab>
</div>
<div style={{paddingRight:"8px"}}>
<Typography variant="h4" > {text.COMPANY_INFORMATION_TITLE}</Typography>
<TextField
id="outlined-full-width"
style={{ margin: 8 }}
placeholder={text.COMPANY_NAME_PLACEHOLDER}
value={user.name_company}
fullWidth
margin="normal"
variant="outlined"
InputLabelProps={{
shrink: true,
}}
/>
</div>
<div style={{paddingLeft:"10px"}}>
<form className={classes.container} noValidate autoComplete="off">
<TextField
style={divStyle}
id="outlined"
label={text.COMPANY_EU_VAT_PLACEHOLDER}
value={user.vat_company}
className={classes.textField}
margin="normal"
variant="outlined"
/>
<TextField
style={div2Style}
id="outlined"
label={text.COMPANY_NUMBER_PLACEHOLDER}
value={user.registration_number_company}
className={classes.textField}
margin="normal"
variant="outlined"
/>
</form>
</div>
<div style={{paddingRight:"8px"}}>
<TextField
id="outlined-full-width"
style={{ margin: 8 }}
value={user.address_company}
placeholder={text.COMPANY_ADDRESS_PLACEHOLDER}
fullWidth
margin="normal"
variant="outlined"
InputLabelProps={{
shrink: true,
}}
/>
</div>
<div style={{paddingRight:"8px"}}>
<form className={classes.container} noValidate autoComplete="off">
<TextField
id="outlined-full-width"
style={{ margin: 8 }}
label={text.COMPANY_CITY_PLACEHOLDER}
value={user.city_company}
className={classes.textField}
fullWidth
margin="normal"
variant="outlined"
InputProps={{
endAdornment: (
<InputAdornment position="start">
<LocationCityIcon/>
</InputAdornment>
)
}}
/>
</form>
</div>
<div>
<TextField
id="outlined-select-currency"
select
label={text.COMPANY_COUNTRY_PLACEHOLDER}
fullWidth
style={{ margin: 8, paddingRight: 8}}
SelectProps={{
MenuProps: {
className: classes.menu,
},
}}
InputProps={{
endAdornment: (
<InputAdornment>
<IconButton>
<FlagIcon/>
</IconButton>
</InputAdornment>
)
}}
margin="normal"
variant="outlined"
/>
</div>
</FuseAnimateGroup>
</div>
<div className="flex flex-col md:w-320">
<FuseAnimateGroup
enter={{
animation: "transition.slideUpBigIn"
}}
>
<Button variant="contained" size="large" color="default" className={classes.button}>
{text.UPDATE_BUTTON_TEXT}
</Button>
</FuseAnimateGroup>
</div>
</div>
);
} else return <div>Loading...</div>
}
}
</UserContext.Consumer>
)}
</TextContext.Consumer>
I've tried to update the state inside the render by doing something like this
<TextContext.Consumer>
{(textContext) => (
<UserContext.Consumer>
{(userConsumer) => {
const text = textContext.text;
const user = userConsumer.user;
this.setState({
user:user,
text: text,
})
</UserContext.Consumer>
)}
</TextContext.Consumer>
The problem with this approach is that it throws the "Maximum update depth exceeded." error.
How should I go about this?
"Maximum update depth exceeded." error.
Do not setState() inside render().
How should I go about this?
Simply extract a component from it.
const User = (props) => {
return (
<>
<span>{props.user}</span>
<span>{props.text}</span>
</>
);
}
// in render
<TextContext.Consumer>
{(textContext) => (
<UserContext.Consumer>
{(userConsumer) => (
<User
text={textContext.text}
user={userConsumer.user}
/>
))}
</UserContext.Consumer>
)}
</TextContext.Consumer>
<User /> will still re-render every time the props (user, text) changes.
you can't update the state inside the render function.
Like that, you will be in the infinity loop of renders. Whenever you change the state that triggers the render function then you change the state again and so on.
anyway, you don't need to store this state inside the local state to use it, you can use it directly from the context.
First of all - are you sure you really need to store context in state? I don't see any reason to copy context (which always available) to state. Just use values from context, not from state.
But if you really need it, you can't update state in render function, because it will cause the infinite update loop. There some options to do so:
Extract component:
return (
<TextContext.Consumer>
{({ text }) => (
<UserContext.Consumer>
({user}) => <ExtractedComponent text={text} user={user} />
</UserContext.Consumer>
)}
</TextContext.Consumer>
);
Then you just need to overrider getDerrivedStateFromProps() for ExtractedComponent to get new state when props changed.
[ugly way] perform conditional update in render function to prevent infinite loop:
if (state.user !== user || state.text !== text) {
this.setState({ user, text });
}
Probably you can switch to functional components with hooks:
const YourComponent = () => {
const { user } = useContext(UserContext);
const { text } = useContext(TextContext);
const [ state, setState ] = useState({});
useEffect(() => {
setState({ user, text });
}, [ user, text ]);
}

Resources