Skip to content

Commit

Permalink
Реорганизация внутренней структуры (#175)
Browse files Browse the repository at this point in the history
* fix(party_kz): fixes typo

* refactor(#174): reorganizes folder structure and types
  • Loading branch information
vitalybaev authored Jan 4, 2025
1 parent d1e7686 commit 2304838
Show file tree
Hide file tree
Showing 31 changed files with 501 additions and 478 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile --ignore-scripts
- name: Run tests
run: pnpm --filter=react-dadata test:lint
run: pnpm test:lint

2 changes: 1 addition & 1 deletion .github/workflows/size-limit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile --ignore-scripts
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ jobs:
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile --ignore-scripts
- name: Type Check
run: pnpm --filter react-dadata test:type-check
- name: Run tests
run:
pnpm --filter=react-dadata test
pnpm test
- name: Coveralls
uses: coverallsapp/github-action@v2
with:
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"bugs": {
"url": "https://github.com/vitalybaev/react-dadata/issues"
},
"scripts": {
"test": "pnpm --filter './packages/**' --if-present test",
"test:lint": "pnpm --filter './packages/**' test:lint-package"
},
"homepage": "https://vitalybaev.github.io/react-dadata/",
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"serve": "vite preview"
"serve": "vite preview",
"test:lint-package": "biome check src/"
},
"dependencies": {
"react": "^18.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"build:ci": "NODE_ENV=production pnpm run '/^(clean|build:css:ci|build:cjs|build:esm|build:types)$/'",
"build": "NODE_ENV=production pnpm run '/^(clean|build:css|build:cjs|build:esm|build:types)$/'",
"size-build": "pnpm build:esm",
"test:lint": "biome check src/ example/",
"test:lint-package": "biome check src/",
"test:size-limit": "pnpm build:esm && size-limit",
"test:type-check": "tsc --noEmit",
"test": "vitest --coverage",
"test:once": "vitest run --coverage",
"vitest-preview": "vitest-preview"
Expand Down
1 change: 1 addition & 0 deletions packages/react-dadata/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="@testing-library/jest-dom/vitest" />
import '@testing-library/jest-dom/vitest';
import { cleanup } from '@testing-library/react';
import { afterEach } from 'vitest';
Expand Down
12 changes: 6 additions & 6 deletions packages/react-dadata/src/__tests__/AddressSuggestions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { http, HttpResponse } from 'msw';
import { type SetupServerApi, setupServer } from 'msw/node';
import React, { createRef, forwardRef, type HTMLProps, type ReactNode } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { debug } from 'vitest-preview';
import { AddressSuggestions } from '../AddressSuggestions';
// import { debug } from 'vitest-preview';
import * as requestModule from '../request';
import type { DaDataAddress, DaDataSuggestion } from '../types';
import { AddressSuggestions } from '../variants/address/address-suggestions';
import type { DaDataAddressSuggestion } from '../variants/address/address-types';
import { addressMockKrasnodar, addressMocks, createAddressMock, mockedRequestCalls } from './mocks';

type RequestLog = {
Expand Down Expand Up @@ -387,7 +387,7 @@ describe('AddressSuggestions', () => {
});

it('correctly renders with renderOption', async () => {
const renderOption: (suggestion: DaDataSuggestion<DaDataAddress>) => ReactNode = (suggestion) => {
const renderOption: (suggestion: DaDataAddressSuggestion) => ReactNode = (suggestion) => {
return <span className="test-class">RenderOption {suggestion.data.country}</span>;
};

Expand Down Expand Up @@ -418,8 +418,8 @@ describe('AddressSuggestions', () => {
});

it('passes current input value to renderOption', async () => {
const renderOption = vi.fn<(suggestion: DaDataSuggestion<DaDataAddress>, query: string) => ReactNode>(
(suggestion: DaDataSuggestion<DaDataAddress>): ReactNode => {
const renderOption = vi.fn<(suggestion: DaDataAddressSuggestion, query: string) => ReactNode>(
(suggestion: DaDataAddressSuggestion): ReactNode => {
return suggestion.value;
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { http, HttpResponse } from 'msw';
import { type SetupServerApi, setupServer } from 'msw/node';
import React from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { PartySuggestions } from '../PartySuggestions';
import { PartySuggestions } from '../variants/party_russia/party-russia-suggestions';
import { partyMocks } from './mocks';

type RequestLog = {
Expand Down
9 changes: 5 additions & 4 deletions packages/react-dadata/src/__tests__/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { RequestOptions } from '../request';
import type { DaDataAddress, DaDataParty, DaDataSuggestion } from '../types';
import type { DaDataAddressSuggestion } from '../variants/address/address-types';
import type { DaDataPartySuggestion } from '../variants/party_russia/party-russia-types';

export const addressMockKrasnodar: DaDataSuggestion<DaDataAddress> = {
export const addressMockKrasnodar: DaDataAddressSuggestion = {
value: 'Краснодарский край, Мостовский р-н',
unrestricted_value: 'Краснодарский край, Мостовский р-н',
data: {
Expand Down Expand Up @@ -100,7 +101,7 @@ export const addressMockKrasnodar: DaDataSuggestion<DaDataAddress> = {
},
};

export const addressMocks: Record<string, DaDataSuggestion<DaDataAddress>[]> = {
export const addressMocks: Record<string, DaDataAddressSuggestion[]> = {
М: [
{
value: 'г Москва',
Expand Down Expand Up @@ -1965,7 +1966,7 @@ export const createAddressMock =
}, wait);
};

export const partyMocks: Record<string, DaDataSuggestion<DaDataParty>[]> = {
export const partyMocks: Record<string, DaDataPartySuggestion[]> = {
с: [
{
value: 'САМАРСКИЙ УНИВЕРСИТЕТ',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { debounce } from 'debounce';
import { nanoid } from 'nanoid';
import React, { type ChangeEvent, type MouseEvent, type FocusEvent, type ReactNode, type ElementType } from 'react';
import shallowEqual from 'shallowequal';
import type { CommonProps, DaDataSuggestion } from './core-types';
import { DefaultHttpCache, HttpCache } from './http-cache';
import { makeRequest } from './request';
import type { CommonProps, DaDataSuggestion } from './types';

export type BaseProps<SuggestionType> = CommonProps<SuggestionType>;

Expand Down
49 changes: 49 additions & 0 deletions packages/react-dadata/src/core-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { ElementType, HTMLProps, ReactNode } from 'react';
import type { HttpCache } from './http-cache';

export type Nullable<T> = T | null;

export interface DaDataSuggestion<T> {
value: string;
unrestricted_value: string;
data: T;
}

/**
* Общие пропсы для всех видов компонента подсказов
*/
export interface CommonProps<SuggestionType> {
token: string;
value?: DaDataSuggestion<SuggestionType>;
url?: string;
defaultQuery?: string;
autoload?: boolean;
delay?: number;
count?: number;
onChange?: (suggestion?: DaDataSuggestion<SuggestionType>) => void;
inputProps?: HTMLProps<HTMLInputElement>;
hintText?: ReactNode;
renderOption?: (suggestion: DaDataSuggestion<SuggestionType>, inputValue: string) => ReactNode;
renderNoSuggestions?: () => ReactNode;
containerClassName?: string;
suggestionsClassName?: string;
suggestionClassName?: string;
currentSuggestionClassName?: string;
hintClassName?: string;
highlightClassName?: string;
minChars?: number;
customInput?: ElementType;
selectOnBlur?: boolean;
uid?: string;
/**
* Необходимо ли кешировать HTTP-запросы?
* Возможно передать собственный кеш наследующий {@link HttpCache}.
*/
httpCache?: boolean | HttpCache;
/**
* Время жизни кеша в миллисекундах.
* Игнорируется если был передан собственный {@link HttpCache}.
*/
httpCacheTtl?: number;
children?: ReactNode | undefined;
}
130 changes: 58 additions & 72 deletions packages/react-dadata/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,65 @@
import { AddressSuggestions } from './AddressSuggestions';
import { BankSuggestions } from './BankSuggestions';
import { EmailSuggestions } from './EmailSuggestions';
import { FioSuggestions } from './FioSuggestions';
import { PartySuggestions } from './PartySuggestions';
import type {
DaDataAddress,
DaDataAddressBounds,
DaDataBank,
DaDataBankStatus,
DaDataBankType,
DaDataEmail,
DaDataFio,
DaDataGender,
DaDataParty,
DaDataPartyBelarus,
DaDataPartyBelarusStatus,
DaDataPartyBranchType,
/**
* Общие типы
*/
export type { DaDataSuggestion } from './core-types';
export { HttpCache } from './http-cache';

/**
* Адреса
*/
export type { DaDataAddress, DaDataAddressBounds, DaDataAddressSuggestion } from './variants/address/address-types';
export { AddressSuggestions } from './variants/address/address-suggestions';

/**
* Организации в России 🇷🇺
*/
export type {
DaDataPartyRussia,
DaDataPartyRussiaStatus,
DaDataPartyStatus,
DaDataPartyType,
DaDataSuggestion,
} from './types';
import { PartyBelarusSuggestions } from './variants/party_belarus/party-belarus';
import { PartyKazakhstanSuggestions } from './variants/party_kazakhstan/party-kazakhstan';
import type {
DaDataPartyStatus,
DaDataPartyBranchType,
DaDataPartyRussiaFio,
DaDataPartyRussiaSuggestion,
DaDataPartySuggestion,
} from './variants/party_russia/party-russia-types';
export { PartySuggestions } from './variants/party_russia/party-russia-suggestions';

/**
* Организации в Беларуси 🇧🇾
*/
export type {
DaDataPartyBelarus,
DaDataPartyBelarusType,
DaDataPartyBelarusStatus,
DaDataPartyBelarusSuggestion,
} from './variants/party_belarus/party-belarus-types';
export { PartyBelarusSuggestions } from './variants/party_belarus/party-belarus-suggestions';

/**
* Организации в Казахстане 🇰🇿
*/
export type {
DaDataPartyKazakhstan,
DaDataPartyKazakhstanStatus,
DaDataPartyKazakhstanSuggestion,
DaDataPartyKazakhstanType,
DaDataPartyKazakhstanStatus,
} from './variants/party_kazakhstan/party-kazakhstan-types';
export { PartyKazakhstanSuggestions } from './variants/party_kazakhstan/party-kazakhstan-suggestions';

type DaDataAddressSuggestion = DaDataSuggestion<DaDataAddress>;
type DaDataPartySuggestion = DaDataSuggestion<DaDataParty>;
type DaDataPartyRussiaSuggestion = DaDataSuggestion<DaDataPartyRussia>;
type DaDataPartyBelarusSuggestion = DaDataSuggestion<DaDataPartyBelarus>;
type DaDataPartyKazakhstanSuggestion = DaDataSuggestion<DaDataPartyKazakhstan>;
type DaDataBankSuggestion = DaDataSuggestion<DaDataBank>;
type DaDataFioSuggestion = DaDataSuggestion<DaDataFio>;
type DaDataEmailSuggestion = DaDataSuggestion<DaDataEmail>;
/**
* Банки
*/
export type { DaDataBank, DaDataBankStatus, DaDataBankType, DaDataBankSuggestion } from './variants/bank/bank-types';
export { BankSuggestions } from './variants/bank/bank-suggestions';

export {
type DaDataSuggestion,
AddressSuggestions,
PartySuggestions,
PartyBelarusSuggestions,
PartyKazakhstanSuggestions,
BankSuggestions,
FioSuggestions,
EmailSuggestions,
type DaDataAddress,
type DaDataAddressSuggestion,
type DaDataParty,
type DaDataPartySuggestion,
type DaDataPartyRussiaSuggestion,
type DaDataPartyBelarusSuggestion,
type DaDataPartyKazakhstanSuggestion,
type DaDataAddressBounds,
type DaDataPartyType,
type DaDataPartyRussia,
type DaDataPartyRussiaStatus,
type DaDataPartyBranchType,
type DaDataPartyStatus,
type DaDataPartyBelarus,
type DaDataPartyBelarusStatus,
type DaDataPartyKazakhstan,
type DaDataPartyKazakhstanStatus,
type DaDataPartyKazakhstanType,
type DaDataBank,
type DaDataBankStatus,
type DaDataBankType,
type DaDataBankSuggestion,
type DaDataFio,
type DaDataFioSuggestion,
type DaDataGender,
type DaDataEmail,
type DaDataEmailSuggestion,
};
export { HttpCache } from './http-cache';
/**
* ФИО
*/
export type { DaDataFio, DaDataFioSuggestion } from './variants/fio/fio-types';
export { FioSuggestions } from './variants/fio/fio-suggestions';

/**
* Email
*/
export type { DaDataEmail, DaDataEmailSuggestion } from './variants/email/email-types';
export { EmailSuggestions } from './variants/email/email-suggestions';
2 changes: 1 addition & 1 deletion packages/react-dadata/src/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DaDataSuggestion } from './core-types';
import type { HttpCache } from './http-cache';
import type { DaDataSuggestion } from './types';

export interface RequestOptions {
headers: { [header: string]: string };
Expand Down
Loading

0 comments on commit 2304838

Please sign in to comment.