Skip to content

NumberInput - support theme and generify useThemeProps #2370

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

Merged
merged 1 commit into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commons/asBaseComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function asBaseComponent<PROPS, STATICS = {}>(WrappedComponent: React.ComponentT
: Modifiers.generateModifiersStyle(options.modifiersOptions, themeProps);
// TODO: omit original modifiers props (left, right, flex, etc..)
// Because they throws an error when being passed to RNView on Android
// @ts-expect-error
const {forwardedRef, ...others} = themeProps;
return (
(this.state.error && UIComponent.defaultProps?.renderError) || (
Expand Down
2 changes: 1 addition & 1 deletion src/commons/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export function extractComponentProps(component: any, props: Dictionary<any>, ig
}

//@ts-ignore
export function getThemeProps(props = this.props, context = this.context, componentDisplayName = '') {
export function getThemeProps<T extends object>(props: T = this.props, context = this.context, componentDisplayName = ''):T {
const componentName =
//@ts-ignore
componentDisplayName || this.displayName || this.constructor.displayName || this.constructor.name;
Expand Down
4 changes: 1 addition & 3 deletions src/components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ const Avatar = forwardRef<any, AvatarProps>((props: AvatarProps, ref: React.Forw
}, [_baseContainerStyle, imageStyle]);

const renderImage = () => {
const hasImage = !_.isUndefined(_source);

if (hasImage) {
if (_source !== undefined) {
// Looks like reanimated does not support SVG
const ImageContainer = animate && !isSvg(_source) ? AnimatedImage : Image;
return (
Expand Down
5 changes: 3 additions & 2 deletions src/components/numberInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isEmpty} from 'lodash';
import React, {useMemo, useCallback, useState, useEffect} from 'react';
import {StyleSheet, StyleProp, ViewStyle} from 'react-native';
import {useDidUpdate} from 'hooks';
import {useDidUpdate, useThemeProps} from 'hooks';
import TextField, {TextFieldProps} from '../../incubator/TextField';
import Text from '../text';
import {getInitialData, parseInput, generateOptions, Options, NumberInputData} from './Presenter';
Expand Down Expand Up @@ -47,6 +47,7 @@ export type NumberInputProps = React.PropsWithRef<
};

function NumberInput(props: NumberInputProps, ref: any) {
const themeProps = useThemeProps(props, 'NumberInput');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't you add generics to useThemeProps so you can send it here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did, but it works without it 🪄
Pretty sure that it gets the type from the input props and that's why I got the errors in Picker and Avatar

const {
onChangeNumber,
initialNumber,
Expand All @@ -60,7 +61,7 @@ function NumberInput(props: NumberInputProps, ref: any) {
trailingTextStyle,
placeholder,
...others
} = props;
} = themeProps;
const [options, setOptions] = useState<Options>(generateOptions(locale, fractionDigits));
const [data, setData] = useState<NumberInputData>();

Expand Down
1 change: 1 addition & 0 deletions src/components/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
disabled={themeProps.editable === false}
>
{renderPicker ? (
// @ts-expect-error - hopefully will be solved after the picker migration ends
renderPicker(value, label)
) : (
<TextFieldMigrator
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useThemeProps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {ThemeManager} from 'style';

const EmptyContext = createContext({});

const useThemeProps = (props: any, componentName: string) => {
const useThemeProps = <T extends object>(props: T, componentName: string) => {
// Note: This adds a dependency on current color scheme and update theme props accordingly
useColorScheme();
const themeContext = ThemeManager.getThemeContext();
Expand Down