Skip to content

Commit

Permalink
feat(strapi-plugin-uuid-field): create strapi uuid field
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKdhim87 authored and Robbert committed Apr 15, 2024
1 parent 1443adc commit 81839a9
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ module.exports = {
'./packages/strapi-plugin-uniform-product-name/tsconfig.server.json',
'./packages/strapi-tiptap-editor/tsconfig.json',
'./packages/strapi-tiptap-editor/tsconfig.server.json',
'./packages/strapi-plugin-uuid-field/tsconfig.json',
'./packages/strapi-plugin-uuid-field/tsconfig.server.json',
'./packages/ui/tsconfig.json',
'./packages/upl/tsconfig.json',
],
Expand Down
1 change: 1 addition & 0 deletions packages/strapi-plugin-uuid-field/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Strapi plugin uuid-field
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { TextInput } from '@strapi/design-system';
import React, { forwardRef, useEffect } from 'react';
import type { ForwardedRef, PropsWithChildren } from 'react';
import { useIntl } from 'react-intl';
import { v4 } from 'uuid';

type AttributesOptionTypes = {
id?: string;
defaultMessage?: string;
};

type Attributes = {
type: string;
customField: string;
pluginOptions: {
i18n: { localized: boolean };
};
};

type Target = {
name: string;
value: string;
type: string;
};

type OnChangeParamTypes = {
target: Target;
};
interface UUIDInputProps {
intlLabel: AttributesOptionTypes;
// eslint-disable-next-line no-unused-vars
onChange: (param: OnChangeParamTypes) => {};
attribute: Attributes;
name: string;
description: AttributesOptionTypes;
disabled: boolean;
error: string;
labelAction: string;
required: boolean;
value: string;
placeholder: AttributesOptionTypes;
}

const UUIDInput = forwardRef(
(
{
value: initialValue,
onChange,
name,
intlLabel,
required,
attribute,
description,
placeholder,
error,
}: PropsWithChildren<UUIDInputProps>,
ref: ForwardedRef<UUIDInputProps>,
) => {
const { formatMessage } = useIntl();
useEffect(() => {
if (!initialValue) {
onChange({ target: { name, value: v4(), type: attribute.type } });
}
}, [initialValue]);

return (
<TextInput
ref={ref}
placeholder={placeholder && formatMessage(placeholder)}
label={intlLabel && formatMessage(intlLabel)}
name={name}
hint={description && formatMessage(description)}
onChange={onChange}
value={initialValue}
required={required}
disabled={true}
error={error}
/>
);
},
);
UUIDInput.displayName = 'UUIDInput';
export default UUIDInput;
70 changes: 70 additions & 0 deletions packages/strapi-plugin-uuid-field/admin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { prefixPluginTranslations } from '@strapi/helper-plugin';
import { Uid } from '@strapi/icons';
import pluginId from './pluginId';
import getTrad from './utils/getTrad';
export default {
register(app: any) {
app.customFields.register({
name: pluginId,
pluginId,
type: 'string',
icon: Uid,
intlLabel: {
id: getTrad('uuid-field.label'),
defaultMessage: 'UUID',
},
intlDescription: {
id: getTrad('uuid-field.description'),
defaultMessage: 'Generate UUID',
},
components: {
Input: async () => import('./components/Input'),
},
options: {
advanced: [
{
sectionTitle: {
id: 'global.settings',
defaultMessage: 'Settings',
},
items: [
{
name: 'required',
type: 'checkbox',
intlLabel: {
id: 'form.attribute.item.requiredField',
defaultMessage: 'Required field',
},
description: {
id: 'form.attribute.item.requiredField.description',
defaultMessage: "You won't be able to create an entry if this field is empty",
},
},
],
},
],
},
});
},
async registerTrads({ locales }: any) {
const importedTrads = await Promise.all(
locales.map((locale: any) => {
return import(`./translations/${locale}.json`)
.then(({ default: data }) => {
return {
data: prefixPluginTranslations(data, pluginId),
locale,
};
})
.catch(() => {
return {
data: {},
locale,
};
});
}),
);

return Promise.resolve(importedTrads);
},
};
5 changes: 5 additions & 0 deletions packages/strapi-plugin-uuid-field/admin/src/pluginId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pluginPkg from '../../package.json';

