Skip to content

Commit

Permalink
feat(Form.Visibility): add pathValue and whenValue
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Jan 10, 2024
1 parent d3b766f commit 2ac037b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export const BooleanExample = () => {
<Form.Handler>
<Flex.Stack>
<Field.Boolean
label="Show content"
variant="buttons"
path="/toggleValue"
label="Show content"
value={false}
/>
<Form.Visibility pathTrue="/toggleValue">
Expand All @@ -28,6 +28,26 @@ export const BooleanExample = () => {
)
}

export const PathValue = () => {
return (
<ComponentBox>
<Form.Handler>
<Field.Toggle
label="Show content"
valueOn="checked"
valueOff="unchecked"
variant="buttons"
path="/toggleValue"
value="unchecked"
/>
<Form.Visibility pathValue="/toggleValue" whenValue="checked">
<P>This is visible</P>
</Form.Visibility>
</Form.Handler>
</ComponentBox>
)
}

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

<Examples.BooleanExample />

### Matching value

<Examples.PathValue />

### Direct properties

<Examples.BasedOnBooleanTrue />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ showTabs: true
| `pathFalsy` | `string` | _(optional)_ Given data context path must be falsy to show children. |
| `pathTrue` | `string` | _(optional)_ Given data context path must be true to show children. |
| `pathFalse` | `string` | _(optional)_ Given data context path must be false to show children. |
| `pathValue` | `string` | _(optional)_ Given data context path must match, as well as the `whenValue` value. |
| `whenValue` | `string` | _(optional)_ The value to match. Should be used together with `pathValue`. |
| `inferData` | `function` | _(optional)_ Will be called to decide by external logic, and show/hide contents based on the return value. |
| `children` | `React.Node` | _(required)_ Contents. |
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export type Props = {
pathTrue?: string
/** Given data context path must be false to show children */
pathFalse?: string
/** Given data context path must match, as well as the "whenValue" value */
pathValue?: string
whenValue?: unknown
/** Infer visibility calling given derivative function with the whole data set. Should return true/false for visibility. */
inferData?: (data: unknown) => boolean
children: React.ReactNode
Expand All @@ -29,11 +32,20 @@ function Visibility({
pathFalsy,
pathTrue,
pathFalse,
pathValue,
whenValue,
inferData,
children,
}: Props) {
const dataContext = useContext(DataContext.Context)

console.log(
'val',
pointer.has(dataContext.data, pathValue) &&
pointer.get(dataContext.data, pathValue),
whenValue
)

if (visible === false) {
return null
}
Expand Down Expand Up @@ -75,6 +87,16 @@ function Visibility({
return null
}

if (
pathValue &&
!(
pointer.has(dataContext.data, pathValue) &&
pointer.get(dataContext.data, pathValue) === whenValue
)
) {
return null
}

if (inferData && !inferData(dataContext.data)) {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Visibility', () => {
expect(Visibility._supportsSpacingProps).toBe('children')
})

describe('visibility-prop', () => {
describe('visibility', () => {
it('renders children when visible is true', () => {
render(<Visibility visible={true}>Child</Visibility>)
expect(screen.getByText('Child')).toBeInTheDocument()
Expand All @@ -25,7 +25,7 @@ describe('Visibility', () => {
})
})

describe('pathDefined-prop', () => {
describe('pathDefined', () => {
it('renders children when target path is defined', () => {
render(
<Provider data={{ isDefined: 'foo' }}>
Expand All @@ -45,7 +45,7 @@ describe('Visibility', () => {
})
})

describe('pathUndefined-prop', () => {
describe('pathUndefined', () => {
it('renders children when target path is defined', () => {
render(
<Provider data={{ isDefined: 'foo' }}>
Expand All @@ -65,7 +65,7 @@ describe('Visibility', () => {
})
})

describe('pathTruthy-prop', () => {
describe('pathTruthy', () => {
it('renders children when target path is truthy', () => {
render(
<Provider data={{ isTrue: true }}>
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('Visibility', () => {
})
})

describe('pathFalsy-prop', () => {
describe('pathFalsy', () => {
it('renders children when target path is falsy', () => {
render(
<Provider data={{ isFalse: false }}>
Expand Down Expand Up @@ -123,8 +123,8 @@ describe('Visibility', () => {
})
})

describe('inferData-prop', () => {
it('renders children when infer-function return true', () => {
describe('inferData', () => {
it('renders children when infer-function returns true', () => {
// eslint-disable-next-line no-unused-vars
const inferData = jest.fn((data) => true)
render(
Expand All @@ -148,4 +148,28 @@ describe('Visibility', () => {
expect(inferData.mock.calls[0][0]).toEqual({ foo: 'bar' })
})
})

describe('pathValue', () => {
it('renders children when target path and value matches', () => {
render(
<Provider data={{ myPath: 'checked' }}>
<Visibility pathValue="/myPath" whenValue="checked">
Child
</Visibility>
</Provider>
)
expect(screen.getByText('Child')).toBeInTheDocument()
})

it('does not render children when target path not not value matches', () => {
render(
<Provider data={{ myPath: 'checked' }}>
<Visibility pathValue="/myPath" whenValue="not-checked">
Child
</Visibility>
</Provider>
)
expect(screen.queryByText('Child')).toBeNull()
})
})
})

0 comments on commit 2ac037b

Please sign in to comment.