Skip to content

Commit

Permalink
Merge pull request #2353 from skbkontur/switcher-validation-tooltip
Browse files Browse the repository at this point in the history
[Switcher] проблемы с валидацией
  • Loading branch information
lossir committed Apr 26, 2021
2 parents aa53349 + 031fdd3 commit 462c908
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/react-ui-validations/src/ReactUiDetection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export class ReactUiDetection {
public static isTokenInput(childrenArray: any): boolean {
return childrenArray != null && childrenArray.type?.__KONTUR_REACT_UI__ === 'TokenInput';
}

public static isSwitcher(childrenArray: any): boolean {
return childrenArray != null && childrenArray.type?.__KONTUR_REACT_UI__ === 'Switcher';
}
}
6 changes: 5 additions & 1 deletion packages/react-ui-validations/src/ValidationTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export class ValidationTooltip extends React.Component<ValidationTooltipProps> {
public render() {
const onlyChild = React.Children.only(this.props.children);
const child = onlyChild && onlyChild.props ? onlyChild.props.children : null;
if (ReactUiDetection.isRadioGroup(child) || ReactUiDetection.isTokenInput(child)) {
if (
ReactUiDetection.isRadioGroup(child) ||
ReactUiDetection.isTokenInput(child) ||
ReactUiDetection.isSwitcher(child)
) {
return (
<Tooltip
useWrapper={false}
Expand Down
50 changes: 50 additions & 0 deletions packages/react-ui-validations/stories/Switcher.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { storiesOf } from '@storybook/react';
import React from 'react';
import { Button } from '@skbkontur/react-ui/components/Button';
import { Switcher } from '@skbkontur/react-ui/components/Switcher/Switcher';

import { ValidationContainer, ValidationInfo, ValidationWrapper } from '../src';
import { Nullable } from '../typings/Types';

storiesOf('Switcher', module).add('required', () => <SwitcherStory />);

interface SwitcherStoryState {
value: string;
}

class SwitcherStory extends React.Component<{}, SwitcherStoryState> {
public state: SwitcherStoryState = {
value: '',
};

private container: ValidationContainer | null = null;

public validateValue(): Nullable<ValidationInfo> {
const { value } = this.state;
if (!value) {
return { message: 'Поле обязательно', type: 'submit' };
}
return null;
}

public render() {
return (
<div style={{ padding: '20px 20px' }}>
<ValidationContainer ref={this.refContainer}>
<ValidationWrapper validationInfo={this.state.value === '' ? this.validateValue() : undefined}>
<Switcher
value={this.state.value}
items={['string1', 'string2']}
onValueChange={value => this.setState({ value })}
/>
</ValidationWrapper>
<div style={{ padding: '20px 0' }}>
<Button onClick={() => this.container && this.container.validate()}>Check</Button>
</div>
</ValidationContainer>
</div>
);
}

private refContainer = (el: ValidationContainer | null) => (this.container = el);
}

0 comments on commit 462c908

Please sign in to comment.