import { IEitherOption, IReactOption, IReactOptionGroup } from '@wowpic/xform-types'; import moment, { Moment } from 'moment'; import ReactDOM from 'react-dom'; export const withBatch = (callback: () => any) => ReactDOM.unstable_batchedUpdates(callback); export const optionsCtor = (options: IEitherOption[] = []): IReactOption[] => options.map( (option) => { if (typeof option === 'string') { return { label: option, value: option, disabled: false }; } const { name, label, value, ...props } = option; return { label: name || label, value, ...props }; } ); export const covertOptionGroup = ( options: IReactOption[] ): IReactOptionGroup[] => { const map = new Map(); options.forEach((item) => { const { group } = item; if (group !== undefined) { if (!map.has(group)) { map.set(group, []); } map.get(group).push(item); } else { if (!map.get('noGroup')) { map.set('noGroup', []); } map.get('noGroup').push(item); } }); const groups = []; for (const [key, value] of map) { groups.push({ groupLabel: key, options: value }); } return groups; }; export const getMomentTime: (time: any) => Moment = (time: any) => { if (!time) { return undefined; } if (typeof time === 'string' || typeof time === 'number') { return moment(new Date(time)); } const trueTime = time as Moment; return trueTime; };