-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strapi-plugin-uuid-field): create strapi uuid field
- Loading branch information
1 parent
1443adc
commit 81839a9
Showing
16 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Strapi plugin uuid-field |
83 changes: 83 additions & 0 deletions
83
packages/strapi-plugin-uuid-field/admin/src/components/Input/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
4 changes: 4 additions & 0 deletions
4
packages/strapi-plugin-uuid-field/admin/src/translations/en.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"uuid-field.label": "UUID", | ||
"uuid-field.description": "Generate a UUID" | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/strapi-plugin-uuid-field/admin/src/translations/nl.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"uuid-field.label": "UUID", | ||
"uuid-field.description": "Genereer UUID" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { register } from './register'; | ||
|
||
export default { | ||
register, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./admin/src').default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./dist/server'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |