feat:增加一种 flex-item 类型方便在 flex 里使用 (#1745)

This commit is contained in:
吴多益 2021-04-04 21:42:41 +08:00 committed by GitHub
parent c82e451bf9
commit 7b00b10c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 28 deletions

View File

@ -91,6 +91,7 @@ export type SchemaType =
| 'drawer'
| 'each'
| 'flex'
| 'flex-item'
| 'grid'
| 'grid-2d'
| 'hbox'

View File

@ -5,33 +5,7 @@
import React from 'react';
import {Renderer, RendererProps} from '../factory';
import {Schema} from '../types';
import {BaseSchema, SchemaObject} from '../Schema';
export type FlexItemProps = {
/**
*
*/
width?: number;
/**
*
*/
height?: number;
/**
* Flex
*/
align: 'auto' | 'flex-start' | 'flex-end' | 'center' | 'baseline' | 'stretch';
/**
*
*/
style?: {
[propName: string]: any;
};
};
export type FlexItem = FlexItemProps & SchemaObject;
import {BaseSchema, SchemaCollection, SchemaObject} from '../Schema';
/**
* Flex
@ -89,7 +63,7 @@ export interface FlexSchema extends BaseSchema {
/**
* flex
*/
items: Array<FlexItem>;
items: Array<SchemaObject>;
/**
*
@ -144,8 +118,65 @@ export default class Flex extends React.Component<FlexProps, object> {
}
}
export interface FlexItemSchema extends BaseSchema {
/**
* wrapper flex
*/
type: 'flex-item';
/**
*
*/
body: SchemaCollection;
/**
*
*/
style?: {
[propName: string]: any;
};
}
export interface FlexItemProps
extends RendererProps,
Omit<FlexItemSchema, 'className'> {
children?: JSX.Element | ((props?: any) => JSX.Element);
}
export class FlexItem extends React.Component<FlexItemProps, object> {
static propsList: Array<string> = ['body', 'className', 'children'];
renderBody(): JSX.Element | null {
const {children, body, render} = this.props;
return children
? typeof children === 'function'
? (children(this.props) as JSX.Element)
: (children as JSX.Element)
: body
? (render('body', body) as JSX.Element)
: null;
}
render() {
const {className, size, classnames: cx, style} = this.props;
return (
<div className={className} style={style}>
{this.renderBody()}
</div>
);
}
}
@Renderer({
test: /(^|\/)flex$/,
name: 'flex'
})
export class FlexRenderer extends Flex {}
@Renderer({
test: /(^|\/)flex-item$/,
name: 'flex-item'
})
export class FlexItemRenderer extends FlexItem {}