Skip to content

Commit d3c5072

Browse files
committed
Add LabelBlock and RandomName
1 parent 0551b95 commit d3c5072

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import Hint, { HintProps } from '../hint/Hint';
3+
import Label, { LabelProps } from '../label/Label';
4+
import ErrorMessage, { ErrorMessageProps } from '../error-message/ErrorMessage';
5+
6+
interface LabelBlockProps {
7+
elementId?: string;
8+
label?: string;
9+
labelProps?: LabelProps;
10+
hint?: string;
11+
hintProps?: HintProps;
12+
error?: string | boolean;
13+
errorProps?: ErrorMessageProps;
14+
}
15+
16+
const LabelBlock: React.FC<LabelBlockProps> = ({
17+
elementId,
18+
label,
19+
labelProps,
20+
hint,
21+
hintProps,
22+
error,
23+
errorProps,
24+
}) => (
25+
<>
26+
{label ? (
27+
<Label id={elementId ? `${elementId}--label` : undefined} htmlFor={elementId} {...labelProps}>
28+
{label}
29+
</Label>
30+
) : null}
31+
{hint ? (
32+
<Hint id={elementId ? `${elementId}--hint` : undefined} {...hintProps}>
33+
{hint}
34+
</Hint>
35+
) : null}
36+
{error && typeof error === 'string' ? (
37+
<ErrorMessage id={elementId ? `${elementId}--error-message` : undefined} {...errorProps}>
38+
{error}
39+
</ErrorMessage>
40+
) : null}
41+
</>
42+
);
43+
44+
export default LabelBlock;

src/util/RandomName.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const getRandomString = (length: number = 5) => {
2+
const randomNumber = Math.random() + 1;
3+
return randomNumber.toString(36).substring(2, length + 2);
4+
};
5+
6+
export const generateRandomName = (prefix?: string) => {
7+
const randomString = getRandomString();
8+
return prefix ? `${prefix}_${randomString}` : randomString;
9+
};

0 commit comments

Comments
 (0)