mirror of
https://github.com/twentyhq/twenty.git
synced 2024-12-19 17:42:27 +03:00
Adds URL validation (#1155)
This commit is contained in:
parent
c91844071a
commit
ee5ac11f98
@ -7,6 +7,7 @@ import {
|
|||||||
import { useCurrentRowEntityId } from '@/ui/table/hooks/useCurrentEntityId';
|
import { useCurrentRowEntityId } from '@/ui/table/hooks/useCurrentEntityId';
|
||||||
import { useUpdateEntityField } from '@/ui/table/hooks/useUpdateEntityField';
|
import { useUpdateEntityField } from '@/ui/table/hooks/useUpdateEntityField';
|
||||||
import { tableEntityFieldFamilySelector } from '@/ui/table/states/tableEntityFieldFamilySelector';
|
import { tableEntityFieldFamilySelector } from '@/ui/table/states/tableEntityFieldFamilySelector';
|
||||||
|
import { isURL } from '~/utils/is-url';
|
||||||
|
|
||||||
import { TextCellEdit } from './TextCellEdit';
|
import { TextCellEdit } from './TextCellEdit';
|
||||||
|
|
||||||
@ -30,6 +31,8 @@ export function GenericEditableURLCellEditMode({ viewField }: OwnProps) {
|
|||||||
function handleSubmit(newText: string) {
|
function handleSubmit(newText: string) {
|
||||||
if (newText === fieldValue) return;
|
if (newText === fieldValue) return;
|
||||||
|
|
||||||
|
if (!isURL(newText)) return;
|
||||||
|
|
||||||
setFieldValue(newText);
|
setFieldValue(newText);
|
||||||
|
|
||||||
if (currentRowEntityId && updateField) {
|
if (currentRowEntityId && updateField) {
|
||||||
|
35
front/src/utils/__tests__/is-url.test.ts
Normal file
35
front/src/utils/__tests__/is-url.test.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { isURL } from '~/utils/is-url';
|
||||||
|
|
||||||
|
describe('isURL', () => {
|
||||||
|
it(`should return false if null`, () => {
|
||||||
|
expect(isURL(null)).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return false if undefined`, () => {
|
||||||
|
expect(isURL(undefined)).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return true if string google`, () => {
|
||||||
|
expect(isURL('google')).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return true if string google.com`, () => {
|
||||||
|
expect(isURL('google.com')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return true if string bbc.co.uk`, () => {
|
||||||
|
expect(isURL('bbc.co.uk')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return true if string web.io`, () => {
|
||||||
|
expect(isURL('web.io')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return true if string x.com`, () => {
|
||||||
|
expect(isURL('x.com')).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should return true if string 2.com`, () => {
|
||||||
|
expect(isURL('2.com')).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
10
front/src/utils/is-url.ts
Normal file
10
front/src/utils/is-url.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { isDefined } from './isDefined';
|
||||||
|
|
||||||
|
export function isURL(url: string | undefined | null) {
|
||||||
|
return (
|
||||||
|
isDefined(url) &&
|
||||||
|
/^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/.test(
|
||||||
|
url,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user