Skip to content

Commit

Permalink
feat: 组件初步完成
Browse files Browse the repository at this point in the history
  • Loading branch information
wb.linminghuang committed Jun 15, 2020
0 parents commit 39b1f30
Show file tree
Hide file tree
Showing 25 changed files with 411 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BROWSER=none
ESLINT=1
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-umi"
}
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/npm-debug.log*
/yarn-error.log
/yarn.lock
/package-lock.json

# production
/dist

# misc
.DS_Store

# umi
.umi
.umi-production
7 changes: 7 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/*.md
**/*.svg
**/*.ejs
**/*.html
package.json
.umi
.umi-production
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100,
"overrides": [
{
"files": ".prettierrc",
"options": { "parser": "json" }
}
]
}
44 changes: 44 additions & 0 deletions .umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { IConfig } from 'umi-types'; // ref: https://umijs.org/config/

const config: IConfig = {
treeShaking: true,
routes: [
{
path: '/',
component: '../layouts/index',
routes: [
{
path: '/demo',
component: './demo',
},
{
path: '/',
component: '../pages/index',
},
],
},
],
plugins: [
// ref: https://umijs.org/plugin/umi-plugin-react.html
[
'umi-plugin-react',
{
antd: true,
dva: true,
dynamicImport: false,
title: 'picture-select',
dll: false,
routes: {
exclude: [
/models\//,
/services\//,
/model\.(t|j)sx?$/,
/service\.(t|j)sx?$/,
/components\//,
],
},
},
],
],
};
export default config;
Empty file added mock/.gitkeep
Empty file.
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"private": true,
"scripts": {
"start": "umi dev",
"build": "umi build",
"test": "umi test",
"lint": "eslint {src,mock,tests}/**/*.{ts,tsx} --fix",
"precommit": "lint-staged"
},
"dependencies": {
"dva": "^2.6.0-beta.6",
"antd": "^3.19.5",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"@types/jest": "^23.3.12",
"@types/react": "^16.7.18",
"@types/react-dom": "^16.0.11",
"@types/react-test-renderer": "^16.0.3",
"babel-eslint": "^9.0.0",
"eslint": "^5.4.0",
"eslint-config-umi": "^1.4.0",
"eslint-plugin-flowtype": "^2.50.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.11.1",
"husky": "^0.14.3",
"lint-staged": "^7.2.2",
"react-test-renderer": "^16.7.0",
"umi": "^2.9.0",
"umi-plugin-react": "^1.8.0",
"umi-types": "^0.3.0"
},
"lint-staged": {
"*.{ts,tsx}": ["eslint --fix", "git add"],
"*.{js,jsx}": ["eslint --fix", "git add"]
},
"engines": {
"node": ">=8.0.0"
}
}
8 changes: 8 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const dva = {
config: {
onError(err: ErrorEvent) {
err.preventDefault();
console.error(err.message);
},
},
};
Binary file added src/assets/yay.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/components/picSelect/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
:global {
.pic-select {
.pic-list {}

.pic-item {
display: inline-block;
position: relative;
width: 200px;
height: 200px;

&-content {
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}

&-name {}
}
}
}
77 changes: 77 additions & 0 deletions src/components/picSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { Checkbox } from 'antd';
import './index.less';

export interface PicTureProps {
id: string;
name: string;
url: string;
}
interface PicSelectProps {
value?: string[];
onChange?: (v: string[]) => void;
list?: PicTureProps[];
}
/** 图片选择组件,样式还没来得及写 */
const PicSelect: React.FC<PicSelectProps> = props => {
const { value, onChange, list } = props;
useEffect(() => {
setLocalValue(value || []);
}, [value]);
const [localValue, setLocalValue] = useState<string[]>([]);
/** 单个选择回调 */
const singeChange = useCallback(
(id: string) => {
const tIndex = localValue.indexOf(id);
let nowValue: string[] = [];
if (tIndex !== -1) {
nowValue = [...localValue];
nowValue.splice(tIndex, 1);
} else {
nowValue = [...localValue, id];
}
setLocalValue(nowValue);
onChange && onChange(nowValue);
},
[localValue, onChange],
);
/** 全选按钮是否全部选中 */
const allCircled = useMemo(() => {
return list?.every((item: PicTureProps) => localValue.includes(item.id));
}, [localValue, list]);
/** 全选按钮点击回调 */
const allCircleChange = useCallback(() => {
let nowValue: string[] = [];
if (allCircled) {
nowValue = [];
} else {
nowValue = [...(list?.map(item => item.id) || [])];
}
setLocalValue(nowValue);
onChange && onChange(nowValue);
}, [list, onChange, localValue, allCircled]);
return (
<div className="pic-select">
<div className="pic-all-circle">
<Checkbox checked={allCircled} onChange={allCircleChange} />
<span>已选中{localValue.length}</span>
</div>
<div className="pic-list">
{list?.map(item => (
<div key={item.id} className="pic-item">
<Checkbox
onChange={() => {
singeChange(item.id);
}}
checked={localValue?.includes(item.id)}
></Checkbox>
<div className="pic-item-content" style={{ backgroundImage: `url(${item.url})` }} />
<div className="pic-item-name">{item.name}</div>
</div>
))}
</div>
</div>
);
};

