-
Notifications
You must be signed in to change notification settings - Fork 825
Improve renderInput spreading experience
#1760
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import React, { useState } from 'react'; | ||
| import { styled } from '@material-ui/core'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| import { DesktopDatePicker } from '@material-ui/pickers'; | ||
|
|
||
| const InputContainer = styled('div')({ | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| }); | ||
|
|
||
| function CustomInput() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| const [selectedDate, handleDateChange] = useState(new Date()); | ||
|
|
||
| return ( | ||
| <DesktopDatePicker | ||
| label="Advanced keyboard" | ||
| value={selectedDate} | ||
| onChange={date => handleDateChange(date)} | ||
| renderInput={({ ref, inputProps, InputProps }) => ( | ||
| <InputContainer ref={ref}> | ||
| <input {...inputProps} /> | ||
| {InputProps?.endAdornment} | ||
| </InputContainer> | ||
| )} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default CustomInput; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ import * as ViewsDatePicker from './ViewsDatePicker.example'; | |
| import * as BasicDatePicker from './BasicDatePicker.example'; | ||
| import * as StaticDatePicker from './StaticDatePicker.example'; | ||
| import * as DatePickers from './DatePickers.example'; | ||
| import * as AdvancedKeyboard from './AdvancedKeyboard.example'; | ||
| import * as CustomInput from './CustomInput.example'; | ||
|
|
||
| <PageMeta component="DatePicker" /> | ||
|
|
||
|
|
@@ -53,11 +53,11 @@ For that use `StaticDatePicker`. | |
| <Example paddingBottom source={StaticDatePicker} /> | ||
| </Hidden> | ||
|
|
||
| #### Advanced keyboard input | ||
| #### Custom input component | ||
|
|
||
| We are providing default localized formats, but you can change this behaviour with `format` and `mask` props. | ||
| You can customize rendering input by `renderInput` prop. But make sure you will spread `ref` and `inputProps` correctly to the custom input component. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe? cc @mbrookes
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We must specify that props are https://material-ui.com/api/text-field/#textfield-api. Will refer this comment at #1761
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way the type of (props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>Because of core TextField typings (mui/material-ui#20716)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would a similar approach to Autocomplete work? export interface AutocompleteRenderInputParams {
id: string;
disabled: boolean;
InputLabelProps: object;
InputProps: {
ref: React.Ref<any>;
className: string;
startAdornment: React.ReactNode;
endAdornment: React.ReactNode;
};
inputProps: object;
}
/**
* Render the input.
*
* @param {object} params
* @returns {ReactNode}
*/
renderInput: (params: AutocompleteRenderInputParams) => React.ReactNode; |
||
|
|
||
| <Example source={AdvancedKeyboard} /> | ||
| <Example source={CustomInput} /> | ||
|
|
||
| #### Customization | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React, { useState } from 'react'; | ||
| import { DateRangePicker, DateRange } from '@material-ui/pickers'; | ||
|
|
||
| function CustomRangeInputs() { | ||
| const [selectedDate, handleDateChange] = useState<DateRange>([null, null]); | ||
|
|
||
| return ( | ||
| <DateRangePicker | ||
| label="Advanced keyboard" | ||
| value={selectedDate} | ||
| onChange={date => handleDateChange(date)} | ||
| renderInput={(startProps, endProps) => ( | ||
| <> | ||
| <input ref={startProps.ref as React.Ref<HTMLInputElement>} {...startProps.inputProps} /> | ||
| <input ref={endProps.ref as React.Ref<HTMLInputElement>} {...endProps.inputProps} /> | ||
| </> | ||
| )} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default CustomRangeInputs; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,9 +100,15 @@ function Regression() { | |
| onChange={changeRange} | ||
| renderInput={(startProps, endProps) => ( | ||
| <> | ||
| <TextField {...startProps} inputProps={{ 'data-mui-test': 'desktop-range-picker' }} /> | ||
| <TextField | ||
| {...startProps} | ||
| inputProps={{ ...startProps.inputProps, 'data-mui-test': 'desktop-range-picker' }} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side question, should we use a label to differentiate the two inputs instead of |
||
| /> | ||
| <DateRangeDelimiter> to </DateRangeDelimiter> | ||
| <TextField {...endProps} inputProps={{ 'data-mui-test': 'desktop-range-picker-end' }} /> | ||
| <TextField | ||
| {...endProps} | ||
| inputProps={{ ...endProps.inputProps, 'data-mui-test': 'desktop-range-picker-end' }} | ||
| /> | ||
| </> | ||
| )} | ||
| /> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,26 @@ | ||
| { | ||
| "build/dist/material-ui-pickers.esm.js": { | ||
| "bundled": 188938, | ||
| "minified": 101335, | ||
| "gzipped": 26402, | ||
| "bundled": 189105, | ||
| "minified": 101411, | ||
| "gzipped": 26429, | ||
| "treeshaked": { | ||
| "rollup": { | ||
| "code": 83375, | ||
| "code": 83443, | ||
| "import_statements": 2121 | ||
| }, | ||
| "webpack": { | ||
| "code": 92851 | ||
| "code": 92919 | ||
| } | ||
| } | ||
| }, | ||
| "build/dist/material-ui-pickers.umd.js": { | ||
| "bundled": 299832, | ||
| "minified": 116954, | ||
| "gzipped": 33374 | ||
| "bundled": 300017, | ||
| "minified": 117022, | ||
| "gzipped": 33387 | ||
| }, | ||
| "build/dist/material-ui-pickers.umd.min.js": { | ||
| "bundled": 258486, | ||
| "minified": 107815, | ||
| "gzipped": 30601 | ||
| "bundled": 258671, | ||
| "minified": 107883, | ||
| "gzipped": 30626 | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
import * as React from 'react';