generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ObjectBase): add inline variant (#104)
- Loading branch information
Showing
10 changed files
with
214 additions
and
45 deletions.
There are no files selected for viewing
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,19 @@ | ||
@import '../../../styles/variables.scss'; | ||
|
||
.#{$ns}object-base { | ||
&__content { | ||
&_inline { | ||
display: flex; | ||
|
||
& > .#{$ns}use-search { | ||
width: 150px; | ||
margin-bottom: 0; | ||
margin-right: 8px; | ||
|
||
&:last-child { | ||
margin-right: 0; | ||
} | ||
} | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/lib/kit/components/Views/ObjectBaseView/ObjectBaseView.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,19 @@ | ||
@import '../../../styles/variables.scss'; | ||
|
||
.#{$ns}object-base-view { | ||
&__content { | ||
&_inline { | ||
display: flex; | ||
|
||
& > div { | ||
flex: auto; | ||
margin-right: 8px; | ||
margin-bottom: 0; | ||
|
||
&:last-child { | ||
margin-right: 0; | ||
} | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/lib/kit/components/Views/ObjectBaseView/ObjectBaseView.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,58 @@ | ||
import React from 'react'; | ||
|
||
import _ from 'lodash'; | ||
|
||
import {ObjectIndependentView, ObjectIndependentViewProps, ViewController} from '../../../../core'; | ||
import {block, filterPropertiesForObjectInline} from '../../../utils'; | ||
|
||
import './ObjectBaseView.scss'; | ||
|
||
const b = block('object-base-view'); | ||
|
||
export interface ObjectBaseViewProps extends ObjectIndependentViewProps { | ||
inline?: boolean; | ||
} | ||
|
||
export const ObjectBaseView: React.FC<ObjectBaseViewProps> = ({ | ||
inline, | ||
spec, | ||
name, | ||
Layout, | ||
...restProps | ||
}) => { | ||
if (!spec.properties || !_.isObjectLike(spec.properties)) { | ||
return null; | ||
} | ||
|
||
const specProperties = inline | ||
? filterPropertiesForObjectInline(spec.properties) | ||
: spec.properties; | ||
|
||
const content = ( | ||
<div className={b('content', {inline})}> | ||
{(spec.viewSpec.order || Object.keys(specProperties)).map((property: string) => | ||
specProperties[property] ? ( | ||
<ViewController | ||
spec={specProperties[property]} | ||
name={`${name ? name + '.' : ''}${property}`} | ||
key={`${name ? name + '.' : ''}${property}`} | ||
/> | ||
) : null, | ||
)} | ||
</div> | ||
); | ||
|
||
if (!Layout) { | ||
return content; | ||
} | ||
|
||
return ( | ||
<Layout spec={spec} name={name} {...restProps}> | ||
{content} | ||
</Layout> | ||
); | ||
}; | ||
|
||
export const ObjectInlineView: ObjectIndependentView = (props) => { | ||
return <ObjectBaseView {...props} inline />; | ||
}; |
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 @@ | ||
export * from './ObjectBaseView'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './cn'; | ||
export * from './common'; | ||
export * from './bigIntMath'; | ||
export * from './objectInline'; |
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,9 @@ | ||
import {Spec, isNumberSpec, isStringSpec} from '../../core'; | ||
|
||
export const filterPropertiesForObjectInline = (properties: Record<string, Spec>) => { | ||
return Object.fromEntries( | ||
Object.entries(properties).filter( | ||
([, propSpec]) => isStringSpec(propSpec) || isNumberSpec(propSpec), | ||
), | ||
); | ||
}; |
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 @@ | ||
import React from 'react'; | ||
|
||
import {StoryFn} from '@storybook/react'; | ||
|
||
import {ObjectInline as ObjectInlineBase, ObjectSpec, SpecTypes} from '../lib'; | ||
|
||
import {InputPreview} from './components'; | ||
|
||
export default { | ||
title: 'Object/Inline', | ||
component: ObjectInlineBase, | ||
}; | ||
|
||
const baseSpec: ObjectSpec = { | ||
type: SpecTypes.Object, | ||
properties: { | ||
type: { | ||
type: SpecTypes.String, | ||
enum: ['first', 'second', 'third'], | ||
viewSpec: { | ||
type: 'select', | ||
placeholder: 'Choose type', | ||
layout: 'transparent', | ||
layoutTitle: 'Type', | ||
}, | ||
}, | ||
name: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'base', | ||
placeholder: 'Type your name', | ||
layout: 'transparent', | ||
layoutTitle: 'Name', | ||
}, | ||
}, | ||
}, | ||
viewSpec: { | ||
type: 'inline', | ||
layout: 'row', | ||
layoutTitle: 'Candidate', | ||
}, | ||
}; | ||
|
||
const value = {type: 'first', name: 'Foo'}; | ||
|
||
const excludeOptions = [ | ||
'description', | ||
'viewSpec.type', | ||
'viewSpec.oneOfParams', | ||
'viewSpec.placeholder', | ||
]; | ||
|
||
const template = (spec: ObjectSpec = baseSpec) => { | ||
const Template: StoryFn<typeof ObjectInlineBase> = (__, {viewMode}) => ( | ||
<InputPreview | ||
spec={spec} | ||
value={value} | ||
excludeOptions={excludeOptions} | ||
viewMode={viewMode} | ||
/> | ||
); | ||
|
||
return Template; | ||
}; | ||
|
||
export const Inline = template(); |