react-native-sortables
Version:
Powerful Sortable Components for Flexible Content Reordering in React Native
41 lines (38 loc) • 1.45 kB
JavaScript
;
import { useMemo } from 'react';
import { StyleSheet } from 'react-native';
import { DEFAULT_SHARED_PROPS } from '../constants';
const isDefaultValueGetter = value => {
return typeof value === 'object' && value !== null && 'get' in value;
};
export default function usePropsWithDefaults(props, componentDefaultProps) {
const keys = new Set([...Object.keys(componentDefaultProps), ...Object.keys(DEFAULT_SHARED_PROPS), ...Object.keys(props)]);
const propsWithDefaults = {};
// Merge user-defined props with defaults
for (const key of keys) {
const k = key;
if (props[k] !== undefined) {
propsWithDefaults[k] = props[k];
} else {
const defaultProp = componentDefaultProps[key] ?? DEFAULT_SHARED_PROPS[key];
propsWithDefaults[k] = isDefaultValueGetter(defaultProp) ? defaultProp.get() : defaultProp;
}
}
// merge drop indicator style from props and defaults
propsWithDefaults.dropIndicatorStyle = useMemo(() => {
const result = {
...DEFAULT_SHARED_PROPS.dropIndicatorStyle
};
const propsStyle = StyleSheet.flatten(props.dropIndicatorStyle ?? {});
for (const key in propsStyle) {
const k = key;
if (propsStyle[k] !== undefined) {
// @ts-expect-error This is fine
result[k] = propsStyle[k];
}
}
return result;
}, [props.dropIndicatorStyle]);
return propsWithDefaults;
}
//# sourceMappingURL=usePropsWithDefaults.js.map