PromucFlow_constructor/app/client/src/widgets/PhoneInputWidget/widget/index.tsx

329 lines
9.9 KiB
TypeScript
Raw Normal View History

import React from "react";
import { WidgetState } from "widgets/BaseWidget";
import { RenderModes, WidgetType } from "constants/WidgetConstants";
import PhoneInputComponent, { PhoneInputComponentProps } from "../component";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import {
ValidationTypes,
ValidationResponse,
} from "constants/WidgetValidation";
refactor: admin settings (#9906) * refactor admin settings feature * separated save-restart bar to separate component * created new CE dir to facilitate code split * created separate ee dir and exporting everything we have in ce file. * little mod * minor fix * splitting settings types config * using object literals for category types instead of enums * CE: support use of component for each category * minor style fix * authentication page UI changes implemented * github signup doc url added back * removed comments * routing updates * made subcategories listing in left pane optional * added muted saml to auth listing * added breadcrumbs and enabled button * created separate component for auth page and auth config * added callout and disconnect components * updated breadcrumbs component * minor updates to common components * updated warning callout and added icon * ce: test cases fixed * updated test file name * warning banner callout added on auth page * updated callout banner for form login * CE: Split config files * CE: moved the window declaration in EE file as its dependency will be updated in EE * CE: Splitting ApiConstants and SocialLogin constants * CE: split login page * CE: moved getSocialLoginButtonProps func to EE file as it's dependencies will be updated in EE * added key icon * CE: created a factory class to share social auths list * Minor style fix for social btns * Updated the third party auth styles * Small fixes to styling * ce: splitting forms constants * breadcrumbs implemented for all pages in admin settings * Settings breadcrumbs separated * splitted settings breadcrumbs between ce and ee * renamed default import * minor style fix * added login form config. * updated login/signup pages to use form login disabled config * removed common functionality outside * implemented breadcrumb component from scratch without using blueprint * removed unwanted code * Small style update * updated breadcrumb categories file name and breadcrumb icon * added cypress tests for admin settings auth page * added comments * update locator for upgrade button * added link for intercom on upgrade button * removed unnecessary file * minor style fix * style fix for auth option cards * split messages constant * fixed imports for message constants splitting. * added message constants * updated unit test cases * fixed messages import in cypress index * fixed messages import again, cypress fails to read re-exported objs. * added OIDC auth method on authentication page * updated import statements from ee to @appsmith * removed dead code * updated read more link UI * PR comments fixes * some UI fixes * used color and fonts from theme * fixed some imports * fixed some imports * removed warning imports * updated OIDC logo and auth method desc copies * css changes * css changes * css changes * updated cypress test for breadcrumb * moved callout component to ads as calloutv2 * UI changes for form fields * updated css for spacing between form fields * added sub-text on auth pages * added active class for breadcrumb item * added config for disable signup toggle and fixed UI issues of restart banner * fixed admin settings page bugs * assigned true as default state for signup * fixed messages import statements * updated code for PR comments related suggestions * reverted file path change in cypress support * updated cypress test * updated cypress test Co-authored-by: Ankita Kinger <ankita@appsmith.com>
2022-02-11 18:08:46 +00:00
import {
createMessage,
FIELD_REQUIRED_ERROR,
} from "@appsmith/constants/messages";
import { DerivedPropertiesMap } from "utils/WidgetFactory";
import {
getCountryCode,
ISDCodeDropdownOptions,
} from "../component/ISDCodeDropdown";
import { AutocompleteDataType } from "utils/autocomplete/TernServer";
import _ from "lodash";
import BaseInputWidget from "widgets/BaseInputWidget";
import derivedProperties from "./parsedDerivedProperties";
import { BaseInputWidgetProps } from "widgets/BaseInputWidget/widget";
import { mergeWidgetConfig } from "utils/helpers";
import {
AsYouType,
CountryCode,
parseIncompletePhoneNumber,
} from "libphonenumber-js";
import * as Sentry from "@sentry/react";
import log from "loglevel";
export function defaultValueValidation(
value: any,
props: PhoneInputWidgetProps,
_?: any,
): ValidationResponse {
const STRING_ERROR_MESSAGE = "This value must be string";
const EMPTY_ERROR_MESSAGE = "";
if (_.isObject(value)) {
return {
isValid: false,
parsed: JSON.stringify(value, null, 2),
messages: [STRING_ERROR_MESSAGE],
};
}
let parsed = value;
if (!_.isString(value)) {
try {
parsed = _.toString(value);
} catch (e) {
return {
isValid: false,
parsed: "",
messages: [STRING_ERROR_MESSAGE],
};
}
}
return {
isValid: _.isString(parsed),
parsed: parsed,
messages: [EMPTY_ERROR_MESSAGE],
};
}
class PhoneInputWidget extends BaseInputWidget<
PhoneInputWidgetProps,
WidgetState
> {
static getPropertyPaneConfig() {
return mergeWidgetConfig(
[
{
sectionName: "General",
children: [
{
propertyName: "allowDialCodeChange",
label: "Allow country code change",
helpText: "Search by country",
controlType: "SWITCH",
isJSConvertible: false,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
{
helpText: "Changes the country code",
propertyName: "dialCode",
label: "Default Country Code",
enableSearch: true,
dropdownHeight: "195px",
controlType: "DROP_DOWN",
placeholderText: "Search by code or country name",
options: ISDCodeDropdownOptions,
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.TEXT,
},
},
{
helpText:
"Sets the default text of the widget. The text is updated if the default text changes",
propertyName: "defaultText",
label: "Default Text",
controlType: "INPUT_TEXT",
placeholderText: "John Doe",
isBindProperty: true,
isTriggerProperty: false,
validation: {
type: ValidationTypes.FUNCTION,
params: {
fn: defaultValueValidation,
expected: {
type: "string",
example: `000 0000`,
autocompleteDataType: AutocompleteDataType.STRING,
},
},
},
},
{
propertyName: "allowFormatting",
label: "Enable Formatting",
helpText: "Formats the phone number as per the country selected",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
validation: { type: ValidationTypes.BOOLEAN },
},
],
},
],
super.getPropertyPaneConfig(),
);
}
static getDerivedPropertiesMap(): DerivedPropertiesMap {
return {
isValid: `{{(() => {${derivedProperties.isValid}})()}}`,
};
}
static getMetaPropertiesMap(): Record<string, any> {
return _.merge(super.getMetaPropertiesMap(), {
value: undefined,
});
}
getFormattedPhoneNumber(value: string) {
const countryCode = getCountryCode(this.props.dialCode);
let formattedValue;
if (!value) {
formattedValue = value;
} else if (this.props.allowFormatting) {
formattedValue = new AsYouType(countryCode as CountryCode).input(value);
} else {
formattedValue = parseIncompletePhoneNumber(value);
}
return formattedValue;
}
componentDidMount() {
//format the defaultText and store it in text
if (!!this.props.text) {
try {
const formattedValue = this.getFormattedPhoneNumber(this.props.text);
this.props.updateWidgetMetaProperty("value", this.props.text);
this.props.updateWidgetMetaProperty("text", formattedValue);
} catch (e) {
log.error(e);
Sentry.captureException(e);
}
}
}
componentDidUpdate(prevProps: PhoneInputWidgetProps) {
if (
this.props.renderMode === RenderModes.CANVAS &&
prevProps.dialCode !== this.props.dialCode
) {
this.onISDCodeChange(this.props.dialCode);
}
if (prevProps.allowFormatting !== this.props.allowFormatting) {
const formattedValue = this.getFormattedPhoneNumber(this.props.value);
this.props.updateWidgetMetaProperty("text", formattedValue);
}
// When the default text changes
if (
prevProps.text !== this.props.text &&
this.props.text === this.props.defaultText
) {
const formattedValue = this.getFormattedPhoneNumber(this.props.text);
this.props.updateWidgetMetaProperty(
"value",
parseIncompletePhoneNumber(formattedValue),
);
this.props.updateWidgetMetaProperty("text", formattedValue);
// If defaultText property has changed, reset isDirty to false
this.props.updateWidgetMetaProperty("isDirty", false);
}
}
onISDCodeChange = (dialCode?: string) => {
const countryCode = getCountryCode(dialCode);
if (this.props.renderMode === RenderModes.CANVAS) {
super.updateWidgetProperty("dialCode", dialCode);
super.updateWidgetProperty("countryCode", countryCode);
} else {
this.props.updateWidgetMetaProperty("dialCode", dialCode);
this.props.updateWidgetMetaProperty("countryCode", countryCode);
}
if (this.props.value && this.props.allowFormatting) {
const formattedValue = this.getFormattedPhoneNumber(this.props.value);
this.props.updateWidgetMetaProperty("text", formattedValue);
}
};
onValueChange = (value: string) => {
let formattedValue;
// Don't format, as value is typed, when user is deleting
if (value && value.length > this.props.text.length) {
formattedValue = this.getFormattedPhoneNumber(value);
} else {
formattedValue = value;
}
this.props.updateWidgetMetaProperty(
"value",
parseIncompletePhoneNumber(formattedValue),
);
this.props.updateWidgetMetaProperty("text", formattedValue, {
triggerPropertyName: "onTextChanged",
dynamicString: this.props.onTextChanged,
event: {
type: EventType.ON_TEXT_CHANGE,
},
});
if (!this.props.isDirty) {
this.props.updateWidgetMetaProperty("isDirty", true);
}
if (value === this.props.defaultText) {
this.props.updateWidgetMetaProperty("isDirty", true);
}
};
handleFocusChange = (focusState: boolean) => {
super.handleFocusChange(focusState);
};
handleKeyDown = (
e:
| React.KeyboardEvent<HTMLTextAreaElement>
| React.KeyboardEvent<HTMLInputElement>,
) => {
super.handleKeyDown(e);
};
getPageView() {
const value = this.props.text ?? "";
const isInvalid =
"isValid" in this.props && !this.props.isValid && !!this.props.isDirty;
const countryCode = this.props.countryCode;
const conditionalProps: Partial<PhoneInputComponentProps> = {};
conditionalProps.errorMessage = this.props.errorMessage;
if (this.props.isRequired && value.length === 0) {
conditionalProps.errorMessage = createMessage(FIELD_REQUIRED_ERROR);
}
return (
<PhoneInputComponent
allowDialCodeChange={this.props.allowDialCodeChange}
autoFocus={this.props.autoFocus}
compactMode
countryCode={countryCode}
defaultValue={this.props.defaultText}
dialCode={this.props.dialCode}
disableNewLineOnPressEnterKey={!!this.props.onSubmit}
disabled={this.props.isDisabled}
iconAlign={this.props.iconAlign}
iconName={this.props.iconName}
inputType={this.props.inputType}
isInvalid={isInvalid}
isLoading={this.props.isLoading}
label={this.props.label}
labelStyle={this.props.labelStyle}
labelTextColor={this.props.labelTextColor}
labelTextSize={this.props.labelTextSize}
onFocusChange={this.handleFocusChange}
onISDCodeChange={this.onISDCodeChange}
onKeyDown={this.handleKeyDown}
onValueChange={this.onValueChange}
placeholder={this.props.placeholderText}
showError={!!this.props.isFocused}
tooltip={this.props.tooltip}
value={value}
widgetId={this.props.widgetId}
{...conditionalProps}
/>
);
}
static getWidgetType(): WidgetType {
return "PHONE_INPUT_WIDGET";
}
}
export interface PhoneInputWidgetProps extends BaseInputWidgetProps {
dialCode?: string;
countryCode?: CountryCode;
defaultText?: string;
allowDialCodeChange: boolean;
}
export default PhoneInputWidget;