From 5601aad96989cc93ec2cb67c87b887e780296be9 Mon Sep 17 00:00:00 2001 From: Michael Marszalek Date: Fri, 8 Nov 2024 11:28:11 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20`Textfield`=20?= =?UTF-8?q?to=20use=20`Field`=20(#2710)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resolves #2713 - Adds `DefaultProps` to `Field` - Adds `counter` which takes a `number` or `FieldCounterProps` - Kept `error` as we can look at this vs changes in `ValidationMessage` later - Will look closer at affix in #2748 --------- Co-authored-by: barsnes --- .changeset/eleven-experts-raise.md | 5 + .changeset/loud-bobcats-look.md | 5 + .changeset/olive-waves-build.md | 5 + .changeset/popular-jeans-happen.md | 5 + .changeset/real-zoos-fail.md | 5 + .changeset/witty-moose-scream.md | 5 + packages/css/field.css | 6 +- .../ErrorSummary/ErrorSummary.stories.tsx | 1 + .../react/src/components/form/Field/Field.mdx | 2 + .../react/src/components/form/Field/Field.tsx | 3 +- .../react/src/components/form/Input/Input.tsx | 3 +- .../src/components/form/Textarea/Textarea.mdx | 3 - .../form/Textarea/Textarea.stories.tsx | 15 - .../components/form/Textfield/Textfield.mdx | 36 ++- .../form/Textfield/Textfield.stories.tsx | 57 +++- .../form/Textfield/Textfield.test.tsx | 60 +--- .../components/form/Textfield/Textfield.tsx | 291 ++++++------------ .../components/form/Textfield/useTextfield.ts | 49 --- packages/react/stories/showcase.module.css | 7 - packages/react/stories/showcase.stories.tsx | 1 - packages/react/stories/testing.stories.tsx | 3 +- 21 files changed, 213 insertions(+), 354 deletions(-) create mode 100644 .changeset/eleven-experts-raise.md create mode 100644 .changeset/loud-bobcats-look.md create mode 100644 .changeset/olive-waves-build.md create mode 100644 .changeset/popular-jeans-happen.md create mode 100644 .changeset/real-zoos-fail.md create mode 100644 .changeset/witty-moose-scream.md delete mode 100644 packages/react/src/components/form/Textfield/useTextfield.ts diff --git a/.changeset/eleven-experts-raise.md b/.changeset/eleven-experts-raise.md new file mode 100644 index 0000000000..e5fd9700da --- /dev/null +++ b/.changeset/eleven-experts-raise.md @@ -0,0 +1,5 @@ +--- +"@digdir/designsystemet-react": patch +--- + +Textfield: Removed `htmlSize`, you can now use native `size` diff --git a/.changeset/loud-bobcats-look.md b/.changeset/loud-bobcats-look.md new file mode 100644 index 0000000000..889d9ab212 --- /dev/null +++ b/.changeset/loud-bobcats-look.md @@ -0,0 +1,5 @@ +--- +"@digdir/designsystemet-react": patch +--- + +Textfield: Refactored `characterLimit` to `counter` and now use new `Field.Counter` sub-component diff --git a/.changeset/olive-waves-build.md b/.changeset/olive-waves-build.md new file mode 100644 index 0000000000..189df40863 --- /dev/null +++ b/.changeset/olive-waves-build.md @@ -0,0 +1,5 @@ +--- +"@digdir/designsystemet-react": patch +--- + +Textfield: Now works as expected with `data-size` diff --git a/.changeset/popular-jeans-happen.md b/.changeset/popular-jeans-happen.md new file mode 100644 index 0000000000..c591e323ee --- /dev/null +++ b/.changeset/popular-jeans-happen.md @@ -0,0 +1,5 @@ +--- +"@digdir/designsystemet-react": patch +--- + +Textfield: Added `multiline` for switching between `input` and `textarea` diff --git a/.changeset/real-zoos-fail.md b/.changeset/real-zoos-fail.md new file mode 100644 index 0000000000..d4533224ce --- /dev/null +++ b/.changeset/real-zoos-fail.md @@ -0,0 +1,5 @@ +--- +"@digdir/designsystemet-react": patch +--- + +Textfield: Removed `hideLabel`, use `aria-label` or `aria-describedby` for "hidden" labels diff --git a/.changeset/witty-moose-scream.md b/.changeset/witty-moose-scream.md new file mode 100644 index 0000000000..5dff1a2114 --- /dev/null +++ b/.changeset/witty-moose-scream.md @@ -0,0 +1,5 @@ +--- +"@digdir/designsystemet-react": patch +--- + +Textfield: Update to use `Field` internally diff --git a/packages/css/field.css b/packages/css/field.css index 0fbaf4adef..351bb66957 100644 --- a/packages/css/field.css +++ b/packages/css/field.css @@ -73,7 +73,11 @@ display: inline-flex; /* Using inline-flex to match native inline-block behaviour of */ position: relative; white-space: nowrap; - width: fit-content; + align-self: stretch; /* If ds-field is placed inside a flex container we need to fill width */ + + &:has(input[size]) { + width: fit-content; + } & :not(.ds-input) { padding-inline: var(--dsc-field-affix-padding-inline); diff --git a/packages/react/src/components/ErrorSummary/ErrorSummary.stories.tsx b/packages/react/src/components/ErrorSummary/ErrorSummary.stories.tsx index 47538fc328..7e076bdc8a 100644 --- a/packages/react/src/components/ErrorSummary/ErrorSummary.stories.tsx +++ b/packages/react/src/components/ErrorSummary/ErrorSummary.stories.tsx @@ -45,6 +45,7 @@ export const WithForm: Story = () => ( diff --git a/packages/react/src/components/form/Field/Field.mdx b/packages/react/src/components/form/Field/Field.mdx index 777ac8547c..54de73ae5a 100644 --- a/packages/react/src/components/form/Field/Field.mdx +++ b/packages/react/src/components/form/Field/Field.mdx @@ -18,4 +18,6 @@ Det er viktig at samme informasjon som vises i prefixet eller suffixet også er ## Antall tegn +Bruk `Field.Counter` for å vise antall tegn som er skrevet i et tekstfelt. + diff --git a/packages/react/src/components/form/Field/Field.tsx b/packages/react/src/components/form/Field/Field.tsx index f049723132..3b2ab68ff0 100644 --- a/packages/react/src/components/form/Field/Field.tsx +++ b/packages/react/src/components/form/Field/Field.tsx @@ -2,9 +2,10 @@ import { useMergeRefs } from '@floating-ui/react'; import cl from 'clsx/lite'; import type { HTMLAttributes } from 'react'; import { forwardRef, useEffect, useRef } from 'react'; +import type { DefaultProps } from '../../../types'; import { fieldObserver } from './fieldObserver'; -export type FieldProps = HTMLAttributes; +export type FieldProps = HTMLAttributes & DefaultProps; export const Field = forwardRef(function Field( { className, ...rest }, ref, diff --git a/packages/react/src/components/form/Input/Input.tsx b/packages/react/src/components/form/Input/Input.tsx index 541dd531d5..b7140ab6ec 100644 --- a/packages/react/src/components/form/Input/Input.tsx +++ b/packages/react/src/components/form/Input/Input.tsx @@ -4,10 +4,11 @@ import { forwardRef } from 'react'; import type { DefaultProps } from '../../../types'; type InputAttr = InputHTMLAttributes; + export type InputProps = { /** Supported `input` types */ type?: InputAttr['type']; - /** Defines the width of in count of characters. + /** Defines the width of `Input` in count of characters. */ size?: number; /** Disables element diff --git a/packages/react/src/components/form/Textarea/Textarea.mdx b/packages/react/src/components/form/Textarea/Textarea.mdx index 694fb07217..f8997b6b43 100644 --- a/packages/react/src/components/form/Textarea/Textarea.mdx +++ b/packages/react/src/components/form/Textarea/Textarea.mdx @@ -9,9 +9,6 @@ import * as TextareaStories from './Textarea.stories'; -## Antall tegn - -{/* */} ## Kontrollert diff --git a/packages/react/src/components/form/Textarea/Textarea.stories.tsx b/packages/react/src/components/form/Textarea/Textarea.stories.tsx index 032a025b27..a3c15b95ec 100644 --- a/packages/react/src/components/form/Textarea/Textarea.stories.tsx +++ b/packages/react/src/components/form/Textarea/Textarea.stories.tsx @@ -45,21 +45,6 @@ export const Preview: Story = { ), }; -// export const WithCharacterCounter: Story = { -// args: { -// cols: 40, -// characterLimit: { -// maxCount: 5, -// }, -// }, -// render: (args) => ( -// <> -// -//