Skip to content

Commit

Permalink
feat: add core-config (#1426)
Browse files Browse the repository at this point in the history
* feat: add core-config

* refactor: update components

* feat: publish core-config package

* fix: update snapshots

* test(textarea): fix tests

* Create pink-candles-protect.md

---------

Co-authored-by: core-ds-bot <[email protected]>
  • Loading branch information
reme3d2y and core-ds-bot authored Nov 18, 2024
1 parent 131972b commit 4bcff51
Show file tree
Hide file tree
Showing 95 changed files with 1,009 additions and 1,664 deletions.
15 changes: 15 additions & 0 deletions .changeset/pink-candles-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@alfalab/core-components-amount-input": patch
"@alfalab/core-components-button": patch
"@alfalab/core-components-calendar-input": patch
"@alfalab/core-components-calendar": patch
"@alfalab/core-components-checkbox-group": patch
"@alfalab/core-components-code-input": patch
"@alfalab/core-components-confirmation": patch
"@alfalab/core-components-custom-button": patch
"@alfalab/core-components-custom-picker-button": patch
"@alfalab/core-components-date-range-input": patch
"@alfalab/core-components-date-time-input": patch
---

Добавлен пакет @alfalab/core-config для глобальных настроек библиотеки. В него включены параметры: breakpoint для переключения между десктопной и мобильной версиями и client для выбора версии по умолчанию при серверном рендеринге
54 changes: 54 additions & 0 deletions .github/workflows/core-config-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Publish core-config

on:
workflow_dispatch:
inputs:
version:
description: 'Введите версию x.x.x'
required: true

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check permissions
if: ${{ contains(fromJSON('["reme3d2y","hextion","SiebenSieben","fulcanellee"]'), github.actor) == false }}
uses: actions/github-script@v6
with:
script: |
core.setFailed("you don't have permission to run this workflow!");
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: yarn --pure-lockfile

- name: Build app
run: |
cd ./external/core-config
yarn build
- name: Set version
if: success()
run: |
cd ./external/core-config
git config user.name core-ds-bot
git config user.email [email protected]
yarn --new-version version ${{ github.event.inputs.version }}
git push
- name: Publish
if: success()
run: |
cd ./external/core-config
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
21 changes: 21 additions & 0 deletions external/core-config/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 core-ds

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added external/core-config/README.md
Empty file.
17 changes: 17 additions & 0 deletions external/core-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@alfalab/core-config",
"version": "1.0.0",
"description": "Глобальный конфиг для настройки core-components",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"files": [
"/dist"
],
"scripts": {
"build": "tsc"
}
}
26 changes: 26 additions & 0 deletions external/core-config/src/CoreConfigContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createContext, useContext } from 'react';

export type CoreConfigContext = {
breakpoint: number;
client: 'desktop' | 'mobile';
};

export const CoreConfigContext = createContext<CoreConfigContext>({
breakpoint: 1024,
client: 'desktop',
});

