首页 > 解决方案 > 如何使用 React Admin 从 TextInput 字段中的 React Rrule Generator 获取 onChange() 值

问题描述

我可以知道如何使用 React Admin 从 TextInput 字段中的 React Rrule Generator 获取 onChange() 值。

编码:

编码

输出示例:

输出示例

用户选择时返回的“重复”值: 这

<RRuleGenerator
  onChange={(recurrences) => console.log(`${recurrences}`)}
  config={{
    repeat: ['Monthly', 'Weekly'],
    yearly: 'on the',
    monthly: 'on',
    end: ['Never', 'On date'],
    weekStartsOnSunday: true,
    hideError: true,
   }}
  />
  <TextInput label="Recurrences" source="recurrences" />

我使用的 react-rrule-generator:https ://github.com/fafruch/react-rrule-generator

修改代码:

export const RACXICreate = (props) => {
const [state, setState] = useState('')
return (<Fragment>
        <RRuleGenerator
        onChange={(rrule) => setState({ rrule })}
         value={state.rrule}
         config={{
         repeat: ['Monthly', 'Weekly'],
         yearly: 'on the',
         monthly: 'on',
         end: ['Never', 'On date'],
         weekStartsOnSunday: true,
         hideError: true,
        }}
       />
       <TextInput label="Recurrences" source="recurrences" value={state.rrule} />
     </Fragment>)}

但是,如果我使用此代码,我能够获得值:

<input type="text" source="recurrences" label="Recurrences" value={state.rrule} />

在此处输入图像描述

但我主要想在 TextInput 字段中获取值。

标签: reactjsrecurrencereact-adminrrule

解决方案


const React, { useState, Fragment } from 'react'

  const x = () => {
    const [state, setState] = useEffect('')
    return (
      <Fragment>
        <RRuleGenerator
          onChange={val => setState(val)}
          config={{
            repeat: ['Monthly', 'Weekly'],
            yearly: 'on the',
            monthly: 'on',
            end: ['Never', 'On date'],
            weekStartsOnSunday: true,
            hideError: true,
          }}
        />
        <TextInput label="Recurrences" source="recurrences" value={state} />
      </Fragment>
    )
  }

使用这样的状态对象


推荐阅读