react-md中如何将EditDialogColumn的文本框替换为DatePicker
Fix: Replace EditDialogColumn's Default Input with DatePicker in react-md DataTable
Got it, let's get this sorted—you want to swap out the plain text input in EditDialogColumn for a DatePicker so users can select dates instead of typing them manually. Here's exactly how to do it:
Key Concept
The EditDialogColumn in react-md supports custom children, which means you can pass the DatePicker directly inside it instead of relying on the default text input. We just need to manage the date value state and sync it properly between the table display and the picker.
Modified Code Example
import React, { useState } from 'react'; import { DataTable, TableBody, TableRow, TableColumn, EditDialogColumn, DatePicker } from 'react-md'; const EditDialogExample = () => { // Track the selected date with state const [startDate, setStartDate] = useState(null); const handleDateUpdate = (selectedDate) => { setStartDate(selectedDate); // Replace this with your existing data update logic // Make sure to adjust for functional component (no `this` keyword) // handleInputChanges(values[0], selectedDate, 'tripStartDate'); }; return ( <DataTable baseId="edit-dialog-example" fullWidth={false} className="data-tables__edit-table-example" > <TableBody> <TableRow> <TableColumn> <EditDialogColumn label="Type Start Date" helpText="Select a date from the picker" // Display the date in YYYY-MM-DD format in the table value={startDate ? startDate.toISOString().split('T')[0] : ''} > {/* Your custom DatePicker component */} <DatePicker id="appointment-date-auto" label="Type Start Date" className="md-cell" value={startDate} onChange={handleDateUpdate} /> </EditDialogColumn> </TableColumn> </TableRow> </TableBody> </DataTable> ); }; export default EditDialogExample;
What Changed & Why
- Added State Management: We use
useStateto keep track of the selected date. This ensures the table display and DatePicker stay in sync. - Custom Child for EditDialogColumn: The
DatePickeris now a direct child ofEditDialogColumn, so clicking the edit button opens the picker instead of a text input. - Formatted Display Value: The
EditDialogColumn'svalueconverts the Date object to aYYYY-MM-DDstring—this is a clean, standard format for table display and matches common backend storage needs. - Updated Change Handler:
handleDateUpdateupdates the local state and connects to your existing data update logic (just adjust the commented line to fit your app's data flow).
Quick Notes
- If you need a default date, initialize the state with a Date object:
useState(new Date('2017-10-28')) - If your
valuesarray comes from a parent component, pass it (and any update functions) as props to this component (since functional components don't usethis) - The DatePicker's format can be adjusted using react-md's date formatting props if you need a different display style
内容的提问来源于stack exchange,提问作者Owais
相关产品推荐
相关产品推荐

