You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Added State Management: We use useState to keep track of the selected date. This ensures the table display and DatePicker stay in sync.
  2. Custom Child for EditDialogColumn: The DatePicker is now a direct child of EditDialogColumn, so clicking the edit button opens the picker instead of a text input.
  3. Formatted Display Value: The EditDialogColumn's value converts the Date object to a YYYY-MM-DD string—this is a clean, standard format for table display and matches common backend storage needs.
  4. Updated Change Handler: handleDateUpdate updates 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 values array comes from a parent component, pass it (and any update functions) as props to this component (since functional components don't use this)
  • The DatePicker's format can be adjusted using react-md's date formatting props if you need a different display style

内容的提问来源于stack exchange,提问作者Owais

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:32:11