export default PicSelect;
7 changes: 7 additions & 0 deletions src/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
html, body, #root {
height: 100%;
}

body {
margin: 0;
}
16 changes: 16 additions & 0 deletions src/layouts/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'jest';
import BasicLayout from '..';
import React from 'react';
import renderer, { ReactTestInstance, ReactTestRenderer } from 'react-test-renderer';

describe('Layout: BasicLayout', () => {
it('Render correctly', () => {
const wrapper: ReactTestRenderer = renderer.create(<BasicLayout />);
expect(wrapper.root.children.length).toBe(1);
const outerLayer = wrapper.root.children[0] as ReactTestInstance;
expect(outerLayer.type).toBe('div');
const title = outerLayer.children[0] as ReactTestInstance;
expect(title.type).toBe('h1');
expect(title.children[0]).toBe('Yay! Welcome to umi!');
});
});
15 changes: 15 additions & 0 deletions src/layouts/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

.normal {
font-family: Georgia, sans-serif;
text-align: center;
}

.title {
font-size: 2.5rem;
font-weight: normal;
letter-spacing: -1px;
background: darkslateblue;
padding: .6em 0;
color: white;
margin: 0;
}
8 changes: 8 additions & 0 deletions src/layouts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import styles from './index.css';

const BasicLayout: React.FC = props => {
return <div className={styles.normal}>{props.children}</div>;
};

export default BasicLayout;
Empty file added src/models/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions src/pages/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'jest';
import Index from '..';
import React from 'react';
import renderer, { ReactTestInstance, ReactTestRenderer } from 'react-test-renderer';


describe('Page: index', () => {
it('Render correctly', () => {
const wrapper: ReactTestRenderer = renderer.create(<Index />);
expect(wrapper.root.children.length).toBe(1);
const outerLayer = wrapper.root.children[0] as ReactTestInstance;
expect(outerLayer.type).toBe('div');
expect(outerLayer.children.length).toBe(2);

});
});
4 changes: 4 additions & 0 deletions src/pages/demo/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.normal {
background: #F279EE;
}
34 changes: 34 additions & 0 deletions src/pages/demo/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { useState, useCallback } from 'react';
import PicSelect from '@/components/picSelect';
import './index.less';

const pictures = [
{
id: '1',
name: 'foo',
url: 'https://gw.alipayobjects.com/mdn/rms_d212b7/afts/img/A*LlfeSa8N0WgAAAAAAAAAAABkARQnAQ',
},
{
id: '2',
name: 'foo',
url: 'https://gw.alipayobjects.com/mdn/rms_d212b7/afts/img/A*LlfeSa8N0WgAAAAAAAAAAABkARQnAQ',
},
{
id: '3',
name: 'foo',
url: 'https://gw.alipayobjects.com/mdn/rms_d212b7/afts/img/A*LlfeSa8N0WgAAAAAAAAAAABkARQnAQ',
},
];
const Demo: React.FC = () => {
const [picValue, setPicValue] = useState<string[]>([]);
const onChange = useCallback((values: string[]) => {
setPicValue(values);
}, []);
return (
<div className="demo">
<PicSelect list={pictures} onChange={onChange} value={picValue}></PicSelect>
</div>
);
};

export default Demo;
23 changes: 23 additions & 0 deletions src/pages/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

.normal {
font-family: Georgia, sans-serif;
margin-top: 4em;
text-align: center;
}

.welcome {
height: 512px;
background: url(../assets/yay.jpg) no-repeat center 0;
background-size: 512px 512px;
}

.list {
font-size: 1.2em;
margin: 1.8em 0 0;
list-style: none;
line-height: 1.5em;
}

.list code {
background: #f7f7f7;
}
Loading

0 comments on commit 39b1f30

Please sign in to comment.