Skip to content

Commit c8a29ee

Browse files
committed
fix: w3c validation + position text validation IF-1684
1 parent 2b05d19 commit c8a29ee

13 files changed

+168
-159
lines changed

contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ function foo() {}
350350
Стоит избегать общих слов, таких как "change". Вместо этого опишите в чем конкретно произошло изменение.
351351

352352
Примеры:
353-
- validationsRemoveExtraSpans - Избавиться от обертки span в ValidationContainer, ValidationWrapper и ValidationText
353+
- validationsDivWrapper - Поменять обертку span на div c 'display: inline' чтобы соответсвовать w3c стандарту в ValidationContainer, ValidationWrapper и ValidationText
354354
- tokenInputRemoveWhitespaceFromDefaultDelimiters - В TokenInput изменили разделитель по умолчанию
355355

356356
2) Добавьте флаг в ReactUIFeatureFlags в файл ReactUIFeatureFlagsContext.tsx и в документацию FEATUREFLAGSCONTEXT.md

packages/react-ui-validations/docs/Pages/Displaying/FeatureFlags/FeatureFlagsContext.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Доступные флаги
44

55
export interface ValidationsFeatureFlags {
6-
validationsRemoveExtraSpans?: boolean;
6+
validationsDivWrapper?: boolean;
77
fixedValidationTextColors?: boolean;
88
darkTheme?: boolean;
99
}
@@ -14,16 +14,16 @@
1414

1515
import { ValidationsFeatureFlagsContext } from '@skbkontur/react-ui-validations'
1616

17-
<ValidationsFeatureFlagsContext.Provider value={{ validationsRemoveExtraSpans: true }}>{/* ... */}</ValidationsFeatureFlagsContext.Provider>;
17+
<ValidationsFeatureFlagsContext.Provider value={{ validationsDivWrapper: true }}>{/* ... */}</ValidationsFeatureFlagsContext.Provider>;
1818

1919
## Использование
2020

21-
### validationsRemoveExtraSpans
21+
### validationsDivWrapper
2222

2323
В ValidationContainer, ValidationWrapper и ValidationText из корня удалён лишний span.
2424
В Validations 2.0 фича будет применена по умолчанию.
2525

26-
!!DemoWithCode!!FeatureFlagsExamplevalidationsRemoveExtraSpans
26+
!!DemoWithCode!!FeatureFlagsExampleValidationsRemoveDivWrapper
2727

2828

2929
### fixedValidationTextColors
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface CheckboxState {
1313
checked: boolean;
1414
}
1515

