react-validate-component 0.8.1 → 1.1.0

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.
@@ -12,22 +12,63 @@ export function VText({
12
12
  vLocateMessage = 'bottom',
13
13
  vMessageClass = '',
14
14
  vIsAnimate = false,
15
+ vUseMaxLength = false,
16
+ vMaxLength = 0,
17
+ vClassMaxLength = '',
15
18
  props = {},
16
19
  }: VTEXT_PARAMS) {
20
+ const inputRef = React.useRef<HTMLInputElement>(null);
21
+ const [inputLength, setInputLength] = React.useState<number>(
22
+ (props?.defaultValue || props?.value)?.length ?? 0
23
+ );
24
+
25
+ // 공통 input 태그 변수화
26
+ const inputHTML = (
27
+ <input
28
+ type="text"
29
+ ref={inputRef}
30
+ {...props}
31
+ onChange={e => {
32
+ if (vUseMaxLength && e.target.value.length > vMaxLength) {
33
+ e.preventDefault();
34
+ e.target.value = e.target.value.slice(0, vMaxLength);
35
+ } else {
36
+ props?.onChange(e);
37
+ }
38
+ }}
39
+ defaultValue={props?.defaultValue ?? ''}
40
+ className={`${props?.className ?? ''} ${vClassName} ${
41
+ vState ? styles.invalid : ''
42
+ }`}
43
+ ></input>
44
+ );
45
+
46
+ // 일부 옵션 값들은 input 기본 속성값을 이용하기 위해 merge 진행
47
+ if (vUseMaxLength) {
48
+ props = { ...props, maxLength: vMaxLength };
49
+ }
50
+
51
+ React.useEffect(() => {
52
+ // 입력 최댓값을 사용할 경우 useEffect Hook 설정
53
+ if (vUseMaxLength) {
54
+ setInputLength((inputRef.current?.value ?? '').length);
55
+ }
56
+ }, [inputRef.current?.value]);
57
+
17
58
  switch (vType) {
18
59
  case 'outer':
19
60
  return (
20
61
  <div
21
- className={`${styles.vinput} ${styles[`vinput-${vLocateMessage}`]}`}
62
+ className={`${styles.vinput} ${styles[`vinput-${vLocateMessage}`]} ${
63
+ vUseMaxLength ? styles.vMaxLengthContainer : ''
64
+ }`}
22
65
  >
23
- <input
24
- type="text"
25
- {...props}
26
- defaultValue={props?.defaultValue ?? ''}
27
- className={`${props?.className ?? ''} ${vClassName} ${
28
- vState ? styles.invalid : ''
29
- }`}
30
- ></input>
66
+ {inputHTML}
67
+ {vUseMaxLength && (
68
+ <p className={`${styles.vMaxLength} ${vClassMaxLength}`}>
69
+ {inputLength} / {vMaxLength}
70
+ </p>
71
+ )}
31
72
  {vState && vShowMessage ? (
32
73
  <p
33
74
  className={`${vMessageClass} ${
@@ -41,15 +82,17 @@ export function VText({
41
82
  );
42
83
  case 'inner':
43
84
  return (
44
- <div className={styles.vinput}>
45
- <input
46
- type="text"
47
- {...props}
48
- defaultValue={props?.defaultValue ?? ''}
49
- className={`${props?.className ?? ''} ${vClassName} ${
50
- vState ? styles.invalid : ''
51
- }`}
52
- ></input>
85
+ <div
86
+ className={`${styles.vinput} ${
87
+ vUseMaxLength ? styles.vMaxLengthContainer : ''
88
+ }`}
89
+ >
90
+ {inputHTML}
91
+ {vUseMaxLength && (
92
+ <p className={`${styles.vMaxLength} ${vClassMaxLength}`}>
93
+ {inputLength} / {vMaxLength}
94
+ </p>
95
+ )}
53
96
  {vState && vShowMessage ? (
54
97
  <p
55
98
  className={`${vMessageClass} ${styles.innerMessage} ${
@@ -66,16 +109,14 @@ export function VText({
66
109
  <div
67
110
  className={`${styles.vinput} ${
68
111
  styles[`tooltipMessage-${vLocateMessage}`]
69
- }`}
112
+ } ${vUseMaxLength ? styles.vMaxLengthContainer : ''}`}
70
113
  >
71
- <input
72
- type="text"
73
- {...props}
74
- defaultValue={props?.defaultValue ?? ''}
75
- className={`${props?.className ?? ''} ${vClassName} ${
76
- vState ? styles.invalid : ''
77
- }`}
78
- ></input>
114
+ {inputHTML}
115
+ {vUseMaxLength && (
116
+ <p className={`${styles.vMaxLength} ${vClassMaxLength}`}>
117
+ {inputLength} / {vMaxLength}
118
+ </p>
119
+ )}
79
120
  {vState && vShowMessage ? (
80
121
  <p
81
122
  className={`${vMessageClass} ${styles.tooltipMessage} ${
@@ -20,5 +20,8 @@ export interface VTEXT_PARAMS {
20
20
  | 'bottom-right';
21
21
  vMessageClass?: string; // 유효성 메시지에 입힐 class 명
22
22
  vIsAnimate?: boolean; // 유효성 메시지 출력 시 애니메이션 적용할지
23
+ vUseMaxLength?: boolean; // 입력 최댓값 표시 사용 여부
24
+ vMaxLength?: number; // 입력 최댓값
25
+ vClassMaxLength?: string; // 입력 최댓값 class 명
23
26
  props?: propsType; // 기타 옵션
24
27
  }