UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

54 lines (53 loc) 2.36 kB
import React from 'react'; /** * withCamelCaseProps is a HOC for function components * it will return a React Component where all snake_case props gets converted to camelCase * * Use the same for TypeScript types by using: ToCamelCase * * @param Base the original function or class * @returns extended function or class */ export declare function withCamelCaseProps<TBase, P>(Base: React.FunctionComponent<P> & TBase): typeof Base; /** * withCamelCaseProps is a HOC for classes * it will return a React Component where all snake_case props gets converted to camelCase * * Use the same for TypeScript types by using: ToCamelCase * * @param Base the original function or class * @returns extended function or class */ export declare function classWithCamelCaseProps<TBase extends React.ComponentClass>(Base: TBase): typeof Base; /** * Converts camel case props to snake case props. * * @template P - The type of the props object. * @param {P} props - The props object to convert. * @returns {P} - The converted props object. */ export declare function convertCamelCaseProps<P>(props: P): P; /** * Convert recursively Types from snake_case to camelCase * * Use it like so: * OriginalProps & ToCamelCase<OriginalProps> * * Disclaimer: Be careful using these with required props * - ToCamelCase makes the required snake_case props also required in camelCase * - ToCamelCasePartial removes required for the camelCase props * - ToCamelCaseFlat is like ToCamelCase, but it will not convert nested objects for performance boost * - ToCamelCasePartialFlat is like ToCamelCasePartial, but it will not convert nested objects for performance boost */ export type ToCamelCasePartial<T> = Partial<ToCamelCase<T>>; export type ToCamelCase<T> = T extends object ? { [K in keyof T as ConvertSnakeToCamelCase<K & string>]: T[K] | ToCamelCase<T[K]>; } : T; export type IncludeCamelCase<T> = Partial<T> & ToCamelCasePartial<T>; type ConvertSnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${T}${Capitalize<ConvertSnakeToCamelCase<U>>}` : S; type ConvertObjectKeysToCamelCase<T> = { [K in keyof T as ConvertSnakeToCamelCase<K & string>]: T[K]; }; export type ToCamelCaseFlat<T> = T extends object ? ConvertObjectKeysToCamelCase<T> : T; export type ToCamelCasePartialFlat<T> = Partial<ToCamelCaseFlat<T>>; export {};