-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
mixin.ts
302 lines (251 loc) · 9.63 KB
/
mixin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import React from 'react';
import PropTypes from 'prop-types';
import IMask, { type InputMask, type InputMaskElement, type FactoryOpts, type AllFactoryStaticOpts } from 'imask';
type AnyProps = Record<string, unknown>;
export
type Falsy = false | 0 | "" | null | undefined;
export
type ReactMaskOpts = FactoryOpts & { unmask?: 'typed' | boolean };
export
type UnmaskValue<Opts extends ReactMaskOpts> =
Opts['unmask'] extends 'typed' ? InputMask<Opts>['typedValue'] :
Opts['unmask'] extends Falsy ? InputMask<Opts>['value'] :
InputMask<Opts>['unmaskedValue']
;
export
type ExtractReactMaskOpts<
MaskElement extends InputMaskElement,
Props extends IMaskInputProps<MaskElement>,
> = Extract<Props, ReactMaskOpts>;
export
type ReactMaskProps<
MaskElement extends InputMaskElement,
Props extends IMaskInputProps<MaskElement>=AnyProps,
> = {
onAccept?: (value: UnmaskValue<ExtractReactMaskOpts<MaskElement, Props>>, maskRef: InputMask<ExtractMaskOpts<MaskElement, Props>>, e?: InputEvent) => void;
onComplete?: (value: UnmaskValue<ExtractReactMaskOpts<MaskElement, Props>>, maskRef: InputMask<ExtractMaskOpts<MaskElement, Props>>, e?: InputEvent) => void;
unmask?: ExtractReactMaskOpts<MaskElement, Props>['unmask'];
value?: UnmaskValue<ExtractReactMaskOpts<MaskElement, Props>>;
inputRef?: React.Ref<MaskElement>;
ref?: React.Ref<React.ComponentType<Props>>;
}
const MASK_PROPS: { [key in keyof (AllFactoryStaticOpts & ReactMaskProps<InputMaskElement, AllFactoryStaticOpts>)]: any } = {
// common
mask: PropTypes.oneOfType([
PropTypes.array,
PropTypes.func,
PropTypes.string,
PropTypes.instanceOf(RegExp),
PropTypes.oneOf([Date, Number, IMask.Masked]),
PropTypes.instanceOf(IMask.Masked as any),
]),
value: PropTypes.any,
unmask: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['typed']),
]),
prepare: PropTypes.func,
prepareChar: PropTypes.func,
validate: PropTypes.func,
commit: PropTypes.func,
overwrite: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['shift']),
]),
eager: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['append', 'remove']),
]),
skipInvalid: PropTypes.bool,
// events
onAccept: PropTypes.func,
onComplete: PropTypes.func,
// pattern
placeholderChar: PropTypes.string,
displayChar: PropTypes.string,
lazy: PropTypes.bool,
definitions: PropTypes.object,
blocks: PropTypes.object,
// enum
enum: PropTypes.arrayOf(PropTypes.string),
// range
maxLength: PropTypes.number,
from: PropTypes.number,
to: PropTypes.number,
// date
pattern: PropTypes.string,
format: PropTypes.func,
parse: PropTypes.func,
autofix: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['pad']),
]),
// number
radix: PropTypes.string,
thousandsSeparator: PropTypes.string,
mapToRadix: PropTypes.arrayOf(PropTypes.string),
scale: PropTypes.number,
normalizeZeros: PropTypes.bool,
padFractionalZeros: PropTypes.bool,
min: PropTypes.oneOfType([
PropTypes.number,
PropTypes.instanceOf(Date),
]),
max: PropTypes.oneOfType([
PropTypes.number,
PropTypes.instanceOf(Date),
]),
// dynamic
dispatch: PropTypes.func,
// ref
inputRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.object }),
]),
} as const;
const MASK_PROPS_NAMES = (Object.keys(MASK_PROPS) as Array<keyof typeof MASK_PROPS>).filter(p => p !== 'value');
const NON_MASK_OPTIONS_NAMES = ['value', 'unmask', 'onAccept', 'onComplete', 'inputRef'] as const;
export
type ReactElementProps<MaskElement extends InputMaskElement> =
Omit<Omit<React.HTMLProps<MaskElement>, keyof typeof MASK_PROPS>, typeof NON_MASK_OPTIONS_NAMES[number] | 'maxLength'>;
type NonMaskProps<
MaskElement extends InputMaskElement,
Props extends IMaskMixinProps<MaskElement>=AnyProps
> = Omit<Props, keyof FactoryOpts>;
export
type ReactMixinComponent<
MaskElement extends InputMaskElement,
Props extends IMaskMixinProps<MaskElement>=AnyProps,
> = React.ComponentType<
& ReactElementProps<MaskElement>
& { inputRef: React.Ref<MaskElement> }
& NonMaskProps<MaskElement, Props>
>;
export
type MaskPropsKeys = Exclude<keyof typeof MASK_PROPS, typeof NON_MASK_OPTIONS_NAMES[number]>;
const MASK_OPTIONS_NAMES = MASK_PROPS_NAMES.filter(pName =>
NON_MASK_OPTIONS_NAMES.indexOf(pName as typeof NON_MASK_OPTIONS_NAMES[number]) < 0
) as Array<MaskPropsKeys>;
export
type ExtractMaskOpts<
MaskElement extends InputMaskElement,
Props extends IMaskInputProps<MaskElement>,
> = Extract<Props, FactoryOpts>;
export
type IMaskMixinProps<MaskElement extends InputMaskElement> =
Omit<ReactMaskProps<MaskElement>, 'ref'> & FactoryOpts;
export
type IMaskInputProps< MaskElement extends InputMaskElement> =
ReactElementProps<MaskElement> & IMaskMixinProps<MaskElement>;
export default
function IMaskMixin<
MaskElement extends InputMaskElement,
Props extends IMaskInputProps<MaskElement>=AnyProps,
>(ComposedComponent: ReactMixinComponent<MaskElement, Props>) {
const MaskedComponent = class extends React.Component<Props> {
static displayName: string;
static propTypes: typeof MASK_PROPS;
declare element: MaskElement;
declare maskRef?: InputMask<ExtractMaskOpts<MaskElement, Props>>;
constructor (props: Props) {
super(props);
this._inputRef = this._inputRef.bind(this);
}
componentDidMount () {
if (!this.props.mask) return;
this.initMask();
}
componentDidUpdate () {
const props = this.props;
const maskOptions = this._extractMaskOptionsFromProps(props);
if (maskOptions.mask) {
if (this.maskRef) {
this.maskRef.updateOptions(maskOptions as any); // TODO fix
if ('value' in props && props.value !== undefined) this.maskValue = props.value;
} else {
this.initMask(maskOptions);
}
} else {
this.destroyMask();
if ('value' in props && props.value !== undefined) {
if ((this.element as HTMLElement)?.isContentEditable && (this.element as HTMLElement).tagName !== 'INPUT' && (this.element as HTMLElement).tagName !== 'TEXTAREA') (this.element as HTMLElement).textContent = props.value;
else (this.element as HTMLInputElement).value = props.value;
}
}
}
componentWillUnmount () {
this.destroyMask();
}
_inputRef (el: MaskElement) {
this.element = el;
if (this.props.inputRef) {
if (Object.prototype.hasOwnProperty.call(this.props.inputRef, 'current'))
(this.props.inputRef as React.MutableRefObject<MaskElement>).current = el;
else
(this.props.inputRef as React.RefCallback<MaskElement>)(el);
}
}
initMask (maskOptions: ExtractMaskOpts<MaskElement, Props> = this._extractMaskOptionsFromProps(this.props)) {
this.maskRef = IMask(this.element, maskOptions)
.on('accept', this._onAccept.bind(this))
.on('complete', this._onComplete.bind(this));
if ('value' in this.props && this.props.value !== undefined) this.maskValue = this.props.value;
}
destroyMask () {
if (this.maskRef) {
this.maskRef.destroy();
delete this.maskRef;
}
}
_extractMaskOptionsFromProps (props: Readonly<Props>): ExtractMaskOpts<MaskElement, Props> {
const { ...cloneProps }: Readonly<Props> = props;
// keep only mask options
(Object.keys(cloneProps) as Array<keyof Props>)
.filter(prop => MASK_OPTIONS_NAMES.indexOf(prop as MaskPropsKeys) < 0)
.forEach(nonMaskProp => {
delete cloneProps[nonMaskProp];
});
return cloneProps as ExtractMaskOpts<MaskElement, Props>;
}
_extractNonMaskProps (props: Readonly<Props>): NonMaskProps<MaskElement, Props> {
const { ...cloneProps } = props as Props;
(MASK_PROPS_NAMES as Array<keyof Props>).forEach(maskProp => {
if (maskProp !== 'maxLength') delete cloneProps[maskProp];
});
if (!('defaultValue' in cloneProps)) cloneProps.defaultValue = props.mask ? '' : cloneProps.value;
delete cloneProps.value;
return cloneProps as NonMaskProps<MaskElement, Props>;
}
get maskValue (): UnmaskValue<ExtractReactMaskOpts<MaskElement, Props>> {
if (!this.maskRef) return '' as UnmaskValue<ExtractReactMaskOpts<MaskElement, Props>>;
if (this.props.unmask === 'typed') return this.maskRef.typedValue;
if (this.props.unmask) return this.maskRef.unmaskedValue;
return this.maskRef.value;
}
set maskValue (value: UnmaskValue<ExtractReactMaskOpts<MaskElement, Props>>) {
if (!this.maskRef) return;
value = (value == null && this.props.unmask !== 'typed' ? '' : value) as UnmaskValue<ExtractReactMaskOpts<MaskElement, Props>>;
if (this.props.unmask === 'typed') this.maskRef.typedValue = value;
else if (this.props.unmask) this.maskRef.unmaskedValue = value;
else this.maskRef.value = value;
}
_onAccept (e?: InputEvent) {
if (this.props.onAccept && this.maskRef) this.props.onAccept(this.maskValue, this.maskRef, e);
}
_onComplete (e?: InputEvent) {
if (this.props.onComplete && this.maskRef) this.props.onComplete(this.maskValue, this.maskRef, e);
}
render () {
return React.createElement(ComposedComponent, {
...this._extractNonMaskProps(this.props),
inputRef: this._inputRef,
});
}
};
const nestedComponentName = ComposedComponent.displayName || ComposedComponent.name || 'Component';
MaskedComponent.displayName = `IMask(${nestedComponentName})`;
MaskedComponent.propTypes = MASK_PROPS;
return React.forwardRef<React.ComponentType<Props>, Props>(
(props, ref) => React.createElement(MaskedComponent, { ...props, ref })
);
}