Capture onChange event from inputComponent passed to OutlinedInput, Material UI - reactjs

I'm trying to use OutlinedInput with KeyboardDatePicker, both from Material UI. I'm passing a DatePicker as input component to 'OutlinedInput' component of Material UI. I want to know how to get date from the inputComponent I'm passing.
With the following code I'm getting "_onChange is not a function" error.
const handleInputChange = (event) => {
if (event.target.name === "deliveryDate") {
temp.deliveryDate = event.target.value;
}
}
function datepicker() {
return <MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
name="deliveryDate"
format="MM/dd/yyyy"
value={projectDetails.deliveryDate}
onChange={props.handleInputChange} /> //not sure if this is how I should write it
</MuiPickersUtilsProvider>;
}
return(
<OutlinedTextBox
name="deliveryDate"
id="deliveryDate"
maxlength={20}
onChange={handleInputChange}
inputComponent = {datepicker} //passing the KeyboardDatePicker as input component
inputLabel="Delivery Date" />
)
Below is my OutlinedTextBox component. It returns
<OutlinedInput
name={props.name}
inputProps={{
maxLength: props.maxlength?props.maxlength:100,
}}
onChange={props.handleInputChange}
{...props}
/>
How to capture onChange function of inputComponent attribute.

Update
From what you said in your comment, all you actually have to do then is edit your datepicker component. You dont need to pass in the value, onChange or name directly as those are already being passed in as props from when you use inputComponent={datepicker}. So all you need to do is declare the props and spread them all into the KeyboardDatePicker.
const DatePicker = (props) => {
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
format="MM/dd/yyyy"
{...props}
/>
</MuiPickersUtilsProvider>
);
};
Original Answer.
You could just add the inputVariant="outlined"property to the KeyboardDatePicker as that will make it outlined. Then you dont actually need to use the OutlinedInput component at all.
<KeyboardDatePicker
name="deliveryDate"
format="MM/dd/yyyy"
value={projectDetails.deliveryDate}
onChange={props.handleInputChange}
inputVariant="outlined
/>

Related

How to change Material UI component property according to props

This is my react js code with MUI TextField and I need to apply error and helperText property when error prop has a value. default error prop value is null.
import React from 'react'
import { TextField } from '#mui/material'
const InputField = (props) => {
const { name, label, value,error=null, onChange } = props;
return (
<TextField
variant='outlined'
label={label}
name={name}
value={value}
onChange={onChange}
{...{error && {error:true,helperText:error}}}
/>
)
}
export default InputField
The error is on this line. How I figer it on MUI 5.10.9 version
{...{error && {error:true,helperText:error}}}
use error and helperText prop directly without condition. also use space character inside helperText to avoid height issue when there is no error present. https://mui.com/material-ui/react-text-field/#helper-text
<TextField
variant='outlined'
label={label}
name={name}
value={value}
onChange={onChange}
error={!!error}
helperText={error || ' '}
/>

Material UI datePicker does not focus to a selected date when using initialFocusedDate

I am using Material UI datePicker nad it must have an initial focus date of Oct, 4th, has a minDate of Oct, 4th and a maxDate of Oct, 10th. Additionally, some of the dates between Oct, 4th and Oct, 10th are disabled with shouldDisableDate.
Until this point, everything is working as expected but, I also need to save its selected value to a global state, which I have accomplished using the onChange property.
Now, as soon as I use the onChange property the calendar's initial focus date changes to today/current date but as I mentioned above it must have an initial focus date of Oct, 4th. And if I try to set up it initial focus date with the initialFocusedDate property, Oct, 4th highlighted but when I select a different date it does not get highlighted/focused.
import React from 'react'
import DateMomentUtils from '#date-io/moment'
import { KeyboardDatePicker, MuiPickersUtilsProvider } from '#material-ui/pickers'
import { Controller } from 'react-hook-form'
import moment from 'moment'
const CalendarInput = ({ control, name, accessibility, setValues }) => {
const startDate = moment('October 4, 2021')
let endDate = moment('October 18, 2021')
if (accessibility === true) {
endDate = moment('October 10, 2021')
}
const skipDates = (date) => {
return moment(date).format('DD') > 10 && moment(date).format('DD') < 18
}
return (
<MuiPickersUtilsProvider utils={DateMomentUtils}>
<Controller
require
name={name}
control={control}
render={({ field: { ref, ...rest } }) => (
<KeyboardDatePicker
autoOk
variant='static'
id='date-picker-dialog'
label='Pick a date *'
format='MM/DD/YYYY'
KeyboardButtonProps={{
'aria-label': 'change date'
}}
{...rest}
minDate={startDate}
maxDate={endDate}
shouldDisableDate={skipDates}
//Here is where thing start not working as expected
onChange={(event, x) => {
setValues({ selectedDate: x })
}}
initialFocusedDate={startDate}
/>
)}
/>
</MuiPickersUtilsProvider>
)
}
export default CalendarInput
Any ideas, what am I missing?
You're overwriting the onChange prop, which was initially set with RHF's onChange handler via the spread of {...rest}. So in your onChange callback from the <KeyboardDatePicker /> you should also call RHF's onChange.
<Controller
require
name={name}
control={control}
defaultValue={startDate}
render={({ field: { ref, onChange, ...rest } }) => (
<KeyboardDatePicker
autoOk
variant="static"
id="date-picker-dialog"
label="Pick a date *"
format="MM/DD/YYYY"
KeyboardButtonProps={{
'aria-label': 'change date',
}}
{...rest}
minDate={startDate}
maxDate={endDate}
shouldDisableDate={skipDates}
onChange={(event, x) => {
setValues({ selectedDate: x });
onChange(x);
}}
initialFocusedDate={startDate}
/>
)}
/>
Please note the destructered RHF's onChange in the render prop callback from <Controller />, which is used in the onChange prop of the <KeyboardDatePicker />.
You should also set a defaultValue for your field, from the documentation:
You need to either provide defaultValue at the field-level or useForm with defaultValues.
I set it with the startDate in code example above.

