carbon-custom-elements
Version:
A Carbon Design System variant that's as easy to use as native HTML elements, with no framework tax, no framework silo.
1 lines • 4.03 kB
Source Map (JSON)
{"version":3,"sources":["globals/mixins/validity.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;GAEG;AACH,oBAAY,iBAAiB;IAC3B;;OAEG;IACH,QAAQ,KAAK;IAEb;;OAEG;IACH,cAAc,aAAa;CAC5B;AAED;;;GAGG;AACH,QAAA,MAAM,aAAa;;QAIf;;;;WAIG;;QAUH;;;;WAIG;;QAMH;;WAEG;;QAGH;;WAEG;;QAGH;;WAEG;;QAGH;;WAEG;;QAGH;;WAEG;;QAGH;;;;WAIG;;QAuBH;;;WAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAON,CAAC;AAEF,eAAe,aAAa,CAAC","file":"validity.d.ts","sourcesContent":["/**\n * @license\n *\n * Copyright IBM Corp. 2020\n *\n * This source code is licensed under the Apache-2.0 license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\n/**\n * Form validation status.\n */\nexport enum VALIDATION_STATUS {\n /**\n * One indicating no validation error.\n */\n NO_ERROR = '',\n\n /**\n * One indicating missing required value.\n */\n ERROR_REQUIRED = 'required',\n}\n\n/**\n * @param Base The base class.\n * @returns A mix-in implementing `.setCustomValidity()` method.\n */\nconst ValidityMixin = <T extends Constructor<HTMLElement>>(Base: T) => {\n abstract class ValidityMixinImpl extends Base {\n // Not using TypeScript `protected` due to: microsoft/TypeScript#17744\n // Using `string` instead of `VALIDATION_STATUS` until we can require TypeScript 3.8\n /**\n * @param state The form validation status.\n * @returns The form validation error messages associated with the given status.\n * @protected\n */\n _getValidityMessage(state: string) {\n return {\n [VALIDATION_STATUS.NO_ERROR]: '',\n [VALIDATION_STATUS.ERROR_REQUIRED]: this.requiredValidityMessage,\n }[state];\n }\n\n // Not using TypeScript `protected` due to: microsoft/TypeScript#17744\n // Using `string` instead of `VALIDATION_STATUS` until we can require TypeScript 3.8\n /**\n * Checks if the value meets the constraints.\n * @returns `VALIDATION_STATUS.NO_ERROR` if the value meets the constraints. Some other values otherwise.\n * @protected\n */\n _testValidity(): string {\n const { required, value } = this;\n return required && !value ? VALIDATION_STATUS.ERROR_REQUIRED : VALIDATION_STATUS.NO_ERROR;\n }\n\n /**\n * `true` to show the UI of the invalid state.\n */\n abstract invalid: boolean;\n\n /**\n * `true` if the value is required.\n */\n abstract required: boolean;\n\n /**\n * The special validity message for `required`.\n */\n abstract requiredValidityMessage: string;\n\n /**\n * The validity message.\n */\n abstract validityMessage: string;\n\n /**\n * The value.\n */\n abstract value: string;\n\n /**\n * Checks if the value meets the constraints.\n * Fires cancelable `invalid` event if it doesn't.\n * @returns `true` if the value meets the constraints. `false` otherwise.\n */\n checkValidity() {\n const status = this._testValidity();\n if (status !== VALIDATION_STATUS.NO_ERROR) {\n if (\n this.dispatchEvent(\n new CustomEvent('invalid', {\n bubbles: false,\n cancelable: true,\n composed: false,\n })\n )\n ) {\n this.invalid = true;\n this.validityMessage = this._getValidityMessage(status);\n }\n return false;\n }\n this.invalid = false;\n this.validityMessage = '';\n return true;\n }\n\n /**\n * Sets the given custom validity message.\n * @param validityMessage The custom validity message\n */\n setCustomValidity(validityMessage: string) {\n this.invalid = Boolean(validityMessage);\n this.validityMessage = validityMessage;\n }\n }\n return ValidityMixinImpl;\n};\n\nexport default ValidityMixin;\n"]}