Skip to content

Commit

Permalink
fix error on passing array as style & correctly preprocess paddingHor…
Browse files Browse the repository at this point in the history
…izontal style
  • Loading branch information
robertKozik committed Jan 30, 2024
1 parent 5267e84 commit 93c3caa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ try {
throw new Error('[react-native-live-markdown] Function `createReactDOMStyle` from react-native-web not found. Please make sure that you are using React Native Web 0.18 or newer.');
}

let preprocessStyle: (style: any) => any;
try {
preprocessStyle =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('react-native-web/dist/exports/StyleSheet/preprocess').default;
} catch (e) {
throw new Error('[react-native-live-markdown] Function `preprocessStyle` from react-native-web not found.');
}

type MarkdownStyle = MarkdownTextInputDecoratorViewNativeComponent.MarkdownStyle;

interface MarkdownTextInputProps extends TextInputProps {
Expand Down Expand Up @@ -388,7 +397,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
style && {
caretColor: (style as TextStyle).color || 'black',
},
createReactDOMStyle(style),
createReactDOMStyle(preprocessStyle(StyleUtils.parseStyleArrayToObject(style))),
]) as CSSProperties
}
role={accessibilityRole || 'textbox'}
Expand Down
17 changes: 16 additions & 1 deletion src/styleUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {processColor, Platform} from 'react-native';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import type * as MarkdownTextInputDecoractorView from './MarkdownTextInputDecoratorViewNativeComponent';

type MarkdownStyle = MarkdownTextInputDecoractorView.MarkdownStyle;
Expand Down Expand Up @@ -93,10 +94,24 @@ function processColorsInMarkdownStyle(input: MarkdownStyle): MarkdownStyle {
return output as MarkdownStyle;
}

function parseStyleArrayToObject<T extends TextStyle | ViewStyle | object>(style: StyleProp<T>): T {
const isArray = Array.isArray(style);
if (!isArray) {
return style as T;
}
return style.reduce((acc, val) => {
const isNestedArray = Array.isArray(val);
if (isNestedArray) {
return {...acc, ...parseStyleArrayToObject(val)};
}
return {...acc, ...val};
}, {});
}

function processMarkdownStyle(input: PartialMarkdownStyle | undefined): MarkdownStyle {
return processColorsInMarkdownStyle(mergeMarkdownStyleWithDefault(input));
}

export type {PartialMarkdownStyle};

export {processMarkdownStyle, mergeMarkdownStyleWithDefault};
export {processMarkdownStyle, mergeMarkdownStyleWithDefault, parseStyleArrayToObject};

0 comments on commit 93c3caa

Please sign in to comment.