2023-12-15 14:39:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-01-15 22:59:36 +00:00
|
|
|
import type { AllHTMLAttributes, AriaAttributes } from 'react';
|
2023-12-15 14:39:22 +00:00
|
|
|
|
2023-12-15 12:45:38 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getSetting } from './utils';
|
2024-09-03 09:20:46 +00:00
|
|
|
import { SelectOption } from '../../base/components';
|
2023-12-15 12:45:38 +00:00
|
|
|
|
2024-01-15 22:59:36 +00:00
|
|
|
// A list of attributes that can be added to a custom field when registering it.
|
|
|
|
type CustomFieldAttributes = Pick<
|
|
|
|
AllHTMLAttributes< HTMLInputElement >,
|
|
|
|
| 'maxLength'
|
|
|
|
| 'readOnly'
|
|
|
|
| 'pattern'
|
|
|
|
| 'title'
|
|
|
|
| 'autoCapitalize'
|
|
|
|
| 'autoComplete'
|
|
|
|
> &
|
|
|
|
AriaAttributes;
|
|
|
|
|
2024-01-05 17:35:28 +00:00
|
|
|
export interface FormField {
|
2023-12-15 12:45:38 +00:00
|
|
|
// The label for the field.
|
|
|
|
label: string;
|
|
|
|
// The label for the field if made optional.
|
|
|
|
optionalLabel: string;
|
|
|
|
// The HTML autocomplete attribute value. See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete
|
|
|
|
autocomplete: string;
|
|
|
|
// How this field value is capitalized.
|
|
|
|
autocapitalize?: string;
|
|
|
|
// Set to true if the field is required.
|
|
|
|
required: boolean;
|
|
|
|
// Set to true if the field should not be rendered.
|
|
|
|
hidden: boolean;
|
|
|
|
// Fields will be sorted and render in this order, lowest to highest.
|
|
|
|
index: number;
|
|
|
|
// The type of input to render. Defaults to text.
|
|
|
|
type?: string;
|
2023-12-15 14:39:22 +00:00
|
|
|
// The options if this is a select field
|
2024-09-03 09:20:46 +00:00
|
|
|
options?: SelectOption[];
|
2024-07-26 16:38:44 +00:00
|
|
|
// The placeholder for the field, only applicable for select fields.
|
|
|
|
placeholder?: string;
|
2024-01-15 22:59:36 +00:00
|
|
|
// Additional attributes added when registering a field. String in key is required for data attributes.
|
|
|
|
attributes?: Record< keyof CustomFieldAttributes, string >;
|
2023-12-15 12:45:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 17:35:28 +00:00
|
|
|
export interface LocaleSpecificFormField extends Partial< FormField > {
|
2023-12-15 12:45:38 +00:00
|
|
|
priority?: number | undefined;
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:35:28 +00:00
|
|
|
export interface CoreAddressForm {
|
|
|
|
first_name: FormField;
|
|
|
|
last_name: FormField;
|
|
|
|
company: FormField;
|
|
|
|
address_1: FormField;
|
|
|
|
address_2: FormField;
|
|
|
|
country: FormField;
|
|
|
|
city: FormField;
|
|
|
|
state: FormField;
|
|
|
|
postcode: FormField;
|
|
|
|
phone: FormField;
|
2023-12-15 12:45:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 17:35:28 +00:00
|
|
|
export interface CoreContactForm {
|
|
|
|
email: FormField;
|
|
|
|
}
|
2023-12-15 12:45:38 +00:00
|
|
|
|
2024-01-05 17:35:28 +00:00
|
|
|
export type AddressForm = CoreAddressForm & Record< string, FormField >;
|
|
|
|
export type ContactForm = CoreContactForm & Record< string, FormField >;
|
|
|
|
export type FormFields = AddressForm & ContactForm;
|
|
|
|
export type AddressFormValues = Omit< ShippingAddress, 'email' >;
|
|
|
|
export type ContactFormValues = { email: string };
|
2024-01-10 14:20:06 +00:00
|
|
|
export type AdditionalInformationFormValues = Record< string, string >;
|
|
|
|
export type FormType =
|
|
|
|
| 'billing'
|
|
|
|
| 'shipping'
|
|
|
|
| 'contact'
|
|
|
|
| 'additional-information';
|
2023-12-15 12:45:38 +00:00
|
|
|
|
|
|
|
export interface CoreAddress {
|
|
|
|
first_name: string;
|
|
|
|
last_name: string;
|
|
|
|
company: string;
|
|
|
|
address_1: string;
|
|
|
|
address_2: string;
|
|
|
|
country: string;
|
|
|
|
city: string;
|
|
|
|
state: string;
|
|
|
|
postcode: string;
|
|
|
|
phone: string;
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:35:28 +00:00
|
|
|
export type AdditionalValues = Record<
|
|
|
|
Exclude< string, keyof CoreAddress >,
|
|
|
|
string | boolean
|
|
|
|
>;
|
2023-12-15 12:45:38 +00:00
|
|
|
|
2024-01-05 17:35:28 +00:00
|
|
|
export type ShippingAddress = CoreAddress;
|
2023-12-15 12:45:38 +00:00
|
|
|
export interface BillingAddress extends ShippingAddress {
|
|
|
|
email: string;
|
|
|
|
}
|
2024-01-05 17:35:28 +00:00
|
|
|
|
|
|
|
export type KeyedFormField = FormField & {
|
|
|
|
key: keyof FormFields;
|
|
|
|
errorMessage?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CountryAddressForm = Record< string, FormFields >;
|
|
|
|
|
|
|
|
export type FormFieldsConfig = Record< keyof FormFields, Partial< FormField > >;
|
2023-12-15 12:45:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default field properties.
|
|
|
|
*/
|
2024-01-05 17:35:28 +00:00
|
|
|
export const defaultFields: FormFields =
|
|
|
|
getSetting< FormFields >( 'defaultFields' );
|
2023-12-15 12:45:38 +00:00
|
|
|
|
|
|
|
export default defaultFields;
|