Skip to content

Commit

Permalink
Better adapter package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JCQuintas committed Apr 16, 2024
1 parent cbda69a commit 4a740d1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import isWithinInterval from 'date-fns/isWithinInterval';
import defaultLocale from 'date-fns/locale/en-US';
// @ts-ignore
import longFormatters from 'date-fns/_lib/format/longFormatters';
import { AdapterDateFnsBase } from '../AdapterDateFnsBase';
import { AdapterDateFnsBase } from './AdapterDateFnsBase';

type DateFnsLocale = typeof defaultLocale;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
/* eslint-disable class-methods-use-this */
import { AdapterFormats, AdapterOptions, MuiPickersAdapter } from '@mui/x-adapter-common';

import addDays from 'date-fns/addDays';
import addSeconds from 'date-fns/addSeconds';
import addMinutes from 'date-fns/addMinutes';
import addHours from 'date-fns/addHours';
import addWeeks from 'date-fns/addWeeks';
import addMonths from 'date-fns/addMonths';
import addYears from 'date-fns/addYears';
import endOfDay from 'date-fns/endOfDay';
import endOfWeek from 'date-fns/endOfWeek';
import endOfYear from 'date-fns/endOfYear';
import { format as dateFnsFormat, longFormatters } from 'date-fns/format';
import getDate from 'date-fns/getDate';
import getDaysInMonth from 'date-fns/getDaysInMonth';
import getHours from 'date-fns/getHours';
import getMinutes from 'date-fns/getMinutes';
import getMonth from 'date-fns/getMonth';
import getSeconds from 'date-fns/getSeconds';
import getMilliseconds from 'date-fns/getMilliseconds';
import getWeek from 'date-fns/getWeek';
import getYear from 'date-fns/getYear';
import isAfter from 'date-fns/isAfter';
import isBefore from 'date-fns/isBefore';
import isEqual from 'date-fns/isEqual';
import isSameDay from 'date-fns/isSameDay';
import isSameYear from 'date-fns/isSameYear';
import isSameMonth from 'date-fns/isSameMonth';
import isSameHour from 'date-fns/isSameHour';
import isValid from 'date-fns/isValid';
import dateFnsParse from 'date-fns/parse';
import setDate from 'date-fns/setDate';
import setHours from 'date-fns/setHours';
import setMinutes from 'date-fns/setMinutes';
import setMonth from 'date-fns/setMonth';
import setSeconds from 'date-fns/setSeconds';
import setMilliseconds from 'date-fns/setMilliseconds';
import setYear from 'date-fns/setYear';
import startOfDay from 'date-fns/startOfDay';
import startOfMonth from 'date-fns/startOfMonth';
import endOfMonth from 'date-fns/endOfMonth';
import startOfWeek from 'date-fns/startOfWeek';
import startOfYear from 'date-fns/startOfYear';
import isWithinInterval from 'date-fns/isWithinInterval';
import enUS from 'date-fns/locale/en-US';
import { Locale as DateFnsLocale } from 'date-fns/locale';

import { AdapterDateFnsBase } from '../AdapterDateFnsBase';
import {
addDays,
addHours,
addMinutes,
addMonths,
addSeconds,
addWeeks,
addYears,
endOfDay,
endOfMonth,
endOfWeek,
endOfYear,
format as dateFnsFormat,
// @ts-ignore
longFormatters,
getDate,
getDaysInMonth,
getHours,
getMilliseconds,
getMinutes,
getMonth,
getSeconds,
getWeek,
getYear,
isAfter,
isBefore,
isEqual,
isSameDay,
isSameHour,
isSameMonth,
isSameYear,
isValid,
isWithinInterval,
Locale as DateFnsLocale,
parse as dateFnsParse,
setDate,
setHours,
setMilliseconds,
setMinutes,
setMonth,
setSeconds,
setYear,
startOfDay,
startOfMonth,
startOfWeek,
startOfYear,
} from 'date-fns';

import { enUS } from 'date-fns/locale';
import { AdapterDateFnsBase } from './AdapterDateFnsBase';

/**
* Based on `@date-io/date-fns`
Expand Down Expand Up @@ -74,7 +78,7 @@ import { AdapterDateFnsBase } from '../AdapterDateFnsBase';
* SOFTWARE.
*/
export class AdapterDateFns
extends AdapterDateFnsBase<DateFnsLocale>
extends AdapterDateFnsBase
// @ts-ignore we fix date typings in the x-date-picker package.
implements MuiPickersAdapter<Date, DateFnsLocale>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
/* eslint-disable class-methods-use-this */

import {
AdapterFormats,
AdapterOptions,
DateBuilderReturnType,
FieldFormatTokenMap,
MuiPickersAdapter,
} from '@mui/x-adapter-common';
import { Locale as DateFnsLocale } from 'date-fns';

export type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;

type DateFnsLocaleBase = {
formatLong?: {
date?: any;
time?: any;
dateTime?: any;
};
code?: string;
};

const formatTokenMap: FieldFormatTokenMap = {
// Year
y: { sectionType: 'year', contentType: 'digit', maxLength: 4 },
Expand Down Expand Up @@ -113,10 +104,7 @@ const defaultFormats: AdapterFormats = {
keyboardDateTime24h: 'P HH:mm',
};

type DateFnsAdapterBaseOptions<DateFnsLocale extends DateFnsLocaleBase> = MakeRequired<
AdapterOptions<DateFnsLocale, never>,
'locale'
> & {
type DateFnsAdapterBaseOptions = MakeRequired<AdapterOptions<DateFnsLocale, never>, 'locale'> & {
longFormatters: Record<'p' | 'P', (token: string, formatLong: any) => string>;
};

Expand Down Expand Up @@ -145,7 +133,7 @@ type DateFnsAdapterBaseOptions<DateFnsLocale extends DateFnsLocaleBase> = MakeRe
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export class AdapterDateFnsBase<DateFnsLocale extends DateFnsLocaleBase>
export class AdapterDateFnsBase
implements
Pick<
// @ts-ignore we fix date typings in the x-date-picker package.
Expand Down Expand Up @@ -175,9 +163,9 @@ export class AdapterDateFnsBase<DateFnsLocale extends DateFnsLocaleBase>

public escapedCharacters = { start: "'", end: "'" };

public longFormatters: DateFnsAdapterBaseOptions<DateFnsLocale>['longFormatters'];
public longFormatters: DateFnsAdapterBaseOptions['longFormatters'];

constructor(props: DateFnsAdapterBaseOptions<DateFnsLocale>) {
constructor(props: DateFnsAdapterBaseOptions) {
const { locale, formats, longFormatters } = props;
this.locale = locale;
this.formats = { ...defaultFormats, ...formats };
Expand Down

0 comments on commit 4a740d1

Please sign in to comment.