@latte-macchiat-io/latte-vanilla-components 0.0.276 → 0.0.277
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
import { clsx } from 'clsx';
|
2
|
-
import { inputRecipe
|
2
|
+
import { inputRecipe } from './styles.css';
|
3
3
|
|
4
4
|
import { InputType } from './types';
|
5
5
|
|
6
|
-
export type InputProps = React.InputHTMLAttributes<HTMLInputElement> &
|
7
|
-
|
8
|
-
|
9
|
-
};
|
6
|
+
export type InputProps = React.InputHTMLAttributes<HTMLInputElement> & {
|
7
|
+
type?: InputType;
|
8
|
+
};
|
10
9
|
|
11
10
|
export const Input = ({ name, type = 'text', value, required, placeholder, className }: InputProps) => (
|
12
11
|
<input id={name} name={name} type={type} value={value} required={required} placeholder={placeholder} className={clsx(inputRecipe(), className)} />
|
@@ -1,13 +1,11 @@
|
|
1
1
|
import { clsx } from 'clsx';
|
2
2
|
|
3
|
-
import { textareaRecipe
|
3
|
+
import { textareaRecipe } from './styles.css';
|
4
4
|
|
5
|
-
export type TextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> &
|
6
|
-
|
7
|
-
|
8
|
-
hasError?: boolean;
|
9
|
-
};
|
5
|
+
export type TextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> & {
|
6
|
+
name: string;
|
7
|
+
};
|
10
8
|
|
11
|
-
export const Textarea = ({ name,
|
12
|
-
<textarea id={name} rows={rows} name={name} className={clsx(textareaRecipe(
|
9
|
+
export const Textarea = ({ name, rows, className }: TextareaProps) => (
|
10
|
+
<textarea id={name} rows={rows} name={name} className={clsx(textareaRecipe(), className)} />
|
13
11
|
);
|
@@ -7,43 +7,46 @@ import { InputType } from './Input/types';
|
|
7
7
|
import { errorMessage, messageContainer, textFieldRecipe } from './styles.css';
|
8
8
|
import { Textarea } from './Textarea';
|
9
9
|
|
10
|
-
|
10
|
+
type CommonProps = {
|
11
11
|
name: string;
|
12
12
|
label?: string;
|
13
|
-
value?: string;
|
14
|
-
rows?: number;
|
15
|
-
required?: boolean;
|
16
|
-
placeholder?: string;
|
17
13
|
errors?: string | string[];
|
18
14
|
type?: InputType | 'textarea';
|
19
|
-
onChange?: (e: { target: { value: string | undefined } }) => void | undefined;
|
20
15
|
};
|
21
16
|
|
17
|
+
export type TextFieldProps =
|
18
|
+
| (React.InputHTMLAttributes<HTMLInputElement> & CommonProps)
|
19
|
+
| (React.TextareaHTMLAttributes<HTMLTextAreaElement> & CommonProps);
|
20
|
+
|
22
21
|
export const TextField = (props: TextFieldProps) => {
|
22
|
+
const { label, name, errors, type, className, ...rest } = props;
|
23
23
|
const hasErrors = useMemo(() => {
|
24
|
-
if (!
|
25
|
-
if (Array.isArray(
|
26
|
-
return Boolean(
|
27
|
-
}, [
|
24
|
+
if (!errors) return false;
|
25
|
+
if (Array.isArray(errors)) return errors.length > 0;
|
26
|
+
return Boolean(errors);
|
27
|
+
}, [errors]);
|
28
28
|
|
29
|
-
const isTextarea =
|
29
|
+
const isTextarea = type === 'textarea';
|
30
30
|
|
31
31
|
return (
|
32
|
-
<div className={clsx(textFieldRecipe(),
|
33
|
-
{
|
34
|
-
|
35
|
-
{isTextarea ? <Textarea {...props} /> : <Input {...props} />}
|
32
|
+
<div className={clsx(textFieldRecipe(), className)}>
|
33
|
+
{label && <label htmlFor={name}>{label}</label>}
|
36
34
|
|
35
|
+
{isTextarea ? (
|
36
|
+
<Textarea {...(rest as React.TextareaHTMLAttributes<HTMLTextAreaElement>)} name={name} />
|
37
|
+
) : (
|
38
|
+
<Input {...(rest as React.InputHTMLAttributes<HTMLInputElement>)} name={name} type={type as InputType} />
|
39
|
+
)}
|
37
40
|
{hasErrors && (
|
38
41
|
<div className={messageContainer}>
|
39
|
-
{Array.isArray(
|
40
|
-
|
42
|
+
{Array.isArray(errors) ? (
|
43
|
+
errors.map((error, index) => (
|
41
44
|
<span key={index} className={errorMessage}>
|
42
45
|
{error}
|
43
46
|
</span>
|
44
47
|
))
|
45
48
|
) : (
|
46
|
-
<span className={errorMessage}>{
|
49
|
+
<span className={errorMessage}>{errors}</span>
|
47
50
|
)}
|
48
51
|
</div>
|
49
52
|
)}
|