From adb8eef93a3df1fef5c83270e3aa5e64fb2a0a45 Mon Sep 17 00:00:00 2001 From: -l Date: Wed, 6 Nov 2024 15:44:39 +0100 Subject: [PATCH] add Value.Upload component --- .../forms/Value/SummaryList/Examples.tsx | 21 ++++- .../uilib/extensions/forms/Value/Upload.mdx | 26 ++++++ .../forms/Value/Upload/Examples.tsx | 91 +++++++++++++++++++ .../extensions/forms/Value/Upload/demos.mdx | 31 +++++++ .../extensions/forms/Value/Upload/info.mdx | 62 +++++++++++++ .../forms/Value/Upload/properties.mdx | 15 +++ .../docs/uilib/extensions/forms/changelog.mdx | 4 + .../more-fields/Upload/info.mdx | 2 + .../extensions/forms/Value/Upload/Upload.tsx | 23 +++++ .../Value/Upload/__tests__/Upload.test.tsx | 75 +++++++++++++++ .../extensions/forms/Value/Upload/index.ts | 2 + .../src/extensions/forms/Value/index.ts | 1 + 12 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload.mdx create mode 100644 packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/Examples.tsx create mode 100644 packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/demos.mdx create mode 100644 packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/info.mdx create mode 100644 packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/properties.mdx create mode 100644 packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx create mode 100644 packages/dnb-eufemia/src/extensions/forms/Value/Upload/__tests__/Upload.test.tsx create mode 100644 packages/dnb-eufemia/src/extensions/forms/Value/Upload/index.ts diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/SummaryList/Examples.tsx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/SummaryList/Examples.tsx index 93f658babe3..4d7dbb7f5d2 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/SummaryList/Examples.tsx +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/SummaryList/Examples.tsx @@ -2,6 +2,16 @@ import ComponentBox from '../../../../../../shared/tags/ComponentBox' import { Card } from '@dnb/eufemia/src' import { Field, Form, Value } from '@dnb/eufemia/src/extensions/forms' +function createMockFile(name: string, size: number, type: string) { + const file = new File([], name, { type }) + Object.defineProperty(file, 'size', { + get() { + return size + }, + }) + return file +} + export const DefaultLayout = () => { return ( @@ -132,15 +142,24 @@ export function InheritVisibility() { export function InheritLabel() { return ( - + + + diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload.mdx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload.mdx new file mode 100644 index 00000000000..6333ac1a371 --- /dev/null +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload.mdx @@ -0,0 +1,26 @@ +--- +title: 'Upload' +description: '`Value.Upload` will render the selected country.' +componentType: 'feature-value' +showTabs: true +tabs: + - title: Info + key: '/info' + - title: Demos + key: '/demos' + - title: Properties + key: '/properties' +breadcrumb: + - text: Forms + href: /uilib/extensions/forms/ + - text: Value + href: /uilib/extensions/forms/Value/ + - text: Upload + href: /uilib/extensions/forms/Value/Upload/ +--- + +import Info from 'Docs/uilib/extensions/forms/Value/Upload/info' +import Demos from 'Docs/uilib/extensions/forms/Value/Upload/demos' + + + diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/Examples.tsx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/Examples.tsx new file mode 100644 index 00000000000..677d7992aa1 --- /dev/null +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/Examples.tsx @@ -0,0 +1,91 @@ +import ComponentBox from '../../../../../../shared/tags/ComponentBox' +import { P } from '@dnb/eufemia/src' +import { Form, Value } from '@dnb/eufemia/src/extensions/forms' + +function createMockFile(name: string, size: number, type: string) { + const file = new File([], name, { type }) + Object.defineProperty(file, 'size', { + get() { + return size + }, + }) + return file +} + +export const Placeholder = () => { + return ( + + + + ) +} + +export const WithValue = () => { + return ( + + + + ) +} + +export const Label = () => { + return ( + + + + ) +} + +export const LabelAndValue = () => { + return ( + + + + ) +} + +export const Inline = () => { + return ( + +

+ This is before the component + + This is after the component +

+
+ ) +} + +export const WithPath = () => { + return ( + + + + + + ) +} diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/demos.mdx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/demos.mdx new file mode 100644 index 00000000000..f828d1679ef --- /dev/null +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/demos.mdx @@ -0,0 +1,31 @@ +--- +showTabs: true +--- + +import * as Examples from './Examples' + +## Demos + +### Placeholder + + + +### Value + + + +### Label + + + +### Label and value + + + +### With path + + + +### Inline + + diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/info.mdx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/info.mdx new file mode 100644 index 00000000000..d58c2028121 --- /dev/null +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/info.mdx @@ -0,0 +1,62 @@ +--- +showTabs: true +--- + +## Description + +`Value.SelectCountry` will render the selected country. + +```jsx +import { Value } from '@dnb/eufemia/extensions/forms' +render() +``` + +There is a corresponding [Field.SelectCountry](/uilib/extensions/forms/feature-fields/SelectCountry) component. + +### The `useCountry` hook + +You can use the `Value.SelectCountry.useCountry` hook to get the country name by ISO code. It returns the country name in the current locale. + +```tsx +import { Value } from '@dnb/eufemia/extensions/forms' + +const MyComponent = () => { + const { getCountryNameByIso } = Value.SelectCountry.useCountry('NO') +} +``` + +### TransformIn and TransformOut + +You can use the `transformIn` and `transformOut` to transform the value before it gets displayed in the field and before it gets sent to the form. The second parameter is the country object. You may have a look at the demo below to see how it works. + +```tsx +const transformOut = (value, country) => { + if (value) { + return `${country.name} (${value})` + } +} +const transformIn = (value) => { + return String(value).match(/\((.*)\)/)?.[1] +} +``` + +### onFocus, onBlur, onChange + +These events have an additional parameter with the country object. + +```tsx +const onFocus = (value, country) => {} +``` + +## The country object + +```ts +{ + cdc: '47', + iso: 'NO', + name: 'Norge', + i18n: { en: 'Norway', nb: 'Norge' }, + regions: ['Scandinavia', 'Nordic'], + continent: 'Europe', +} +``` diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/properties.mdx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/properties.mdx new file mode 100644 index 00000000000..48112439f45 --- /dev/null +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Value/Upload/properties.mdx @@ -0,0 +1,15 @@ +--- +showTabs: true +--- + +import TranslationsTable from 'dnb-design-system-portal/src/shared/parts/TranslationsTable' +import PropertiesTable from 'dnb-design-system-portal/src/shared/parts/PropertiesTable' +import { ValueProperties } from '@dnb/eufemia/src/extensions/forms/Value/ValueDocs' + +## Properties + + + +## Translations + + diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/changelog.mdx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/changelog.mdx index fe03c63d274..85fbb373da0 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/changelog.mdx +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/changelog.mdx @@ -11,6 +11,10 @@ breadcrumb: # Change log +## 10.55 + +- Added [Value.Upload](/uilib/extensions/forms/Value/Upload/) component to render a file value. + Change log for the Eufemia Forms extension. ## v10.54 diff --git a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/feature-fields/more-fields/Upload/info.mdx b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/feature-fields/more-fields/Upload/info.mdx index c2603ec6acc..58eeb8a6b50 100644 --- a/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/feature-fields/more-fields/Upload/info.mdx +++ b/packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/feature-fields/more-fields/Upload/info.mdx @@ -11,6 +11,8 @@ import { Field } from '@dnb/eufemia/extensions/forms' render() ``` +There is a corresponding [Value.Upload](/uilib/extensions/forms/Value/Upload) component. + ## The data and file format The returned data is an array of objects containing a file object and a unique ID. The file object contains the file itself and some additional properties like an unique ID. diff --git a/packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx new file mode 100644 index 00000000000..3a2541cf4bb --- /dev/null +++ b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/Upload.tsx @@ -0,0 +1,23 @@ +import React from 'react' +import classnames from 'classnames' +import ValueBlock from '../../ValueBlock' +import { useValueProps } from '../../hooks' +import { ValueProps } from '../../types' + +export type Props = ValueProps + +function Upload(props: Props) { + const { value, className, ...rest } = useValueProps(props) + console.log(value) + return ( + + {'value'} + + ) +} + +Upload._supportsSpacingProps = true +export default Upload diff --git a/packages/dnb-eufemia/src/extensions/forms/Value/Upload/__tests__/Upload.test.tsx b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/__tests__/Upload.test.tsx new file mode 100644 index 00000000000..6b8cf409c1b --- /dev/null +++ b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/__tests__/Upload.test.tsx @@ -0,0 +1,75 @@ +import React from 'react' +import { screen, render } from '@testing-library/react' +import { Value, Form } from '../../..' + +describe('Value.SelectCountry', () => { + it('renders string values', () => { + render() + + expect( + document.querySelector( + '.dnb-forms-value-select-country .dnb-forms-value-block__content' + ) + ).toHaveTextContent('Norge') + }) + + it('renders label when showEmpty is true', () => { + render() + expect(document.querySelector('.dnb-form-label')).toHaveTextContent( + 'My label' + ) + }) + + it('renders value and label', () => { + render() + expect( + document.querySelector( + '.dnb-forms-value-select-country .dnb-forms-value-block__content' + ) + ).toHaveTextContent('Norge') + + expect(document.querySelector('.dnb-form-label')).toHaveTextContent( + 'My selections' + ) + }) + + it('renders custom label', () => { + render() + expect(document.querySelector('.dnb-form-label')).toHaveTextContent( + 'Custom label' + ) + }) + + it('renders placeholder', () => { + render() + expect(screen.getByText('Please select a value')).toBeInTheDocument() + }) + + it('renders value from path', () => { + render( + + + + ) + + expect( + document.querySelector( + '.dnb-forms-value-select-country .dnb-forms-value-block__content' + ) + ).toHaveTextContent('Sveits') + }) + + it('formats value in different locale', () => { + render( + + + + ) + + expect( + document.querySelector( + '.dnb-forms-value-select-country .dnb-forms-value-block__content' + ) + ).toHaveTextContent('Switzerland') + }) +}) diff --git a/packages/dnb-eufemia/src/extensions/forms/Value/Upload/index.ts b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/index.ts new file mode 100644 index 00000000000..c2369c59991 --- /dev/null +++ b/packages/dnb-eufemia/src/extensions/forms/Value/Upload/index.ts @@ -0,0 +1,2 @@ +export { default } from './Upload' +export * from './Upload' diff --git a/packages/dnb-eufemia/src/extensions/forms/Value/index.ts b/packages/dnb-eufemia/src/extensions/forms/Value/index.ts index 05e14cc75f4..bc35941251e 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Value/index.ts +++ b/packages/dnb-eufemia/src/extensions/forms/Value/index.ts @@ -16,3 +16,4 @@ export { default as OrganizationNumber } from './OrganizationNumber' export { default as SelectCountry } from './SelectCountry' export { default as ArraySelection } from './ArraySelection' export { default as Selection } from './Selection' +export { default as Upload } from './Upload'