export const useCoreConfig = (overrides: Partial<CoreConfigContext> = {}) => {
const config = useContext(CoreConfigContext);

Object.entries(overrides).forEach(([key, value]) => {
if (value === undefined) {
delete overrides[key as keyof typeof overrides];
}
});

return {
...config,
...overrides,
};
};
1 change: 1 addition & 0 deletions external/core-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CoreConfigContext';
14 changes: 14 additions & 0 deletions external/core-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2016",
"jsx": "react",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"declarationDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
5 changes: 3 additions & 2 deletions packages/amount-input/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { FocusEvent, forwardRef, Fragment, useCallback, useEffect, useSta
import cn from 'classnames';

import { Input, InputProps } from '@alfalab/core-components-input';
import { getComponentBreakpoint } from '@alfalab/core-components-shared';
import { withSuffix } from '@alfalab/core-components-with-suffix';
import { CurrencyCodes } from '@alfalab/data';
import { formatAmount, THINSP } from '@alfalab/utils';
Expand Down Expand Up @@ -135,8 +134,9 @@ export const AmountInput = forwardRef<HTMLInputElement, AmountInputProps>(
onChange,
onClear,
onBlur,
breakpoint = getComponentBreakpoint(),
onKeyDown,
breakpoint,
client,
transparentMinor = true,
inputClassName,
label,
Expand Down Expand Up @@ -363,6 +363,7 @@ export const AmountInput = forwardRef<HTMLInputElement, AmountInputProps>(
dataTestId={dataTestId}
ref={ref}
breakpoint={breakpoint}
client={client}
/>
</div>
);
Expand Down
15 changes: 9 additions & 6 deletions packages/button/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import React, { forwardRef } from 'react';

import { useMatchMedia } from '@alfalab/core-components-mq';
import { getComponentBreakpoint } from '@alfalab/core-components-shared';
import { useIsDesktop } from '@alfalab/core-components-mq';

import { ButtonDesktop } from './desktop';
import { ButtonMobile } from './mobile';
import { ButtonProps } from './typings';

export const Button = forwardRef<HTMLAnchorElement | HTMLButtonElement, ButtonProps>(
(
{ children, breakpoint = getComponentBreakpoint(), defaultMatchMediaValue, ...restProps },
{
children,
breakpoint,
client,
defaultMatchMediaValue = client === undefined ? undefined : client === 'desktop',
...restProps
},
ref,
) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);
const isDesktop = useIsDesktop(breakpoint, defaultMatchMediaValue);

const Component = isDesktop ? ButtonDesktop : ButtonMobile;

Expand Down
6 changes: 6 additions & 0 deletions packages/button/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,14 @@ export type ButtonProps = CommonButtonProps & {
*/
breakpoint?: number;

/**
* Версия, которая будет использоваться при серверном рендеринге
*/
client?: 'desktop' | 'mobile';

/**
* Значение по-умолчанию для хука useMatchMedia
* @deprecated Используйте client
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};
1 change: 1 addition & 0 deletions packages/calendar-input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@alfalab/core-components-calendar": "^7.15.1",
"@alfalab/core-components-date-input": "^4.4.6",
"@alfalab/core-components-mq": "^4.3.0",
"@alfalab/core-components-popover": "^6.3.2",
"@alfalab/hooks": "^1.13.0",
"@alfalab/icons-glyph": "^2.139.0",
Expand Down
20 changes: 9 additions & 11 deletions packages/calendar-input/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React, { forwardRef } from 'react';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { DateInputProps } from '@alfalab/core-components-date-input';
import { getComponentBreakpoint } from '@alfalab/core-components-shared';
import { useMedia } from '@alfalab/hooks';
import { useIsDesktop } from '@alfalab/core-components-mq';

import { CalendarInputProps } from './components/calendar-input/Component';
import { CalendarInputDesktop } from './desktop';
Expand All @@ -15,6 +14,11 @@ export type CalendarInputResponsiveProps = Omit<CalendarInputProps, 'view'> & {
* @default 1024
*/
breakpoint?: number;

/**
* Версия, которая будет использоваться при серверном рендеринге
*/
client?: 'desktop' | 'mobile';
};

export type CalendarInputMedia = 'desktop' | 'mobile';
Expand All @@ -24,16 +28,10 @@ export type CalendarInputMedia = 'desktop' | 'mobile';
* use UniversalDateInput instead
*/
export const CalendarInputResponsive = forwardRef<HTMLInputElement, CalendarInputResponsiveProps>(
({ breakpoint = getComponentBreakpoint(), ...restProps }, ref) => {
const [view] = useMedia<CalendarInputMedia>(
[
['mobile', `(max-width: ${breakpoint - 1}px)`],
['desktop', `(min-width: ${breakpoint}px)`],
],
'desktop',
);
({ breakpoint, client, ...restProps }, ref) => {
const isDesktop = useIsDesktop(breakpoint, client === 'desktop');

return view === 'desktop' ? (
return isDesktop ? (
<CalendarInputDesktop {...restProps} ref={ref} />
) : (
<CalendarInputMobile {...restProps} ref={ref} />
Expand Down
4 changes: 3 additions & 1 deletion packages/calendar-input/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@alfalab/core-components-input/*": ["../input/src/*"],
"@alfalab/core-components-button/*": ["../button/src/*"],
"@alfalab/core-components-calendar/*": ["../calendar/src/*"],
"@alfalab/core-components-popover/*": ["../popover/src/*"],
"@alfalab/core-components-*": ["../*/src"]
}
},
Expand All @@ -19,6 +20,7 @@
{ "path": "../date-input" },
{ "path": "../popover" },
{ "path": "../modal" },
{ "path": "../shared" },
{ "path": "../mq" },
{ "path": "../shared" }
]
}
21 changes: 17 additions & 4 deletions packages/calendar/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { forwardRef } from 'react';

import { useMatchMedia } from '@alfalab/core-components-mq';
import { getComponentBreakpoint } from '@alfalab/core-components-shared';
import { useIsDesktop } from '@alfalab/core-components-mq';

import { CalendarMobile, CalendarMobileProps } from './components/calendar-mobile';
import { CalendarDesktop, CalendarDesktopProps } from './desktop';
Expand All @@ -14,15 +13,29 @@ export type ResponsiveCalendarProps = CalendarDesktopProps &
*/
breakpoint?: number;

/**
* Версия, которая будет использоваться при серверном рендеринге
*/
client?: 'desktop' | 'mobile';

/**
* Значение по-умолчанию для хука useMatchMedia
* @deprecated Используйте client
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const CalendarResponsive = forwardRef<HTMLDivElement, ResponsiveCalendarProps>(
({ breakpoint = getComponentBreakpoint(), defaultMatchMediaValue, ...restProps }, ref) => {
const [isDesktop] = useMatchMedia(`(min-width: ${breakpoint}px)`, defaultMatchMediaValue);
(
{
breakpoint,
client,
defaultMatchMediaValue = client === undefined ? undefined : client === 'desktop',
...restProps
},
ref,
) => {
const isDesktop = useIsDesktop(breakpoint, defaultMatchMediaValue);

return isDesktop ? (
<CalendarDesktop {...restProps} ref={ref} />
Expand Down
18 changes: 11 additions & 7 deletions packages/checkbox-group/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { FC } from 'react';

import { useMatchMedia } from '@alfalab/core-components-mq';
import { getComponentBreakpoint } from '@alfalab/core-components-shared';
import { useIsDesktop } from '@alfalab/core-components-mq';

import { BaseCheckboxGroupProps } from './components/base-checkbox-group';
import { CheckboxGroupDesktop } from './desktop';
Expand All @@ -14,20 +13,25 @@ export type CheckboxGroupProps = Omit<BaseCheckboxGroupProps, 'styles'> & {
*/
breakpoint?: number;

/**
* Версия, которая будет использоваться при серверном рендеринге
*/
client?: 'desktop' | 'mobile';

/**
* Значение по-умолчанию для хука useMatchMedia
* @deprecated Используйте client
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};

export const CheckboxGroup: FC<CheckboxGroupProps> = ({
breakpoint = getComponentBreakpoint(),
defaultMatchMediaValue,
breakpoint,
client,
defaultMatchMediaValue = client === undefined ? undefined : client === 'desktop',
...restProps
}) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);
const isDesktop = useIsDesktop(breakpoint, defaultMatchMediaValue);

const Component = isDesktop ? CheckboxGroupDesktop : CheckboxGroupMobile;

Expand Down
Loading

0 comments on commit 4bcff51

Please sign in to comment.