Having an issue with antd datepicker not displaying value

Im having an issue with the datepicker component. I have tried multiple things but the date I am passing will not display in the datepicker. I just set the fields in the beginning of my component. The warrantynotes field shows just fine. I get different errors depending on what I try but mostly :
TypeError: date.clone is not a function
format
form.setFieldsValue({
datepurchased: e.datepurchased,
warrantynotes: e.warrantynotes,
})
<Form.Item
name={'datepurchased'}
label="Date Purchased:"
// defaultValue={moment(warranty.datepurchased)}
// format={dateFormat}
// defaultPickerValue={moment(warranty.datepurchased)}
>
<DatePicker
onChange={handleChange}
// defaultValue={moment(warranty.datepurchased)}
// format={dateFormat}
// defaultPickerValue={moment(warranty.datepurchased)}
></DatePicker>
</Form.Item>
I tried to change your code, use defaultValue instead defaultPickerValue
const { DatePicker, Space } = antd;
function handleChange() {}
const myDate = "2021-01-07";
ReactDOM.render(
<Space direction="vertical" size={12}>
<DatePicker onChange={handleChange} defaultPickerValue={moment()} />
<DatePicker onChange={handleChange} defaultValue={moment()} />
<DatePicker onChange={handleChange} defaultValue={moment(myDate)} />
</Space>,
mountNode
);

How to change variant of text input in react admin

I wanted to change the variant of TextInput using material UI. The default variant is Standard and I want to change that to outlined but not able to do so. Followed documentation for material-ui Material-ui documentation
Below is my code snippet
<TabbedForm>
<FormTab label="INFO">
{/* <TextInput disabled label="" source="id" type="hidden"/> */}
<TextInput label="Name" source="name" variant="outlined"/>
<TextInput source="shortdesc" />
</FormTab>
</TabbedForm>
You need to pass in variant="standard" to the react-admin <TextInput> component. The issue is that the <Filter> and <Create> pages, etc, automatically pass in a bunch of props, like record and basePath, which includes variant: undefined for some reason. So if you just write <TextInput variant="standard" ... />, your prop will get overwritten. And then when the TextInput component passes its props to the ResettableTextField component, the now undefined variant prop gets defaulted to "filled"...
So, you just need to extract the TextInput component out, so that your variant prop comes last in the props order:
export const TextInput = props => <RaTextInput {...props} variant="standard" />
Personally, I've abstracted this all out into a HOC, because this is what you have to do for all the Inputs that use TextFields:
import React from 'react'
import {
TextInput as RaTextInput,
NumberInput as RaNumberInput,
SelectInput as RaSelectInput,
DateInput as RaDateInput,
DateTimeInput as RaDateTimeInput,
NullableBooleanInput as RaNullableBooleanInput,
AutocompleteInput as RaAutocompleteInput,
} from 'react-admin'
const standardize = Component => props => <Component {...props} variant="standard" />
export const TextInput = standardize(RaTextInput)
export const NumberInput = standardize(RaNumberInput)
export const SelectInput = standardize(RaSelectInput)
export const DateInput = standardize(RaDateInput)
export const DateTimeInput = standardize(RaDateTimeInput)
export const NullableBooleanInput = standardize(RaNullableBooleanInput)
export const AutocompleteInput = standardize(RaAutocompleteInput)
Check my code
<TabbedForm>
<FormTab label="INFO">
{/* <TextInput disabled label="" source="id" type="hidden"/> */}
<TextField label="Name" source="name" variant="outlined"/>
<TextField source="shortdesc" />
</FormTab>
</TabbedForm>
check my fork
React-admin has fixed this.
You can now use the variant="outlined | standard | filled" prop on the component enclosing the input.
Ex: SimpleForm, Create, Filter, etc.
The Git resolution

How to change value of Material-UI's Textfield on focus?

I'm trying to change the value of Material-UI's TextField when it focuses. It seems that I can do that through inputProps, but I can't find any example of how to implement that.
add onFocus prop with TextField
<TextField value={this.state.value} onFocus={onFocus} label="Custom CSS" variant="outlined" id="custom-css-outlined-input" />
then handle the onFocus event
const onFocus = () => {
this.setState({
value:'new value'
})
}
it should update the value in the field when focus comes on the text field

Resources