Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 93x 93x 93x 93x 93x 632x 632x 632x 632x 632x 632x 632x 632x 632x 632x 632x 114x 114x 114x 2x 8x 2x 1x 216x 7x 13x 2x 5x 2x 1x 93x 42x 42x 35x 35x 35x 35x 3x 3x 35x 35x 42x 93x 43x 43x 18x 18x 18x 18x 43x 93x | import { Accessor, FormatterParserProvider, Loader, Mask } from '@zeedhi/core';
import { Input } from '../zd-input/input';
import { InputFactory } from '../zd-input/input-factory';
// import { ITextInput, ITextInputEvents } from './interfaces';
import { ITextInput } from './interfaces';
const textInputDefaults = {
valueWithPrefix: false,
prefix: '',
valueWithSuffix: false,
suffix: '',
mask: '',
};
/**
* Base class for Text component.
*/
export class TextInput extends Input implements ITextInput {
/**
* Appends an icon inside text input component.
*/
public appendIcon = '';
/**
* Appends an icon outside text input component.
*/
public appendOuterIcon = '';
// /**
// * Defines text input events.
// */
// public events!: ITextInputEvents;
/**
* Displays a prefix text inside text input component. Overrides prependIcon prop.
*/
public prefix: string = textInputDefaults.prefix;
/**
* Prepends an icon inside text input component.
*/
public prependIcon = '';
/**
* Prepends an icon outside text input component.
*/
public prependOuterIcon = '';
/**
* Displays a suffix text inside text input component. Overrides appendIcon prop.
*/
public suffix: string = textInputDefaults.suffix;
/**
* Sets input type.
*/
public type = '';
/**
* Defines text input value should concat the prefix text.
*/
public valueWithPrefix: boolean = textInputDefaults.valueWithPrefix;
/**
* Defines text input value should concat the suffix text.
*/
public valueWithSuffix: boolean = textInputDefaults.valueWithSuffix;
public inputMode?: string | undefined;
protected formatterFn = FormatterParserProvider.getFormatter('ZdTextInput');
protected parserFn = FormatterParserProvider.getParser('ZdTextInput');
/* istanbul ignore next */
/**
* Create a new TextInput.
* @param props TextInput properties
*/
constructor(props: ITextInput) {
super(props);
this.appendIcon = this.getInitValue('appendIcon', props.appendIcon, this.appendIcon);
this.appendOuterIcon = this.getInitValue('appendOuterIcon', props.appendOuterIcon, this.appendOuterIcon);
this.prefix = this.getInitValue('prefix', props.prefix, this.prefix);
this.prependIcon = this.getInitValue('prependIcon', props.prependIcon, this.prependIcon);
this.prependOuterIcon = this.getInitValue('prependOuterIcon', props.prependOuterIcon, this.prependOuterIcon);
this.suffix = this.getInitValue('suffix', props.suffix, this.suffix);
this.type = props.type || this.type;
this.valueWithPrefix = this.getInitValue('valueWithPrefix', props.valueWithPrefix, this.valueWithPrefix);
this.valueWithSuffix = this.getInitValue('valueWithSuffix', props.valueWithSuffix, this.valueWithSuffix);
this.inputMode = this.getInitValue('inputMode', props.inputMode, this.inputMode);
}
public onCreated(): void {
super.onCreated();
this.addKeyMap();
}
private addKeyMap() {
this.keyMap = {
...this.keyMap,
'ctrl+space': {
event: this.callSingleIconEvent.bind(this),
stop: true,
active: true,
input: true,
},
};
}
private callSingleIconEvent({ event, element }: any) {
const iconsProps = ['prependIcon', 'prependOuterIcon', 'appendIcon', 'appendOuterIcon'];
const icons = iconsProps.filter((prop) => !!(this as any)[prop]);
if (icons.length === 1) {
this.callEvent(`${icons[0]}Click`, { event, element, component: this });
}
}
/**
* Retrieves a formatted value without valueSuffix and with mask.
* @param value Text value
* @returns Text formatted value
*/
public formatter(value: string) {
return this.formatterFn(value, this);
}
/**
* Retrieves a parsed value with prefix and suffix if it is needed and without mask.
* @param value Text value
* @returns Text parsed value
*/
public parser(value: string) {
return this.parserFn(value, this);
}
/**
* Triggered when the appended icon is clicked.
* @param event DOM event
* @param element Text input
*/
public appendIconClick(event: Event, element: any) {
this.callEvent('appendIconClick', { event, element, component: this });
}
/**
* Triggered when the appended outer icon is clicked.
* @param event DOM event
* @param element Text input
*/
public appendOuterIconClick(event: Event, element: any) {
this.callEvent('appendOuterIconClick', { event, element, component: this });
}
/**
* Triggered when the prepended icon is clicked.
* @param event DOM event
* @param element Text input
*/
public prependIconClick(event: Event, element: any) {
this.callEvent('prependIconClick', { event, element, component: this });
}
/**
* Triggered when the prepended outer icon is clicked.
* @param event DOM event
* @param element Text input
*/
public prependOuterIconClick(event: Event, element: any) {
this.callEvent('prependOuterIconClick', { event, element, component: this });
}
public getMask() {
return this.mask;
}
}
FormatterParserProvider.registerFormatter(
'ZdTextInput',
(
value: any,
{
valueWithPrefix = textInputDefaults.valueWithPrefix,
prefix = textInputDefaults.prefix,
valueWithSuffix = textInputDefaults.valueWithSuffix,
suffix = textInputDefaults.suffix,
mask = textInputDefaults.mask,
}: any = {},
) => {
let formatted = value;
if (formatted) {
formatted = valueWithPrefix ? formatted.substring(prefix.length) : formatted;
formatted = valueWithSuffix ? formatted.slice(0, -suffix.length) : formatted;
let maskDef = mask;
if (Accessor.isAccessorDefinition(maskDef)) {
const [controller, accessor] = Accessor.getAccessor(maskDef);
maskDef = Loader.getInstance(controller)[accessor];
}
const maskValue = typeof maskDef === 'function' ? maskDef(Mask.getValueWithoutMask(formatted)) : maskDef;
formatted = maskValue ? Mask.getValueWithMask(Mask.getValueWithoutMask(formatted), maskValue) : formatted;
}
return formatted;
},
);
FormatterParserProvider.registerParser(
'ZdTextInput',
(
value: any,
{
valueWithPrefix = textInputDefaults.valueWithPrefix,
prefix = textInputDefaults.prefix,
valueWithSuffix = textInputDefaults.valueWithSuffix,
suffix = textInputDefaults.suffix,
mask = textInputDefaults.mask,
}: any = {},
) => {
let formatted = value;
if (formatted) {
const prefixValue = valueWithPrefix ? prefix : '';
const suffixValue = valueWithSuffix ? suffix : '';
formatted = mask ? Mask.getValueWithoutMask(formatted) : formatted;
formatted = prefixValue + formatted + suffixValue;
}
return formatted;
},
);
InputFactory.register('ZdTextInput', TextInput);
|