MyFrom文件夹 index.jsx () FromItem.jsx

    1. import React ,{useRef}from 'react'
    2. import style from './style.less'
    3. import FromItem from './FromItem'
    4. import { Form } from 'antd';
    5. const MyFrom = React.forwardRef((props,fromRef) => {
    6. const configurevalue = props.configurevalue||[]
    7. const onFinish = (values) => {
    8. props.onFinish(values)
    9. };
    10. const onFinishFailed = (errorInfo) => {
    11. props.onFinishFailed(errorInfo)
    12. };
    13. const handleFromItemChange =(value,form_key)=>{
    14. props.onChange(value,form_key)
    15. }
    16. return (
    17. <div className={style.From}>
    18. <Form
    19. onFinish={onFinish}
    20. onFinishFailed={onFinishFailed}
    21. autoComplete="off"
    22. ref={fromRef}
    23. {...props}
    24. >
    25. {configurevalue.map((_e)=>{
    26. return(
    27. <Form.Item
    28. label={_e.label}
    29. key={_e.form_key}
    30. name={_e.form_key}
    31. {..._e.configure}
    32. >
    33. <FromItem configurevalue={_e} onChange={handleFromItemChange} />
    34. </Form.Item>
    35. )
    36. })}
    37. </Form>
    38. </div>
    39. )
    40. })
    41. export default MyFrom
    1. import React from 'react'
    2. import { Input, Select, DatePicker, TimePicker } from 'antd';
    3. const { Option } = Select;
    4. const { RangePicker } = DatePicker;
    5. export default function FromItem(props) {
    6. const label = props.configurevalue.label
    7. const type = props.configurevalue.type
    8. const fromItemProps = props.configurevalue.fromItemProps
    9. const handleInputChange = (e) => {
    10. const value = e.target.value
    11. const form_key = props.configurevalue.form_key
    12. props.onChange(value, form_key)
    13. }
    14. const handleSelectChange = (e) => {
    15. const value = e
    16. const form_key = props.configurevalue.form_key
    17. props.onChange(value, form_key)
    18. }
    19. const handleDatePickerChange = (date, dateString) => {
    20. const value = dateString
    21. const form_key = props.configurevalue.form_key
    22. props.onChange(value, form_key)
    23. }
    24. const renderInput = () => {
    25. return <Input placeholder={`请输入${label}`} {...fromItemProps} onChange={handleInputChange} />
    26. }
    27. const renderSelect = () => {
    28. const data = fromItemProps?.optiondata ?? []
    29. return (
    30. <Select placeholder={`请选择${label}`} {...fromItemProps} onChange={handleSelectChange}>
    31. {data.map((e) => {
    32. return <Option value={e.value} key={e.value}>{e.label}</Option>
    33. })}
    34. </Select>
    35. )
    36. }
    37. const renderDatePicker = () => {
    38. return <DatePicker {...fromItemProps} onChange={handleDatePickerChange} />
    39. }
    40. const renderRangePicker = () => {
    41. return <RangePicker {...fromItemProps} onChange={handleDatePickerChange} />
    42. }
    43. const renderTimePicker = () => {
    44. return <TimePicker {...fromItemProps} onChange={handleDatePickerChange} />
    45. }
    46. const typeMap = {
    47. input: renderInput(),
    48. select: renderSelect(),
    49. DatePicker: renderDatePicker(),
    50. RangePicker: renderRangePicker(),
    51. TimePicker: renderTimePicker()
    52. }
    53. return (
    54. <>
    55. {typeMap[type]}
    56. </>
    57. )
    58. }