feat(core-blocks): CB-872 add default value support for input

This commit is contained in:
Naumov Alexey
2021-04-21 14:57:48 +03:00
parent eb1f52e8e6
commit adec630bd2
10 changed files with 14 additions and 11 deletions
@@ -43,6 +43,7 @@ export interface ICheckboxObjectProps<TKey extends string> extends CheckboxInput
autoHide?: boolean;
name: TKey;
}
export interface CheckboxType {
(props: CheckboxBaseProps & ICheckboxControlledProps): React.ReactElement<any, any> | null;
<TKey extends string>(props: CheckboxBaseProps & ICheckboxObjectProps<TKey>): React.ReactElement<any, any> | null;
@@ -74,7 +75,7 @@ export const Checkbox: CheckboxType = observer(function Checkbox({
onChange,
});
if (autoHide && !isControlPresented(name, state, defaultChecked)) {
if (autoHide && !isControlPresented(name, state)) {
return null;
}
@@ -27,7 +27,7 @@ export const FieldCheckbox: CheckboxType = function FieldCheckbox({
}: CheckboxBaseProps & (ICheckboxControlledProps | ICheckboxObjectProps<any>)) {
const styles = useStyles(baseFormControlStyles, fieldCheckboxStyles);
if (rest.autoHide && !isControlPresented(rest.name, rest.state, rest.defaultChecked)) {
if (rest.autoHide && !isControlPresented(rest.name, rest.state)) {
return null;
}
@@ -39,7 +39,7 @@ export const FieldCheckboxNew: CheckboxType = function FieldCheckboxNew({
}: CheckboxBaseProps & (ICheckboxControlledProps | ICheckboxObjectProps<any>)) {
const styles = useStyles(baseFormControlStylesNew, fieldCheckboxStyles);
if (rest.autoHide && !isControlPresented(rest.name, rest.state, rest.defaultChecked)) {
if (rest.autoHide && !isControlPresented(rest.name, rest.state)) {
return null;
}
@@ -121,7 +121,7 @@ export const Switch: SwitchType = observer(function Switch({
checkboxState.checked && switchState.checked
);
if (autoHide && !isControlPresented(name, state, defaultChecked)) {
if (autoHide && !isControlPresented(name, state)) {
return null;
}
@@ -125,7 +125,7 @@ export const SwitchNew: SwitchType = observer(function SwitchNew({
checkboxState.checked && switchState.checked
);
if (autoHide && !isControlPresented(name, state, defaultChecked)) {
if (autoHide && !isControlPresented(name, state)) {
return null;
}
@@ -16,12 +16,11 @@ import { baseFormControlStyles } from './baseFormControlStyles';
import { FormContext } from './FormContext';
import { isControlPresented } from './isControlPresented';
type BaseProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'name' | 'value' | 'defaultValue'> & {
type BaseProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'name' | 'value'> & {
description?: string;
mod?: 'surface';
long?: boolean;
short?: boolean;
defaultValue?: string;
};
type ControlledProps = BaseProps & {
@@ -28,12 +28,11 @@ const INPUT_FIELD_STYLES = css`
}
`;
type BaseProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'name' | 'value' | 'defaultValue'> & ILayoutSizeProps & {
type BaseProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'name' | 'value'> & ILayoutSizeProps & {
description?: string;
mod?: 'surface';
ref?: React.Ref<HTMLInputElement>;
style?: ComponentStyle;
defaultValue?: string | number;
};
type ControlledProps = BaseProps & {
@@ -7,7 +7,7 @@
*/
export function isControlPresented(
name: string | undefined, state: any, defaultValue?: string | number | boolean
name: string | undefined, state: any, defaultValue?: string | number | readonly string[]
): boolean {
if (state !== undefined && name !== undefined) {
if (name in state) {
@@ -17,7 +17,7 @@ import { RenderField } from './RenderField';
interface ObjectPropertyFormProps extends ILayoutSizeProps {
properties: ObjectPropertyInfo[];
state?: Record<string, string | number>;
state?: Record<string, any>;
category?: string | null;
editable?: boolean;
autofillToken?: string;
@@ -111,6 +111,7 @@ export const RenderField: React.FC<RenderFieldProps> = observer(function RenderF
id={property.id}
name={property.id!}
state={state}
defaultChecked={defaultValue}
title={property.description}
disabled={disabled || readOnly}
className={className}
@@ -124,6 +125,7 @@ export const RenderField: React.FC<RenderFieldProps> = observer(function RenderF
id={property.id}
name={property.id!}
checked={value}
defaultChecked={defaultValue}
title={property.description}
disabled={disabled || readOnly}
className={className}
@@ -175,6 +177,7 @@ export const RenderField: React.FC<RenderFieldProps> = observer(function RenderF
title={property.description}
name={property.id!}
state={state}
defaultValue={defaultValue}
description={description}
disabled={disabled}
readOnly={readOnly}
@@ -194,6 +197,7 @@ export const RenderField: React.FC<RenderFieldProps> = observer(function RenderF
title={property.description}
name={property.id!}
value={value}
defaultValue={defaultValue}
description={description}
disabled={disabled}
readOnly={readOnly}