Skip to content

Commit

Permalink
Merge pull request #411 from vrk-kpa/feature/textinput-improvements
Browse files Browse the repository at this point in the history
[Feature] TextInput improvements
  • Loading branch information
ketsappi authored Nov 20, 2020
2 parents 1a126b7 + f9f1511 commit dccf99d
Show file tree
Hide file tree
Showing 18 changed files with 677 additions and 699 deletions.
188 changes: 0 additions & 188 deletions src/components/Form/TextInput.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export {
DropdownItem,
DropdownItemProps,
} from './Dropdown/Dropdown';
export { TextInput, TextInputProps } from './Form/TextInput';
export { Checkbox, CheckboxProps } from './Form/Checkbox';
export {
Toggle,
Expand Down
14 changes: 14 additions & 0 deletions src/core/Form/HintText/HintText.baseStyles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { css } from 'styled-components';
import { withSuomifiTheme, TokensAndTheme } from '../../theme';
import { font } from '../../theme/reset';

export const baseStyles = withSuomifiTheme(
({ theme }: TokensAndTheme) => css`
&.fi-hint-text {
display: block;
color: ${theme.colors.blackBase};
margin-bottom: ${theme.spacing.xs};
${font({ theme })('bodyTextSmall')};
}
`,
);
40 changes: 40 additions & 0 deletions src/core/Form/HintText/HintText.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { render } from '@testing-library/react';
import { axeTest } from '../../../utils/test/axe';

import { HintText } from './HintText';

describe('props', () => {
describe('children', () => {
it('shows the given text', () => {
const { container } = render(<HintText>Test text</HintText>);
expect(container.firstChild).toHaveTextContent('Test text');
});

it('is null when no children given', () => {
const { container } = render(<HintText />);
expect(container.firstChild).toEqual(null);
});
});

describe('id', () => {
it('has the given id', () => {
const { container } = render(<HintText id="test-id">Test text</HintText>);
expect(container.firstChild).toHaveAttribute('id', 'test-id');
});
});

describe('className', () => {
it('has the given custom classname', () => {
const { container } = render(
<HintText className="custom-style">Test text</HintText>,
);
expect(container.firstChild).toHaveClass('custom-style');
});
});
});

test(
'should not have basic accessibility issues',
axeTest(<HintText>Test text</HintText>),
);
47 changes: 47 additions & 0 deletions src/core/Form/HintText/HintText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { Component, ReactNode } from 'react';
import classnames from 'classnames';
import { default as styled } from 'styled-components';
import { baseStyles } from './HintText.baseStyles';
import { HtmlSpan, HtmlSpanProps } from '../../../reset';
import { TokensProp, InternalTokensProp } from 'core/theme';
import { withSuomifiDefaultProps } from '../../theme/utils';

const baseClassName = 'fi-hint-text';

interface InternalHintTextProps extends HtmlSpanProps {
/** id */
id?: string;
/** HintText element content */
children?: ReactNode;
/** Custom class name for styling and customizing */
className?: string;
}

export interface HintTextProps extends InternalHintTextProps, TokensProp {}

const StyledHintText = styled(
({
className,
children,
tokens,
...passProps
}: HintTextProps & InternalTokensProp) => (
<HtmlSpan
{...passProps}
className={classnames(className, baseClassName, {})}
>
{children}
</HtmlSpan>
),
)`
${(tokens) => baseStyles(withSuomifiDefaultProps(tokens))}
`;
export class HintText extends Component<HintTextProps> {
render() {
const { children, ...passProps } = withSuomifiDefaultProps(this.props);
if (!children) {
return null;
}
return <StyledHintText {...passProps}>{children}</StyledHintText>;
}
}
4 changes: 4 additions & 0 deletions src/core/Form/LabelText/LabelText.baseStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const baseStyles = withSuomifiTheme(
display: block;
margin-bottom: ${theme.spacing.xs};
color: ${theme.colors.blackBase};
& .fi-label-text_optionalText {
${theme.typography.bodyTextSmall};
}
}
}
`,
Expand Down
87 changes: 87 additions & 0 deletions src/core/Form/LabelText/LabelText.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from 'react';
import { render } from '@testing-library/react';
import { axeTest } from '../../../utils/test/axe';

import { LabelText } from './LabelText';

describe('props', () => {
describe('children', () => {
it('shows the given text', () => {
const { container } = render(<LabelText>Test text</LabelText>);
expect(container.firstChild).toHaveTextContent('Test text');
});
});

describe('optionalText', () => {
it('has the optional text element', () => {
const { getByText } = render(
<LabelText optionalText="optional">Test text</LabelText>,
);
const optionalText = getByText('(optional)');
expect(optionalText).toHaveClass('fi-label-text_optionalText');
});
});

describe('id', () => {
it('has the given id', () => {
const { container } = render(
<LabelText id="test-id">Test text</LabelText>,
);
expect(container.firstChild).toHaveAttribute('id', 'test-id');
});
});

describe('className', () => {
it('has the given custom classname', () => {
const { container } = render(
<LabelText className="custom-style">Test text</LabelText>,
);
expect(container.firstChild).toHaveClass('custom-style');
});
});

describe('labelMode', () => {
it('should be visible by default', () => {
const { getByText } = render(<LabelText>Test text</LabelText>);
const label = getByText('Test text');
expect(label).toHaveClass('fi-label-text_label-span');
});

it('should be hidden', () => {
const { getByText } = render(
<LabelText labelMode="hidden">Test text</LabelText>,
);
const label = getByText('Test text');
expect(label).toHaveClass('fi-visually-hidden');
});
});

describe('asProp', () => {
it('has default of div as wrapping element', () => {
const { container } = render(<LabelText>Test text</LabelText>);
expect(container.firstChild).toBeInstanceOf(HTMLDivElement);
});

it('has the given wrapper element', () => {
const { container } = render(<LabelText as="label">Test text</LabelText>);
expect(container.firstChild).toBeInstanceOf(HTMLLabelElement);
});
});

describe('labelSpanProps', () => {
it('has the given props', () => {
const { getByText } = render(
<LabelText labelSpanProps={{ style: { fontSize: 12 } }}>
Test text
</LabelText>,
);
const textSpan = getByText('Test text');
expect(textSpan).toHaveAttribute('style', 'font-size: 12px;');
});
});
});

test(
'should not have basic accessibility issues',
axeTest(<LabelText>Test text</LabelText>),
);
Loading

0 comments on commit dccf99d

Please sign in to comment.