-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataForm - Add combined fields support (#65399)
Co-authored-by: louwie17 <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: gigitux <[email protected]> Co-authored-by: oandregal <[email protected]> Co-authored-by: jameskoster <[email protected]>
- Loading branch information
1 parent
2b6b567
commit e0a8d29
Showing
10 changed files
with
252 additions
and
25 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
packages/dataviews/src/components/dataform-combined-edit/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,66 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalHStack as HStack, | ||
__experimentalVStack as VStack, | ||
__experimentalHeading as Heading, | ||
__experimentalSpacer as Spacer, | ||
} from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { DataFormCombinedEditProps, NormalizedField } from '../../types'; | ||
|
||
function Header( { title }: { title: string } ) { | ||
return ( | ||
<VStack className="dataforms-layouts__dropdown-header" spacing={ 4 }> | ||
<HStack alignment="center"> | ||
<Heading level={ 2 } size={ 13 }> | ||
{ title } | ||
</Heading> | ||
<Spacer /> | ||
</HStack> | ||
</VStack> | ||
); | ||
} | ||
|
||
function DataFormCombinedEdit< Item >( { | ||
field, | ||
data, | ||
onChange, | ||
hideLabelFromVision, | ||
}: DataFormCombinedEditProps< Item > ) { | ||
const className = 'dataforms-combined-edit'; | ||
const visibleChildren = ( field.children ?? [] ) | ||
.map( ( fieldId ) => field.fields.find( ( { id } ) => id === fieldId ) ) | ||
.filter( | ||
( childField ): childField is NormalizedField< Item > => | ||
!! childField | ||
); | ||
const children = visibleChildren.map( ( child ) => { | ||
return ( | ||
<div className="dataforms-combined-edit__field" key={ child.id }> | ||
<child.Edit | ||
data={ data } | ||
field={ child } | ||
onChange={ onChange } | ||
/> | ||
</div> | ||
); | ||
} ); | ||
|
||
const Stack = field.direction === 'horizontal' ? HStack : VStack; | ||
|
||
return ( | ||
<> | ||
{ ! hideLabelFromVision && <Header title={ field.label } /> } | ||
<Stack spacing={ 4 } className={ className } as="fieldset"> | ||
{ children } | ||
</Stack> | ||
</> | ||
); | ||
} | ||
|
||
export default DataFormCombinedEdit; |
12 changes: 12 additions & 0 deletions
12
packages/dataviews/src/components/dataform-combined-edit/style.scss
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,12 @@ | ||
.dataforms-layouts-panel__field-dropdown { | ||
.dataforms-combined-edit { | ||
border: none; | ||
padding: 0; | ||
} | ||
} | ||
|
||
.dataforms-combined-edit { | ||
&__field { | ||
flex: 1 1 auto; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
packages/dataviews/src/dataforms-layouts/get-visible-fields.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,29 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizeCombinedFields } from '../normalize-fields'; | ||
import type { | ||
Field, | ||
CombinedFormField, | ||
NormalizedCombinedFormField, | ||
} from '../types'; | ||
|
||
export function getVisibleFields< Item >( | ||
fields: Field< Item >[], | ||
formFields: string[] = [], | ||
combinedFields?: CombinedFormField< Item >[] | ||
): Field< Item >[] { | ||
const visibleFields: Array< | ||
Field< Item > | NormalizedCombinedFormField< Item > | ||
> = [ ...fields ]; | ||
if ( combinedFields ) { | ||
visibleFields.push( | ||
...normalizeCombinedFields( combinedFields, fields ) | ||
); | ||
} | ||
return formFields | ||
.map( ( fieldId ) => | ||
visibleFields.find( ( { id } ) => id === fieldId ) | ||
) | ||
.filter( ( field ): field is Field< Item > => !! field ); | ||
} |
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
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
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