react-native-picker 不会在编辑表单上填充数据

发布于 2025-01-16 21:46:29 字数 3635 浏览 0 评论 0原文

我正在使用 react-native-web ,对于 Native 端,我正在使用 import { Picker } from '@react-native-picker/picker'

I' m 使用可重用的表单模式进行创建和编辑。在网络上,数据填充得很好,但例如在 iOS 上,下拉菜单设置不正确。我创建了一个可重用的组件,它的子组件是react-native-picker/picker。例如,我在表单安装上设置状态,如下所示:

 const [languageAbility, setLanguageAbility] = useState<number>(
    childProfile?.languageAbility?.id || 0,
  )
  const [grade, setGrade] = useState<number>(childProfile?.grade?.id || 0)
 

在我的组件上,它们的设置如下:

 <AccessibleSelect
              selectedValue={grade}
              options={childMenusData?.menus?.schoolGrades.map((sg) => ({
                label: sg.title,
                value: sg?.id,
              }))}
              onValueChange={(val: number) => setGrade(val)}
              label={t('ChildForms.Grade')}
              testID="grade"
              inputStyle={styles.selectStyles}
              tooltipText={isMobile ? 'What is a grade?' : ''}
              tooltipContent={t('ChildForms.WhatIsAGradeContent')}
              tooltipPlacement={'bottom'}
            />
          </View>
          {!isMobile && (
            <View style={styles.inputContainerHalf}>
              <Tooltip
                text="What is a grade?"
                content={t('ChildForms.WhatIsAGradeContent')}
                placement={'right'}
              />
            </View>
          )}
        </View>
        <View
          style={{
            flexDirection: isMobile ? 'column' : 'row',
            width: '100%',
            margin: 0,
          }}
        >
          <View
            style={[
              styles.inputContainerHalf,
              { marginRight: isMobile ? 0 : 30 },
            ]}
          >
            <AccessibleSelect
              selectedValue={languageAbility}
              options={childMenusData?.menus?.languageAbilities.map((la) => ({
                label: la.title,
                value: la.id,
              }))}
              onValueChange={(val: number) => setLanguageAbility(val)}
              label={t('ChildForms.LanguageAbility')}
              testID="languageAbility"
              additionalContainerStyles={{ paddingTop: 0, paddingBottom: 0 }}
              inputStyle={styles.selectStyles}
            />
          </View>
        </View>

实际的 Picker 组件(AccessibleSelect)如下所示:




  return (
    <View style={[styles.container, additionalContainerStyles]}>
 
      <Picker
        ref={refToAttachToInput}
        style={[
          Platform.OS === 'ios'
            ? styles.pickerIOS
            : [
                styles.picker,
                { borderColor, borderStyle: 'solid', borderWidth: 1 },
              ],
          inputStyle,
        ]}
        selectedValue={selectedValue}
        onValueChange={onValueChange}
        defaultValue={defaultValue && defaultValue}
        itemStyle={styles.pickerText}
        mode={isAndroid && 'dropdown'}
      >
        {options.map((o) => (
          <Picker.Item label={o.label} key={o.value} value={o.value} />
        ))}
      </Picker>

      <View
        style={[
          styles.bottomContainer,
          { justifyContent: fieldError ? 'space-between' : 'flex-end' },
        ]}
      >
        {!!fieldError && (
          <Text accessibilityLiveRegion="assertive" style={styles.errorText}>
            {fieldError}
          </Text>
        )}
      </View>
    </View>
  )
}



I'm using react-native-web and for the Native side, I'm using import { Picker } from '@react-native-picker/picker'

I'm using a reusable form modal for create and edit. On web, the data populates just fine but on iOS for example, the drop downs don't get set correctly. I've created a reusable component that has react-native-picker/picker as it's child component. So for example I set state on form mount like this:

 const [languageAbility, setLanguageAbility] = useState<number>(
    childProfile?.languageAbility?.id || 0,
  )
  const [grade, setGrade] = useState<number>(childProfile?.grade?.id || 0)
 

and on my components, they are set up like this:

 <AccessibleSelect
              selectedValue={grade}
              options={childMenusData?.menus?.schoolGrades.map((sg) => ({
                label: sg.title,
                value: sg?.id,
              }))}
              onValueChange={(val: number) => setGrade(val)}
              label={t('ChildForms.Grade')}
              testID="grade"
              inputStyle={styles.selectStyles}
              tooltipText={isMobile ? 'What is a grade?' : ''}
              tooltipContent={t('ChildForms.WhatIsAGradeContent')}
              tooltipPlacement={'bottom'}
            />
          </View>
          {!isMobile && (
            <View style={styles.inputContainerHalf}>
              <Tooltip
                text="What is a grade?"
                content={t('ChildForms.WhatIsAGradeContent')}
                placement={'right'}
              />
            </View>
          )}
        </View>
        <View
          style={{
            flexDirection: isMobile ? 'column' : 'row',
            width: '100%',
            margin: 0,
          }}
        >
          <View
            style={[
              styles.inputContainerHalf,
              { marginRight: isMobile ? 0 : 30 },
            ]}
          >
            <AccessibleSelect
              selectedValue={languageAbility}
              options={childMenusData?.menus?.languageAbilities.map((la) => ({
                label: la.title,
                value: la.id,
              }))}
              onValueChange={(val: number) => setLanguageAbility(val)}
              label={t('ChildForms.LanguageAbility')}
              testID="languageAbility"
              additionalContainerStyles={{ paddingTop: 0, paddingBottom: 0 }}
              inputStyle={styles.selectStyles}
            />
          </View>
        </View>

And the actual Picker component (AccessibleSelect) looks like this:




  return (
    <View style={[styles.container, additionalContainerStyles]}>
 
      <Picker
        ref={refToAttachToInput}
        style={[
          Platform.OS === 'ios'
            ? styles.pickerIOS
            : [
                styles.picker,
                { borderColor, borderStyle: 'solid', borderWidth: 1 },
              ],
          inputStyle,
        ]}
        selectedValue={selectedValue}
        onValueChange={onValueChange}
        defaultValue={defaultValue && defaultValue}
        itemStyle={styles.pickerText}
        mode={isAndroid && 'dropdown'}
      >
        {options.map((o) => (
          <Picker.Item label={o.label} key={o.value} value={o.value} />
        ))}
      </Picker>

      <View
        style={[
          styles.bottomContainer,
          { justifyContent: fieldError ? 'space-between' : 'flex-end' },
        ]}
      >
        {!!fieldError && (
          <Text accessibilityLiveRegion="assertive" style={styles.errorText}>
            {fieldError}
          </Text>
        )}
      </View>
    </View>
  )
}



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文