Skip to content

Commit c0ab464

Browse files
committed
Allow better customization of Hint component
1 parent 882d847 commit c0ab464

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

docs/API.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ The `Hint` component can be used to wrap custom inputs.
173173
#### Props
174174
Name | Type | Default | Description
175175
-----|------|---------|------------
176-
children `(required)` | node | |
176+
children `(required)` | `ReactNode` | |
177+
className | `string` | Class names applied to the containing element. |
178+
hintClassName | `string` | Class names applied to the the hint element. |
179+
hintStyle | `CSSProperties` | Inline styles applied to the hint element. |
180+
style | `CSSProperties` | Inline styl;es applied to the containing element. |
177181

178182
### `<Input>`
179183
Abstract `<input>` component that handles an `inputRef` prop and is used as the basis for both single- and multi-select input components.

src/components/Hint/Hint.test.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
1+
import * as React from 'react';
2+
13
import * as stories from './Hint.stories';
2-
import { generateSnapshots } from '../../tests/helpers';
4+
import {
5+
composeStories,
6+
generateSnapshots,
7+
render,
8+
screen,
9+
} from '../../tests/helpers';
10+
11+
const { Default } = composeStories(stories);
312

413
describe('<Hint>', () => {
514
generateSnapshots(stories);
15+
16+
it('applies a classname and style to the containing element', () => {
17+
const { container } = render(
18+
<Default className="custom-class" style={{ backgroundColor: 'blue' }} />
19+
);
20+
21+
expect(container.firstChild).toHaveClass('custom-class');
22+
expect(container.firstChild).toHaveStyle({ backgroundColor: 'blue' });
23+
});
24+
25+
it('applies a classname and style to the hint element', () => {
26+
render(<Default hintClassName="hint-class" hintStyle={{ color: 'red' }} />);
27+
28+
const hintElement = screen.getByDisplayValue('california');
29+
expect(hintElement).toHaveClass('hint-class');
30+
expect(hintElement).toHaveStyle({ color: 'red' });
31+
});
632
});

src/components/Hint/Hint.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
import React, { ReactNode } from 'react';
1+
import cx from 'classnames';
2+
import React, { CSSProperties, ReactNode } from 'react';
23

34
import { useHint } from '../../hooks';
45

56
export interface HintProps {
67
children: ReactNode;
78
className?: string;
9+
hintClassName?: string;
10+
hintStyle?: CSSProperties;
11+
style?: CSSProperties;
812
}
913

10-
const Hint = ({ children, className }: HintProps) => {
14+
const Hint = (props: HintProps) => {
1115
const { hintRef, hintText } = useHint();
1216

1317
return (
1418
<div
15-
className={className}
19+
className={props.className}
1620
style={{
1721
display: 'flex',
1822
flex: 1,
1923
height: '100%',
2024
position: 'relative',
25+
...props.style,
2126
}}>
22-
{children}
27+
{props.children}
2328
<input
2429
aria-hidden
25-
className="rbt-input-hint"
30+
className={cx('rbt-input-hint', props.hintClassName)}
2631
ref={hintRef}
2732
readOnly
2833
style={{
@@ -35,6 +40,7 @@ const Hint = ({ children, className }: HintProps) => {
3540
position: 'absolute',
3641
top: 0,
3742
width: '100%',
43+
...props.hintStyle,
3844
}}
3945
tabIndex={-1}
4046
value={hintText}

0 commit comments

Comments
 (0)