16-
export default class FeatureFlagsExampleValidationsWrapperAndContainerRemoveExtraSpan extends React.Component {
16+
export default class FeatureFlagsExampleValidationsWrapperAndContainerDivWrapper extends React.Component {
1717
public state: CheckboxState = {
1818
checked: false,
1919
};
@@ -31,9 +31,7 @@ export default class FeatureFlagsExampleValidationsWrapperAndContainerRemoveExtr
3131

3232
render() {
3333
return (
34-
<ValidationsFeatureFlagsContext.Provider
35-
value={{ validationsRemoveExtraSpans: true }}
36-
>
34+
<ValidationsFeatureFlagsContext.Provider value={{ validationsDivWrapper: true }}>
3735
<ValidationContainer ref={this.refContainer}>
3836
<ValidationWrapper validationInfo={this.validateSex()}>
3937
<Checkbox

packages/react-ui-validations/src/ValidationContextWrapper.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ValidationWrapperInternal } from './ValidationWrapperInternal';
99
import type { ScrollOffset, ValidateArgumentType } from './ValidationContainer';
1010
import { isNullable } from './utils/isNullable';
1111
import { FocusMode } from './FocusMode';
12+
import { InlineDiv } from './utils/InlineDiv';
1213

1314
export interface ValidationContextSettings {
1415
scrollOffset: ScrollOffset;
@@ -165,8 +166,8 @@ export class ValidationContextWrapper extends React.Component<ValidationContextW
165166
private featureFlags!: ValidationsFeatureFlags;
166167

167168
private children = (flags: ValidationsFeatureFlags) => {
168-
if (flags.validationsRemoveExtraSpans) {
169-
return this.props.children;
169+
if (flags.validationsDivWrapper) {
170+
return <InlineDiv>{this.props.children}</InlineDiv>;
170171
}
171172

172173
return <span>{this.props.children}</span>;

packages/react-ui-validations/src/ValidationText.tsx

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@ export const ValidationText = ({ pos, children, validation, 'data-tid': dataTid
2020
: '#d43517';
2121

2222
if (pos === 'right') {
23-
const childrenAndValidationText = (
24-
<>
23+
return (
24+
<span style={{ display: 'inline-block' }}>
2525
{children}
2626
<span data-tid={dataTid} data-validation-message="text" style={{ marginLeft: '10px', color }}>
2727
{(validation && validation.message) || ''}
2828
</span>
29-
</>
30-
);
31-
32-
return featureFlags.validationsRemoveExtraSpans ? (
33-
childrenAndValidationText
34-
) : (
35-
<span style={{ display: 'inline-block' }}>{childrenAndValidationText}</span>
29+
</span>
3630
);
3731
}
3832

@@ -53,12 +47,7 @@ export const ValidationText = ({ pos, children, validation, 'data-tid': dataTid
5347
</span>
5448
);
5549

56-
return featureFlags.validationsRemoveExtraSpans ? (
57-
<>
58-
{children}
59-
<span style={{ position: 'absolute', display: 'block' }}>{validationText}</span>
60-
</>
61-
) : (
50+
return (
6251
<span style={{ position: 'relative', display: 'inline-block' }}>
6352
{children}
6453
<span style={{ position: 'absolute', bottom: 0, left: 0, height: 0 }}>{validationText}</span>
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import React from 'react';
22

3-
import {
4-
ValidationsFeatureFlags,
5-
ValidationsFeatureFlagsContext,
6-
getFullValidationsFlagsContext,
7-
} from './utils/featureFlagsContext';
83
import { ReactUiDetection, Tooltip } from './ReactUiDetection';
94

105
export type TooltipPosition =
@@ -33,31 +28,22 @@ export class ValidationTooltip extends React.Component<ValidationTooltipProps> {
3328
public static __KONTUR_REACT_UI__ = 'ValidationTooltip';
3429
public static displayName = 'ValidationTooltip';
3530

36-
private featureFlags!: ValidationsFeatureFlags;
3731
public render() {
3832
const { children, pos, error, render, ...rest } = this.props;
3933

4034
const onlyChild = React.Children.only(children);
4135
const child = onlyChild && onlyChild.props ? onlyChild.props.children : null;
4236

43-
return (
44-
<ValidationsFeatureFlagsContext.Consumer>
45-
{(flags) => {
46-
this.featureFlags = getFullValidationsFlagsContext(flags);
47-
return !this.featureFlags.validationsRemoveExtraSpans &&
48-
(ReactUiDetection.isRadioGroup(child) ||
49-
ReactUiDetection.isTokenInput(child) ||
50-
ReactUiDetection.isSwitcher(child)) ? (
51-
<Tooltip useWrapper={false} pos={pos} render={error && render} trigger={'hover&focus'} {...rest}>
52-
{child}
53-
</Tooltip>
54-
) : (
55-
<Tooltip pos={pos} render={error && render} trigger={'hover&focus'} {...rest}>
56-
{children}
57-
</Tooltip>
58-
);
59-
}}
60-
</ValidationsFeatureFlagsContext.Consumer>
37+
return ReactUiDetection.isRadioGroup(child) ||
38+
ReactUiDetection.isTokenInput(child) ||
39+
ReactUiDetection.isSwitcher(child) ? (
40+
<Tooltip useWrapper={false} pos={pos} render={error && render} trigger={'hover&focus'} {...rest}>
41+
{child}
42+
</Tooltip>
43+
) : (
44+
<Tooltip pos={pos} render={error && render} trigger={'hover&focus'} {...rest}>
45+
{children}
46+
</Tooltip>
6147
);
6248
}
6349
}

packages/react-ui-validations/src/ValidationWrapperInternal.tsx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import warning from 'warning';
33

44
import { Nullable } from '../typings/Types';
55

6-
import {
7-
ValidationsFeatureFlags,
8-
ValidationsFeatureFlagsContext,
9-
getFullValidationsFlagsContext,
10-
} from './utils/featureFlagsContext';
116
import { getRootNode } from './utils/getRootNode';
127
import { isBrowser } from './utils/utils';
138
import { smoothScrollIntoView } from './smoothScrollIntoView';
@@ -70,8 +65,6 @@ export class ValidationWrapperInternal extends React.Component<
7065
public static contextType = ValidationContext;
7166
public context: ValidationContextType = this.context;
7267

73-
private featureFlags!: ValidationsFeatureFlags;
74-
7568
public componentDidMount() {
7669
warning(
7770
this.context,
@@ -150,23 +143,9 @@ export class ValidationWrapperInternal extends React.Component<
150143
});
151144
}
152145

153-
return (
154-
<ValidationsFeatureFlagsContext.Consumer>
155-
{(flags) => {
156-
this.featureFlags = getFullValidationsFlagsContext(flags);
157-
return React.cloneElement(
158-
this.props.errorMessage(
159-
this.featureFlags.validationsRemoveExtraSpans ? clonedChild : <span>{clonedChild}</span>,
160-
!!validation,
161-
validation,
162-
),
163-
{
164-
'data-tid': dataTid,
165-
},
166-
);
167-
}}
168-
</ValidationsFeatureFlagsContext.Consumer>
169-
);
146+
return React.cloneElement(this.props.errorMessage(clonedChild, !!validation, validation), {
147+
'data-tid': dataTid,
148+
});
170149
}
171150

172151
private customRef = (instance: Nullable<ReactInstance>) => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import type { FC, PropsWithChildren } from 'react';
3+
4+
export const InlineDiv: FC<PropsWithChildren<React.HTMLAttributes<HTMLDivElement>>> = ({ children, ...rest }) => (
5+
<div style={{ display: 'inline' }} {...rest}>
6+
{children}
7+
</div>
8+
);

packages/react-ui-validations/src/utils/featureFlagsContext/ValidationsFeatureFlagsContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type FeatureFlags = 'validationsRemoveExtraSpans' | 'fixedValidationTextC
55
export type ValidationsFeatureFlags = Partial<Record<FeatureFlags, boolean>>;
66

77
export const validationsFeatureFlagsDefault: ValidationsFeatureFlags = {
8-
validationsRemoveExtraSpans: false,
8+
validationsDivWrapper: false,
99
fixedValidationTextColors: false,
1010
};
1111

packages/react-ui-validations/stories/Checkbox.stories.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Meta } from '@storybook/react';
33
import { Button } from '@skbkontur/react-ui/components/Button';
44
import { Checkbox } from '@skbkontur/react-ui/components/Checkbox/Checkbox';
55

6-
import { ValidationContainer, ValidationInfo, ValidationWrapper, ValidationsFeatureFlagsContext } from '../src';
6+
import { ValidationContainer, ValidationInfo, ValidationWrapper } from '../src';
77
import { Nullable } from '../typings/Types';
88

99
export default {
@@ -22,19 +22,17 @@ export const Required = () => {
2222
};
2323

2424
return (
25-
<ValidationsFeatureFlagsContext.Provider value={{ validationsRemoveExtraSpans: true }}>
26-
<div style={{ padding: 20 }}>
27-
<ValidationContainer ref={refContainer}>
28-
<ValidationWrapper validationInfo={validateSex()}>
29-
<Checkbox checked={checked ? checked : false} onValueChange={setChecked}>
30-
Checkbox
31-
</Checkbox>
32-
</ValidationWrapper>
33-
<div style={{ padding: '20px 0' }}>
34-
<Button onClick={() => refContainer.current?.validate()}>Check</Button>
35-
</div>
36-
</ValidationContainer>
37-
</div>
38-
</ValidationsFeatureFlagsContext.Provider>
25+
<div style={{ padding: 20 }}>
26+
<ValidationContainer ref={refContainer}>
27+
<ValidationWrapper validationInfo={validateSex()}>
28+
<Checkbox checked={checked ? checked : false} onValueChange={setChecked}>
29+
Checkbox
30+
</Checkbox>
31+
</ValidationWrapper>
32+
<div style={{ padding: '20px 0' }}>
33+
<Button onClick={() => refContainer.current?.validate()}>Check</Button>
34+
</div>
35+
</ValidationContainer>
36+
</div>
3937
);
4038
};

packages/react-ui-validations/stories/Input.stories.tsx

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@ import { Input } from '@skbkontur/react-ui/components/Input';
55
import { Select } from '@skbkontur/react-ui/components/Select';
66
import { Gapped } from '@skbkontur/react-ui';
77

8-
import {
9-
text,
10-
tooltip,
11-
ValidationBehaviour,
12-
ValidationContainer,
13-
ValidationInfo,
14-
ValidationWrapper,
15-
ValidationsFeatureFlagsContext,
16-
} from '../src';
8+
import { text, tooltip, ValidationBehaviour, ValidationContainer, ValidationInfo, ValidationWrapper } from '../src';
179
import { Nullable } from '../typings/Types';
1810

1911
export default {
@@ -178,23 +170,21 @@ export const Example_6 = () => {
178170
};
179171

180172
return (
181-
<ValidationsFeatureFlagsContext.Provider value={{ validationsRemoveExtraSpans: true }}>
182-
<ValidationContainer ref={refContainer}>
183-
<div style={{ padding: 50, height: 200, position: 'relative' }}>
184-
<div style={{ position: 'absolute', top: 100 }}>
185-
<ValidationWrapper validationInfo={validateValue1()}>
186-
<Input value={value1} onValueChange={setValue1} />
187-
</ValidationWrapper>
188-
</div>
189-
<div style={{ position: 'absolute', top: 20 }}>
190-
<ValidationWrapper validationInfo={validateValue2()}>
191-
<Input value={value2} onValueChange={setValue2} />
192-
</ValidationWrapper>
193-
</div>
173+
<ValidationContainer ref={refContainer}>
174+
<div style={{ padding: 50, height: 200, position: 'relative' }}>
175+
<div style={{ position: 'absolute', top: 100 }}>
176+
<ValidationWrapper validationInfo={validateValue1()}>
177+
<Input value={value1} onValueChange={setValue1} />
178+
</ValidationWrapper>
194179
</div>
195-
<Button onClick={() => refContainer.current?.submit()}>Отправить</Button>
196-
</ValidationContainer>
197-
</ValidationsFeatureFlagsContext.Provider>
180+
<div style={{ position: 'absolute', top: 20 }}>
181+
<ValidationWrapper validationInfo={validateValue2()}>
182+
<Input value={value2} onValueChange={setValue2} />
183+
</ValidationWrapper>
184+
</div>
185+
</div>
186+
<Button onClick={() => refContainer.current?.submit()}>Отправить</Button>
187+
</ValidationContainer>
198188
);
199189
};
200190

@@ -381,21 +371,19 @@ export const Example_11 = () => {
381371
refContainer.current?.submit();
382372
}}
383373
>
384-
<ValidationsFeatureFlagsContext.Provider value={{ validationsRemoveExtraSpans: true }}>
385-
<ValidationContainer ref={refContainer}>
386-
<ValidationWrapper
387-
renderMessage={tooltip('left middle')}
388-
validationInfo={{ message: 'Ошибка!', type: 'submit' }}
389-
>
390-
<Input width="100%" placeholder={'Валидация'} />
391-
</ValidationWrapper>
392-
<br />
393-
<br />
394-
<ValidationWrapper renderMessage={text('bottom')} validationInfo={{ message: 'Ошибка!', type: 'submit' }}>
395-
<Input width="100%" placeholder={'Валидация'} />
396-
</ValidationWrapper>
397-
</ValidationContainer>
398-
</ValidationsFeatureFlagsContext.Provider>
374+
<ValidationContainer ref={refContainer}>
375+
<ValidationWrapper
376+
renderMessage={tooltip('left middle')}
377+
validationInfo={{ message: 'Ошибка!', type: 'submit' }}
378+
>
379+
<Input width="100%" placeholder={'Валидация'} />
380+
</ValidationWrapper>
381+
<br />
382+
<br />
383+
<ValidationWrapper renderMessage={text('bottom')} validationInfo={{ message: 'Ошибка!', type: 'submit' }}>
384+
<Input width="100%" placeholder={'Валидация'} />
385+
</ValidationWrapper>
386+
</ValidationContainer>
399387
<br />
400388
<br />
401389
<br />

0 commit comments

Comments
 (0)