unable to set year range in mui x date picker i.e. from 2022 - material-ui

I just wanted to show years from 2022 in mui-x date picker
<DatePicker
open={isStartDateOpen}
error={false}
onOpen={() => setIsStartDateOpen(true)}
onClose={() => setIsStartDateOpen(false)}
value={startDate}
inputFormat="dd-MM-yyyy"
closeOnSelect
disablePast
// yearRange="-10 : +10"
// yearRange="1950:2013"
// shouldDisableYear={() => }
onChange={(newValue) => {
setStartDate(newValue);
}}
renderInput={(params) => (
<TextField
fullWidth
{...params}
error={false}
onClick={() => setIsStartDateOpen(true)}
/>
)}
/>
Any Solution that i can show only years from 2022 to 2040 or something like that. as yearRange is not exist in this library

You can add the following to your stylesheet to remove disabled years
.PrivatePickersYear-root.has(.PrivatePickersYear-yearButton.Mui-disabled) {
display: none;
}

Related

MaterialUI Autocomplete - Disable option after selection

How to disable the selected option in the list of options in autocomplete MUI?
For example, after selecting option "b", it should be disabled.
import Autocomplete from "#material-ui/lab/Autocomplete";
import { TextField } from "#material-ui/core";
function App() {
return (
<Autocomplete
freeSolo
id="free-solo-demo"
options={["a", "b", "c"]}
renderInput={params => (
<TextField
{...params}
label="freeSolo"
margin="normal"
variant="outlined"
fullWidth
/>
)}
/>
);
}
You can achieve this using getOptionDisabled prop. You just have to pass a function to this prop which says to disable the option if already been selected.
There is another prop available in this component called filterSelectedOptions, which filters out the selected option.
You can find both of these working examples over here Stackblitz👈
const [selectedOptions, setSelectedOptions] = useState([]);
const options = ['a', 'b', 'c'];
<Autocomplete
multiple
freeSolo
options={options}
value={selectedOptions}
//disabling selected options
getOptionDisabled={(option) =>
selectedOptions.some((selectedOption) => selectedOption === option)
}
onChange={(_, value) => {
setSelectedOptions(value);
}}
renderInput={(params) => (
<TextField
{...params}
label="Multiple values"
placeholder="Favorites"
/>
)}
/>

mui5 - Autocomplete showing a blank entry when empty

The Autocomplete was working fine in v4. In mui5, the following code causes an empty value selected:
As shown above, an empty value causes a blank entry selected.
<Autocomplete
multiple
id="languagesList"
options={tabPassed.data.languages}
defaultValue={cookies['cookielanguages']?
translateMultiOptions(tabPassed.data.languages, cookies['cookielanguages']):translateMultiOptions(tabPassed.data.languages, currentUser.languages)}
limitTags={3}
groupBy={option => option.frequent}
getOptionLabel={(option) => (option && option.title)? option.title: ""}
onChange={(event, fieldValue) => {handleInputChange(event, fieldValue, "languages")}}
renderInput={(params) => <TextField {...params} label="Language " variant="outlined" placeholder={"Start Typing OR Select"}/>}
/>

Touchable components are not working in ReactNative when used inside ScrollView ->Flat-List ->map for iPhone 6s only

The onPress function of all touchables is not triggering the function when used inside ScrollView -> Flat-List -> map for iPhone 6s only. The code is working fine for all other devices (iPhone X, iPhone11 etc). The problem is resolved when I used onPressIn but it degrades the user experience.
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
<FlatList
data={props.seats}
keyExtractor={(index) => `seater${index}`}
scrollEnabled={false}
renderItem={({ item, index }) => (
{ props.seats[index].map(
(object, i) => (
<TouchableOpacity onPress={() => alert("Helllo")}>
</TouchableOpacity>
)
)}
)}
/>
</ScrollView>
Try removing ScrollView as Flatlist already have scroll and for horizontal you can add it like
<FlatList
data={props.seats}
keyExtractor={(index) => `seater${index}`}
scrollEnabled={false}
renderItem={({ item, index }) => (
{ props.seats[index].map(
(object, i) => (
<TouchableOpacity onPress={() => alert("Helllo")}>
</TouchableOpacity>
)
)}
)}
horizontal // add it here
/>

material ui picker with formik with class component

I have #material-ui/pickers with Formik in my React Class Component whenever I am trying to change date & time from picker i am getting below error
TypeError : Cannot read property 'type' of undefined
Below is my code
// Handle fields change
handleChange = input => e => {
this.props.formikHandleChange(e);
this.setState({ [input]: e.target.value });
};
<MuiPickersUtilsProvider utils={MomentUtils}>
<DateTimePicker
label="Creation Date"
name={creationDate}
onChange={handleChange('creationDate')}
value={values.creationDate}
onBlur={formikHandleBlur}
/>
</MuiPickersUtilsProvider>
you can use formik setFieldValue
onChange={(date) =>
setFieldValue(
'creationDate',
date
)}
// Handle fields change
handleChange = (e, input) => {
this.props.formikHandleChange(e);
this.setState({ [input]: e.target.value });
};
<MuiPickersUtilsProvider utils={MomentUtils}>
<DateTimePicker
label="Creation Date"
name={creationDate}
onChange={(e) => handleChange(e,input)}
value={values.creationDate}
onBlur={formikHandleBlur}
/>
</MuiPickersUtilsProvider>
You can set the property with [e.target.name]: e.target.value

what's ref in react native and when should i use ref?

I'm working on react native project, i created forms with react native components.
I used TextInput to edit a state value like this :
<TextInput
shake
keyboardAppearance='light'
autoFocus={false}
autoCapitalize='none'
autoCorrect={false}
keyboardType='default'
returnKeyType='next'
value={this.state.sector}
onChangeText={sector => this.setState({ sector })}
/>
With console.log the sector value i get correctly the current value after input change, but i have seen some examples with ref like this :
<TextInput
shake
keyboardAppearance='light'
autoFocus={false}
autoCapitalize='none'
autoCorrect={false}
keyboardType='default'
returnKeyType='next'
value={this.state.sector}
ref={input => (this.sectorInput = input)}
onChangeText={sector => this.setState({ sector })}
/>
i don't understand this operation :
ref={input => (this.sectorInput = input)}
can someone explain what's the ref and why we used with an input and when should i use a ref ?
If you want to access TextInput methods then you have to create reference of that component, then using reference you can use it's method.
For example you have form in your app and you want that when user fill the first field and after that you want to set focus on next field then you can do like this :
<TextInput
shake
keyboardAppearance='light'
autoFocus={false}
autoCapitalize='none'
autoCorrect={false}
keyboardType='default'
returnKeyType='next'
value={this.state.sector}
ref={input => { this.sectorInput = input}}
onSubmitEditing={() => {
this.nextField.focus();
}}
onChangeText={sector => this.setState({ sector })}
/>
<TextInput
shake
keyboardAppearance='light'
autoFocus={false}
autoCapitalize='none'
autoCorrect={false}
keyboardType='default'
returnKeyType='next'
value={this.state.sector}
ref={input => { this.nextField = input}}
onSubmitEditing={() => {
this.handleSubmit();
}}
onChangeText={nextField => this.setState({ nextField })}
/>
Now, when user will fill the sector field then if he press next, then nextField will automatically get focused.