react-native-sortables
Version:
Powerful Sortable Components for Flexible Content Reordering in React Native
54 lines (51 loc) • 1.66 kB
JavaScript
;
import { StyleSheet } from 'react-native';
import { DEFAULT_SHARED_PROPS, STYLE_PROPS } from '../constants/props';
const hasStyleProp = (styleKey, props) => {
return styleKey in props;
};
export const getPropsWithDefaults = (props, componentDefaultProps) => {
const propsWithDefaults = {
...DEFAULT_SHARED_PROPS,
...componentDefaultProps
};
for (const key in props) {
if (props[key] !== undefined) {
propsWithDefaults[key] = props[key];
}
}
// merge styles from props and defaults
for (const styleKey of STYLE_PROPS) {
if (hasStyleProp(styleKey, propsWithDefaults)) {
const style = {
...(hasStyleProp(styleKey, DEFAULT_SHARED_PROPS) && DEFAULT_SHARED_PROPS[styleKey]),
...(hasStyleProp(styleKey, componentDefaultProps) && componentDefaultProps[styleKey])
};
const propsStyle = hasStyleProp(styleKey, props) ? StyleSheet.flatten(props[styleKey]) : {};
// Only override defaultStyle with defined values from propsStyle
Object.entries(propsStyle).forEach(([key, value]) => {
if (value !== undefined) {
// @ts-expect-error This is fine
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
style[key] = value;
}
});
propsWithDefaults[styleKey] = style;
}
}
const sharedProps = {};
const rest = {};
for (const key in propsWithDefaults) {
const k = key;
if (k in DEFAULT_SHARED_PROPS) {
sharedProps[key] = propsWithDefaults[k];
} else {
rest[key] = propsWithDefaults[k];
}
}
return {
rest,
sharedProps
};
};
//# sourceMappingURL=props.js.map