Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing doesn't permit arrays when supplying style overrides #527

Closed
stevebutler2210 opened this issue Nov 10, 2023 · 2 comments · Fixed by #528
Closed

Typing doesn't permit arrays when supplying style overrides #527

stevebutler2210 opened this issue Nov 10, 2023 · 2 comments · Fixed by #528

Comments

@stevebutler2210
Copy link
Contributor

Describe the bug

The PickerStyle type definition doesn't allow for arrays of style objects to be passed for any of the properties, requiring unnecessary use of StyleSheet.flatten() to satisfy the typings. Passing arrays of style objects is valid syntax (thanks to RN StyleProp), and behaves as expected if you ignore the type errors—use of flatten shouldn't be necessary here unless the situation strictly requires it (to flatten nesting etc).

Proposed solution would be to amend the typing of the PickerStyle interface to use e.g. StyleProp<ViewStyle> instead of ViewStyle.

To Reproduce

Pass an array of ViewStyle objects into the style.inputAndroidContainer prop.

Simple reproduction can be seen with the following implementation, or on the snack linked below:

import React from "react";
import type { StyleProp, ViewStyle } from "react-native";
import RNPickerSelect from "react-native-picker-select";

interface DropdownProps {
  value: string;
  options: string[];
  editable?: boolean;
}

export const Dropdown: React.FC<DropdownProps> = ({
  value,
  options,
  editable,
}) => {
  const $customInputWrapperStyles: StyleProp<ViewStyle> = [
    $inputWrapperStyles,
    !editable && $inputWrapperDisabledStyles,
  ]

  return (
    <RNPickerSelect
      value={value}
      items={options.map((option) => ({ label: option, value: option }))}
      disabled={!editable}
      style={{
        inputAndroidContainer: $customInputWrapperStyles,
      }}
    />
  );
};

const $inputWrapperStyles: ViewStyle = {
  borderWidth: 1,
  borderRadius: 100,
  borderColor: "black",
  backgroundColor: "white",
};

const $inputWrapperDisabledStyles: ViewStyle = {
  backgroundColor: "gray",
};

The styles work, but a type error is raised.

Expected behavior

As the style syntax is valid, no type error should be raised.

Screenshots

Screenshot 2023-11-10 at 17 31 01

Additional details

  • react-native-picker-select version: 8.1.0
  • react-native version: 0.72.4
  • expo sdk version: 49

Reproduction and/or code sample

https://snack.expo.dev/@stevebutlerkyan/rnpicker-styleprop-type-error-example

@lfkwtz
Copy link
Collaborator

lfkwtz commented Nov 14, 2023

Interested in contributing a fix?

@stevebutler2210
Copy link
Contributor Author

@lfkwtz 💯 just wanted to make sure the issue was agreed upon first! If we're on the same page, I'll try and get something raised later today 👍

stevebutler2210 added a commit to stevebutler2210/react-native-picker-select that referenced this issue Nov 14, 2023
The previous typings for the `style` prop were restrictive, preventing valid passing of arrays of styles to each of the attributes.

Updating these to use `StyleProp<XStyle>` ensures valid arrays of style objects are accepted.

Fixes: lawnstarter#527
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants