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(ObjectValue): added new input object value (#28)
- Loading branch information
1 parent
59afaae
commit 67214a1
Showing
9 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/lib/kit/components/Inputs/ObjectValueInput/ObjectValueInput.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,45 @@ | ||
import React from 'react'; | ||
|
||
import _ from 'lodash'; | ||
|
||
import {Controller, FieldValue, ObjectIndependentInput, ValidateError} from '../../../../core'; | ||
|
||
const OBJECT_VALUE_PROPERTY_NAME = 'value'; | ||
|
||
export const ObjectValueInput: ObjectIndependentInput = ({spec, input, name}) => { | ||
const parentOnChange = React.useCallback( | ||
(childName: string, childValue: FieldValue, childErrors?: Record<string, ValidateError>) => | ||
input.onChange( | ||
(currentValue) => | ||
_.set( | ||
{...(currentValue || {})}, | ||
childName.split(`${name}.`).join(''), | ||
childValue, | ||
), | ||
childErrors, | ||
), | ||
[input.onChange, input.name], | ||
); | ||
|
||
const parentOnUnmount = React.useCallback( | ||
(childName: string) => input.onChange((currentValue) => currentValue, {[childName]: false}), | ||
[input.onChange], | ||
); | ||
|
||
const specProperties = {...spec.properties}; | ||
|
||
if (!specProperties[OBJECT_VALUE_PROPERTY_NAME]) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Controller | ||
initialValue={input.value?.[OBJECT_VALUE_PROPERTY_NAME]} | ||
spec={specProperties[OBJECT_VALUE_PROPERTY_NAME]} | ||
name={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`} | ||
key={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`} | ||
parentOnChange={parentOnChange} | ||
parentOnUnmount={parentOnUnmount} | ||
/> | ||
); | ||
}; |
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 './ObjectValueInput'; |
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
20 changes: 20 additions & 0 deletions
20
src/lib/kit/components/Views/ObjectValueInputView/ObjectValueInputView.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,20 @@ | ||
import React from 'react'; | ||
|
||
import {ObjectIndependentView, ViewController} from '../../../../core'; | ||
|
||
const OBJECT_VALUE_PROPERTY_NAME = 'value'; | ||
|
||
export const ObjectValueInputView: ObjectIndependentView = ({spec, name}) => { | ||
const specProperties = {...spec.properties}; | ||
|
||
if (!specProperties[OBJECT_VALUE_PROPERTY_NAME]) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ViewController | ||
spec={specProperties[OBJECT_VALUE_PROPERTY_NAME]} | ||
name={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`} | ||
/> | ||
); | ||
}; |
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 './ObjectValueInputView'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
|
||
import {ComponentStory} from '@storybook/react'; | ||
|
||
import {ObjectSpec, ObjectValueInput as ObjectValueInputBase, SpecTypes} from '../lib'; | ||
|
||
import {InputPreview} from './components'; | ||
|
||
export default { | ||
title: 'Object/ObjectValue', | ||
component: ObjectValueInputBase, | ||
}; | ||
|
||
const baseSpec: ObjectSpec = { | ||
type: SpecTypes.Object, | ||
properties: { | ||
value: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'base', | ||
layout: 'row', | ||
layoutTitle: 'Value', | ||
}, | ||
}, | ||
}, | ||
viewSpec: { | ||
type: 'object_value', | ||
}, | ||
}; | ||
|
||
const excludeOptions = ['description', 'validator', 'viewSpec', 'required']; | ||
|
||
const template = (spec: ObjectSpec = baseSpec) => { | ||
const Template: ComponentStory<typeof ObjectValueInputBase> = (__, {viewMode}) => ( | ||
<InputPreview spec={spec} excludeOptions={excludeOptions} viewMode={viewMode} /> | ||
); | ||
|
||
return Template; | ||
}; | ||
|
||
export const ObjectValue = template(); |