Skip to content

Commit

Permalink
fix(forms): Support undefined in toggle and button fields (#2775)
Browse files Browse the repository at this point in the history
Before, passing no value defaulted to the `valueOff` option in
`Field.Toggle`, leading to the same in `Field.Button` (false option
being chosen). Now, no buttons is selected when value is undefined.

<img width="939" alt="Screenshot 2023-10-20 at 14 38 13"
src="https://github.com/dnbexperience/eufemia/assets/2526740/db4cd31f-6fd0-4078-8986-7961eda988eb">
  • Loading branch information
henit authored Oct 23, 2023
1 parent 8a6b766 commit a22fad5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ export const ButtonsFalse = () => {
)
}

export const ButtonsUndefined = () => {
return (
<ComponentBox>
<Field.Boolean
variant="buttons"
label="Label text"
onChange={(value) => console.log('onChange', value)}
/>
</ComponentBox>
)
}

export const ButtonsRequired = () => {
return (
<ComponentBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ import * as Examples from './Examples'

<Examples.ButtonsFalse />

#### Button - Value undefined (no option selected)

<Examples.ButtonsUndefined />

#### Buttons - Required

<Examples.ButtonsRequired />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function Toggle(props: Props) {
}

const isOn = value === valueOn
const isOff = value === valueOff

switch (variant) {
default:
Expand Down Expand Up @@ -134,7 +135,7 @@ function Toggle(props: Props) {
id={id}
text={textOff ?? sharedContext?.translation.Forms.booleanNo}
on_click={setOff}
variant={isOn ? 'secondary' : undefined}
variant={isOff ? undefined : 'secondary'}
disabled={disabled}
status={error ? 'error' : undefined}
/>
Expand Down

0 comments on commit a22fad5

Please sign in to comment.