const pluginId = pluginPkg.name.replace(/^@frameless\/(@[^-,.][\w,-]+\/|strapi-)plugin-/i, '');

export default pluginId;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"uuid-field.label": "UUID",
"uuid-field.description": "Generate a UUID"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"uuid-field.label": "UUID",
"uuid-field.description": "Genereer UUID"
}
5 changes: 5 additions & 0 deletions packages/strapi-plugin-uuid-field/admin/src/utils/getTrad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pluginId from '../pluginId';

const getTrad = (id: string) => `${pluginId}.${id}`;

export default getTrad;
5 changes: 5 additions & 0 deletions packages/strapi-plugin-uuid-field/custom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module '@strapi/design-system/*';
declare module '@strapi/design-system';
declare module '@strapi/icons';
declare module '@strapi/icons/*';
declare module '@strapi/helper-plugin';
45 changes: 45 additions & 0 deletions packages/strapi-plugin-uuid-field/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@frameless/strapi-plugin-uuid-field",
"version": "0.0.0-semantically-released",
"description": "A strapi custom field for generating an UUID for each document",
"keywords": [],
"private": false,
"strapi": {
"name": "uuid-field",
"description": "Generate UUID",
"kind": "plugin",
"displayName": "Generate UUID"
},
"repository": {
"type": "git+ssh",
"url": "git@github.com:frameless/strapi.git"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"author": {
"name": ""
},
"license": "",
"peerDependencies": {
"@strapi/strapi": ">=4.0.0"
},
"engines": {
"node": "20.x.x"
},
"dependencies": {
"@strapi/design-system": "1.12.2",
"@strapi/icons": "1.12.2",
"uuid": "9.0.1"
},
"devDependencies": {
"@types/uuid": "9.0.8",
"rimraf": "5.0.5"
},
"scripts": {
"build": "tsc -p tsconfig.server.json",
"clean": "rimraf dist",
"prebuild": "yarn clean",
"watch": "tsc -p tsconfig.server.json -w"
}
}
5 changes: 5 additions & 0 deletions packages/strapi-plugin-uuid-field/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { register } from './register';

export default {
register,
};
10 changes: 10 additions & 0 deletions packages/strapi-plugin-uuid-field/server/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Strapi } from '@strapi/strapi';
import plugin from '../admin/src/pluginId';

export const register = ({ strapi }: { strapi: Strapi }) => {
strapi.customFields.register({
name: plugin,
plugin,
type: 'string',
});
};
3 changes: 3 additions & 0 deletions packages/strapi-plugin-uuid-field/strapi-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./admin/src').default;
3 changes: 3 additions & 0 deletions packages/strapi-plugin-uuid-field/strapi-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./dist/server');
10 changes: 10 additions & 0 deletions packages/strapi-plugin-uuid-field/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@strapi/typescript-utils/tsconfigs/admin",
"compilerOptions": {
"target": "ESNext",
"strict": true,
"lib": ["dom", "dom.iterable", "esnext"]
},
"include": ["admin", "custom.d.ts"],
"exclude": ["node_modules/", "dist/", "server/", "**/*.test.ts"]
}
11 changes: 11 additions & 0 deletions packages/strapi-plugin-uuid-field/tsconfig.server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@strapi/typescript-utils/tsconfigs/server",

"compilerOptions": {
"outDir": "dist",
"rootDir": "."
},

"include": ["server", "server/**/*.json"],
"exclude": ["node_modules/", "dist/", "admin/", "**/*.test.ts"]
}

0 comments on commit 81839a9

Please sign in to comment.