Skip to content

Commit

Permalink
feat: component 支持外部定义的 props 类型传入 (#1685)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Dec 15, 2022
1 parent e1a84a5 commit 7a90906
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 42 deletions.
14 changes: 7 additions & 7 deletions packages/f2/src/chart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ export interface ComponentPosition {
}

// 统计图表
class Chart<TRecord extends DataRecord = DataRecord> extends Component<
ChartProps<TRecord>,
ChartState
> {
class Chart<
TRecord extends DataRecord = DataRecord,
IProps extends ChartProps<TRecord> = ChartProps<TRecord>
> extends Component<IProps, ChartState> {
// 坐标系
private componentsPosition: ComponentPosition[] = [];

// controller
public coord: CoordController;
public scale: ScaleController;

constructor(props: ChartProps<TRecord>, context: IContext) {
constructor(props: IProps, context: IContext) {
super(props);

const { theme, px2hd } = context;
Expand All @@ -77,7 +77,7 @@ class Chart<TRecord extends DataRecord = DataRecord> extends Component<
};
}

private getStyle(props: ChartProps<TRecord>) {
private getStyle(props: IProps) {
const { context, layout } = this;
const { theme, px2hd } = context;
const { left, top, width, height } = layout;
Expand Down Expand Up @@ -107,7 +107,7 @@ class Chart<TRecord extends DataRecord = DataRecord> extends Component<
}

// props 更新
willReceiveProps(nextProps: ChartProps<TRecord>) {
willReceiveProps(nextProps: IProps) {
const { scale, coord, props: lastProps } = this;
const { style: nextStyle, data: nextData, scale: nextScale } = nextProps;
const { style: lastStyle, data: lastData, scale: lastScale } = lastProps;
Expand Down
5 changes: 4 additions & 1 deletion packages/f2/src/components/area/withArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { DataRecord } from '../../chart/Data';
export type AreaProps<TRecord extends DataRecord = DataRecord> = LineProps<TRecord>;

export default (View) => {
return class Area<TRecord extends DataRecord = DataRecord> extends withLine(View)<TRecord> {
return class Area<
TRecord extends DataRecord = DataRecord,
IProps extends AreaProps<TRecord> = AreaProps<TRecord>
> extends withLine(View)<TRecord, IProps> {
getDefaultCfg() {
return {
geomType: 'area',
Expand Down
14 changes: 7 additions & 7 deletions packages/f2/src/components/axis/withAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ type BBox = {
export { AxisProps };

export default (View) => {
return class Axis<TRecord extends DataRecord = DataRecord> extends Component<
AxisProps<TRecord> & ChartChildProps,
{}
> {
return class Axis<
TRecord extends DataRecord = DataRecord,
IProps extends AxisProps<TRecord> = AxisProps<TRecord>
> extends Component<IProps & ChartChildProps, {}> {
axisStyle: Style = {};

constructor(props: AxisProps<TRecord> & ChartChildProps) {
constructor(props: IProps & ChartChildProps) {
super(props);
const { chart, field } = props;

const scaleOption = this.getScaleOption(props);
chart.setScale(field as string, scaleOption);
}

willReceiveProps(nextProps: AxisProps<TRecord> & ChartChildProps) {
willReceiveProps(nextProps: IProps & ChartChildProps) {
const { props: lastProps } = this;
const { chart, field } = nextProps;

Expand All @@ -45,7 +45,7 @@ export default (View) => {
this.updateCoord();
}

getScaleOption(props: AxisProps<TRecord>) {
getScaleOption(props: IProps) {
const { type, tickCount, range, mask, formatter, ticks, min, max, nice } = props;

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/f2/src/components/gauge/withGauge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface GuageProps {
}

export default (View: ComponentType) => {
return class Guage extends Component<GuageProps> {
return class Guage<IProps extends GuageProps = GuageProps> extends Component<IProps> {
render() {
const { props, context } = this;
const { startAngle, endAngle, tickCount, center, r, tickOffset, tickLength } = props;
Expand Down
6 changes: 4 additions & 2 deletions packages/f2/src/components/guide/withGuide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ export interface GuideProps {
}

export default (View: ComponentType) => {
return class Guide extends Component<GuideProps & ChartChildProps> {
return class Guide<IProps extends GuideProps = GuideProps> extends Component<
IProps & ChartChildProps
> {
chart: Chart;
triggerRef: Ref;
guideBBox: any;

constructor(props: GuideProps & ChartChildProps) {
constructor(props: IProps & ChartChildProps) {
super(props);
// 创建ref
this.triggerRef = {};
Expand Down
8 changes: 4 additions & 4 deletions packages/f2/src/components/interval/withInterval.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export interface IntervalProps<TRecord extends DataRecord = DataRecord>
}

export default (Views) => {
return class Interval<TRecord extends DataRecord = DataRecord> extends Geometry<
TRecord,
IntervalProps<TRecord>
> {
return class Interval<
TRecord extends DataRecord = DataRecord,
IProps extends IntervalProps<TRecord> = IntervalProps<TRecord>
> extends Geometry<TRecord, IProps> {
getDefaultCfg() {
return {
geomType: 'interval',
Expand Down
4 changes: 3 additions & 1 deletion packages/f2/src/components/legend/withLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export interface LegendProps {
}

export default (View) => {
return class Legend extends Component<LegendProps & ChartChildProps> {
return class Legend<IProps extends LegendProps = LegendProps> extends Component<
IProps & ChartChildProps
> {
legendStyle: GroupStyleProps;
itemWidth: Number;
constructor(props) {
Expand Down
8 changes: 4 additions & 4 deletions packages/f2/src/components/line/withLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface LineProps<TRecord extends DataRecord = DataRecord> extends Geom
}

export default (View) => {
return class Line<TRecord extends DataRecord = DataRecord> extends Geometry<
TRecord,
LineProps<TRecord>
> {
return class Line<
TRecord extends DataRecord = DataRecord,
IProps extends LineProps<TRecord> = LineProps<TRecord>
> extends Geometry<TRecord, IProps> {
getDefaultCfg() {
return {
geomType: 'line',
Expand Down
4 changes: 3 additions & 1 deletion packages/f2/src/components/pieLabel/withPieLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export interface PieLabelProps {
}

export default (View) => {
return class PieLabel extends Component<PieLabelProps & ChartChildProps> {
return class PieLabel<IProps extends PieLabelProps = PieLabelProps> extends Component<
IProps & ChartChildProps
> {
triggerRef: Ref;
labels: [];
constructor(props) {
Expand Down
8 changes: 4 additions & 4 deletions packages/f2/src/components/point/withPoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { DataRecord } from '../../chart/Data';
export type PointProps<TRecord extends DataRecord = DataRecord> = GeometryProps<TRecord>;

export default (View) => {
return class Point<TRecord extends DataRecord = DataRecord> extends Geometry<
TRecord,
PointProps
> {
return class Point<
TRecord extends DataRecord = DataRecord,
IProps extends PointProps<TRecord> = PointProps<TRecord>
> extends Geometry<TRecord, IProps> {
getDefaultCfg() {
return {
geomType: 'point',
Expand Down
9 changes: 5 additions & 4 deletions packages/f2/src/components/sunburst/withSunburst.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ export interface SunburstProps<TRecord extends DataRecord = DataRecord> {
}

export default (View) => {
return class Sunburst<TRecord extends DataRecord = DataRecord> extends Component<
SunburstProps<TRecord>
> {
return class Sunburst<
TRecord extends DataRecord = DataRecord,
IProps extends SunburstProps<TRecord> = SunburstProps<TRecord>
> extends Component<IProps> {
coord: CoordController;
color: Category;
triggerRef: Ref[];

constructor(props: SunburstProps<TRecord>, context) {
constructor(props: IProps, context) {
super(props, context);
const { color, data } = props;

Expand Down
7 changes: 5 additions & 2 deletions packages/f2/src/components/tooltip/withTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ export interface TooltipState {
}

export default (View) => {
return class Tooltip extends Component<TooltipProps & ChartChildProps, TooltipState> {
constructor(props: TooltipProps & ChartChildProps) {
return class Tooltip<IProps extends TooltipProps = TooltipProps> extends Component<
IProps & ChartChildProps,
TooltipState
> {
constructor(props: IProps & ChartChildProps) {
super(props);
this.state = {
records: null,
Expand Down
9 changes: 5 additions & 4 deletions packages/f2/src/components/treemap/withTreemap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ export interface TreemapProps<TRecord extends DataRecord = DataRecord> {
}

export default (View) => {
return class Treemap<TRecord extends DataRecord = DataRecord> extends Component<
TreemapProps<TRecord>
> {
return class Treemap<
TRecord extends DataRecord = DataRecord,
IProps extends TreemapProps<TRecord> = TreemapProps<TRecord>
> extends Component<IProps> {
coord: CoordController;
color: Category;
triggerRef: Ref[];

constructor(props: TreemapProps<TRecord>, context) {
constructor(props: IProps, context) {
super(props, context);
const { color, data } = props;
this.coord = new CoordController();
Expand Down

0 comments on commit 7a90906

Please sign in to comment.