localeProbider.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {
  2. LocaleProvider,
  3. Pagination,
  4. DatePicker,
  5. TimePicker,
  6. Calendar,
  7. Popconfirm,
  8. Table,
  9. Modal,
  10. Button,
  11. Select,
  12. Transfer,
  13. Radio,
  14. } from 'antd';
  15. import zhCN from 'antd/lib/locale-provider/zh_CN';
  16. import moment from 'moment';
  17. import 'moment/locale/zh-cn';
  18. moment.locale('en');
  19. const Option = Select.Option;
  20. const RangePicker = DatePicker.RangePicker;
  21. const columns = [
  22. {
  23. title: 'Name',
  24. dataIndex: 'name',
  25. filters: [
  26. {
  27. text: 'filter1',
  28. value: 'filter1',
  29. },
  30. ],
  31. },
  32. {
  33. title: 'Age',
  34. dataIndex: 'age',
  35. },
  36. ];
  37. class Page extends React.Component {
  38. state = {
  39. visible: false,
  40. };
  41. showModal = () => {
  42. this.setState({ visible: true });
  43. };
  44. hideModal = () => {
  45. this.setState({ visible: false });
  46. };
  47. render() {
  48. const info = () => {
  49. Modal.info({
  50. title: 'some info',
  51. content: 'some info',
  52. });
  53. };
  54. const confirm = () => {
  55. Modal.confirm({
  56. title: 'some info',
  57. content: 'some info',
  58. });
  59. };
  60. return (
  61. <div className="locale-components">
  62. <div className="example">
  63. <Pagination defaultCurrent={1} total={50} showSizeChanger />
  64. </div>
  65. <div className="example">
  66. <Select showSearch style={{ width: 200 }}>
  67. <Option value="jack">jack</Option>
  68. <Option value="lucy">lucy</Option>
  69. </Select>
  70. <DatePicker />
  71. <TimePicker />
  72. <RangePicker style={{ width: 200 }} />
  73. </div>
  74. <div className="example">
  75. <Button type="primary" onClick={this.showModal}>
  76. Show Modal
  77. </Button>
  78. <Button onClick={info}>Show info</Button>
  79. <Button onClick={confirm}>Show confirm</Button>
  80. <Popconfirm title="Question?">
  81. <a href="#">Click to confirm</a>
  82. </Popconfirm>
  83. </div>
  84. <div className="example">
  85. <Transfer dataSource={[]} showSearch targetKeys={[]} render={item => item.title} />
  86. </div>
  87. <div style={{ width: 319, border: '1px solid #d9d9d9', borderRadius: 4 }}>
  88. <Calendar fullscreen={false} value={moment()} />
  89. </div>
  90. <div className="example">
  91. <Table dataSource={[]} columns={columns} />
  92. </div>
  93. <Modal title="Locale Modal" visible={this.state.visible} onCancel={this.hideModal}>
  94. <p>Locale Modal</p>
  95. </Modal>
  96. </div>
  97. );
  98. }
  99. }
  100. class App extends React.Component {
  101. constructor() {
  102. super();
  103. this.state = {
  104. locale: null,
  105. };
  106. }
  107. changeLocale = e => {
  108. const localeValue = e.target.value;
  109. this.setState({ locale: localeValue });
  110. if (!localeValue) {
  111. moment.locale('en');
  112. } else {
  113. moment.locale('zh-cn');
  114. }
  115. };
  116. render() {
  117. const { locale } = this.state;
  118. return (
  119. <div>
  120. <div className="change-locale">
  121. <span style={{ marginRight: 16 }}>Change locale of components: </span>
  122. <Radio.Group defaultValue={undefined} onChange={this.changeLocale}>
  123. <Radio.Button key="en" value={undefined}>
  124. English
  125. </Radio.Button>
  126. <Radio.Button key="cn" value={zhCN}>
  127. 中文
  128. </Radio.Button>
  129. </Radio.Group>
  130. </div>
  131. <LocaleProvider locale={locale}>
  132. <Page
  133. key={locale ? locale.locale : 'en' /* Have to refresh for production environment */}
  134. />
  135. </LocaleProvider>
  136. </div>
  137. );
  138. }
  139. }
  140. ReactDOM.render(<App />, mountNode);