MCPcopy Create free account
hub / github.com/Monogatari/Monogatari / didMount

Method didMount

src/components/text-input/index.ts:117–174  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

115 }
116
117 override didMount(): Promise<void> {
118 this.addEventListener('submit', (event) => {
119 event.stopPropagation();
120 event.preventDefault();
121 let inputValue: string | string[] = '';
122 // Retrieve the value submitted
123 if (this.props.type === 'radio') {
124 const checked = this.element().find('[data-content="field"]:checked');
125 if (checked.exists()) {
126 inputValue = checked.value() as string;
127 } else {
128 inputValue = '';
129 }
130
131 } else if (this.props.type === 'checkbox') {
132 inputValue = [];
133 this.element().find('[data-content="field"]:checked').each((element) => {
134 (inputValue as string[]).push($_(element).value() as string);
135 });
136 } else {
137 inputValue = this.content('field').value() as string;
138 }
139
140 // Run the validation function asynchronously. If it returns false,
141 // it means the input is invalid and we have to show the warning message.
142 this.engine.assertAsync(this.props.validate as (...args: unknown[]) => unknown, this.engine, [inputValue]).then(() => {
143 // Once validation was done, we run the Save function where usually,
144 // the input received will be saved on the storage or used for other
145 // actions.
146 this.engine.assertAsync(this.props.onSubmit as (...args: unknown[]) => unknown, this.engine, [inputValue]).then(() => {
147 // Nothing to do here
148 }).catch(() => {
149 // Nothing to do here
150 }).finally(() => {
151 this.remove();
152 this.props.callback();
153 });
154 }).catch(() => {
155 // Show the warning message since the input was invalid
156 this.content('warning').text(this.engine.replaceVariables(this.props.warning));
157 });
158 });
159
160 // For inputs that require a text field, we place the default value after
161 // mount instead of in-creation because this way, the cursor will be placed
162 // at the end of the default value. If we did it in-creation, it would
163 // be placed at the start.
164 const text: TextInputType[] = ['text', 'textarea', 'password', 'email', 'url', 'number', 'color'];
165 const { type, default: defaultValue } = this.props;
166
167 if (text.indexOf(type) > -1) {
168 if (defaultValue !== null && defaultValue !== '') {
169 this.content('field').value(defaultValue);
170 }
171 }
172 this.content('field').get(0)?.focus();
173 return Promise.resolve();
174 }

Callers

nothing calls this directly

Calls 7

assertAsyncMethod · 0.80
replaceVariablesMethod · 0.80
eachMethod · 0.65
removeMethod · 0.65
getMethod · 0.65
elementMethod · 0.45
contentMethod · 0.45

Tested by

no test coverage detected